Git 和网络代理相关的曾经使用过的命令

Git 命令

很多人学习git的时候,命令五花八门的,我认为总结一些常用的命令是很重要的,以下是本人在开发过程中常用到命令

基本操作

1
2
git init
git commit -m""
  • 添加文件

    • 记录删除操作:
      1
      git add .
    • 不记录删除操作:
      1
      git add --all
  • 远程仓库

    • 添加远程:
      1
      2
      git remote add origin http...  # 添加远程
      git remote add upstream https: # 添加上游
    • 查看远程:
      1
      git remote -v
  • 推送与拉取

    1
    2
    git push -u origin master
    git pull origin master
  • 查看版本信息

    • 查看版本号:
      1
      git reflog
    • 查看最近 5 条版本信息:
      1
      git log -n 5 --oneline
  • 版本回退

    • 硬回退:
      1
      git reset --hard 版本号
    • 软回退:
      1
      git reset --soft 6d3a76f

分支操作

  • 创建并切换分支
    1
    git checkout -b online
  • 分支合并
    1
    git merge online  # 把 master 合并到 online 分支

配置用户信息

1
2
git config [--global] user.name '吃面包'
git config [--global] user.email '14731398+eat-bread@user.noreply.gitee.com'

配置代理

1
2
3
4
5
6
7
8
9
10
11
# 配置代理
git config --global http.proxy http://127.0.0.1:7897
git config --global https.proxy http://127.0.0.1:7897

# 使用局域网代理
git config --global http.proxy http://192.168.1.8:7897
git config --global https.proxy http://192.168.1.8:7897

# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy

忽略文件

  • 移除文件夹但保留本地文件:
    1
    git rm -r --cached **/build/
  • 更新 .gitignore
    1
    git add .gitignore

回退 GitHub 版本

  1. 在 GitHub 上无法直接回退,需要借助本地操作。
  2. 获取需要回退到的 commit 哈希值,复制。
  3. 克隆仓库:
    1
    git clone <repository>
  4. 执行回退:
    1
    2
    git reset --hard <哈希值>
    git push --force # 注意:会直接覆盖 GitHub 的记录,谨慎使用!

恢复被删除的目录

  1. 提取某个版本的目录或文件:

    1
    git checkout abc1234^ -- [相对目录/文件路径]
    • 从指定的父版本 abc1234 中提取文件或目录。
    • 提取后会恢复到当前分支的工作区。
  2. 提交恢复内容:

    1
    2
    git add [目录]
    git commit -m"恢复目录"

问题

SSL 证书错误

1
fatal: unable to access 'https://github.com/eatbreads/my_snake.git/': SSL certificate problem: unable to get local issuer certificate

解决方法:

1
git config --global http.sslverify false

代理连接失败

1
fatal: unable to access 'https://github.com/eatbreads/fly_server.git/': Failed connect to 172.17.44.73:7897; No route to host

注意: 有时即使配置了代理也可能无法连接。