Git 相关汇总

◇ 初次使用及配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 安装 git
scoop install git
# 查看已有配置
git config --list
# 配置用户信息
git config --global user.name "username"
git config --global user.email [email protected]
# 设置代理
git config --global https.proxy http://127.0.0.1:xxxx
git config --global https.proxy https://127.0.0.1:xxxx
# 移除代理
git config --global --unset http.proxy
git config --global --unset https.proxy
# 生成 SSH KEY
# 默认情况下,密钥文件将存储在当前用户根目录下的.ssh文件夹中
ssh-keygen -t rsa -b 4096 -C "[email protected]"
# 测试
ssh -T [email protected]

◇ 常用命令

* 基础命令

1
2
3
4
5
6
7
git init    # 初始化本地仓库

git add . # 将当前目录中的所有更改添加到Git暂存区
git commit -m "commit message" # 将已经暂存的更改提交到本地Git存储库
git push origin master # 将本地master分支推送到远程分支
git push origin branch1:branch2 # 将本地branch1分支推送到远程branch2分支
git push -f origin feature-branch # 强制推送分支

* 远程相关

1
2
3
4
5
git clone git://example.com/myproject     # 克隆远程默认分支到本地

git remote -v # 查看所有关联远程仓库
git remote add <remote-name> <remote-url> # 添加远程仓库并命名
git remote rm <remote-name> # 删除远程仓库

* 分支相关

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
git branch        # 查看本地分支
git branch -r # 查看远程分支
git branch -a # 查看所有分支(包括远程分支)
git branch -vv # 查看当前分支与远程分支的关联关系

git branch -m <new-branch-name> # 重命名当前分支名称
git branch -m <old-branch-name> <new-branch-name> # 重命名特定分支名称

git checkout <branch-name> # 切换本地分支
git checkout -b <new-branch-name> # 创建一个新分支,并立即切换到该分支
# ↓↓↓创建一个与远程分支关联的本地分支,并切换到该本地分支
git checkout -b <local-branch-name> origin/<remote-branch-name>

git branch -d <branch-name> # 安全删除已经合并的本地分支
git branch -D <branch-name> # 强制删除本地分支
git push origin --delete <remote-branch-name> # 删除远程分支

* 元信息相关

1
2
3
4
5
6
7
git fetch origin  # 获取远程分支元信息
# 获取特定远程分支元信息
git fetch origin <remote-branch-name>

# 删除本地库中远程分支的元信息,不影响远程分支
git branch -dr origin/<remote-branch-name>
git fetch -p # 精简本地库,删除本地库中不再存在的远程分支的元信息

* 合并相关

1
2
3
git merge branch1     # 合并branch1分支的内容到当前分支
git pull origin/<remote-branch-name> # 合并远程分支
git pull origin b:a # 合并远程分支b到本地分支a

* 子模块相关

1
2
3
4
# 添加子模块
git submodule add -b <remote-branch> https://xxx.git /<path>
# 自动克隆子模块
git clone --recursive https://github.com/example/example.git

* 标签相关

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 创建一个标签
git tag v1.0.0
# 给历史 commit 打上标签
git tag v1.0.8 ba9f9e

# 推送标签至远程仓库
git push origin v1.0.8
# 推送所有标签
git push origin --tags

# 查看所有标签
git tag
# 查看 1.0.x 版本的 tag
git tag -l "v1.0*"
# 查看标签详细信息
git show v1.0.8

# 删除本地仓库上的标签
git tag -d v1.0.9
# 更新远程仓库
git push origin :refs/tags/v1.0.9

# 直接删除远程标签
git push origin --delete v1.0.9
# 同步到本地
# 注意:会强制同步远程 tag 到本地,所以会导致本地新建的未提交到远程服务器的 tag 也会被删除
git fetch --prune --prune-tags

# 创建标签并添加附加信息
git tag v2.0.0 -m "version 2.0.0 released"
# 添加多行信息可以添加多个 -m "<message>"
git tag v2.0.0 -m "version 2.0.0 released" -m "rebuild with react hooks" -m "support typescript"

# 查看三行的 tag 信息
git tag -n3

# 编辑已有 tag,除了删除重新打 tag 之外还可以用
git tag v1.0.0 v1.0.0^{} -f -m "first commit"
# 此时如果线上已经存在这个 tag,需要强制推送
git push origin -f v1.0.0

◇ 其它情景

▷ GitHub历史版本回退

1. 查找历史版本

网站操作:

浏览 GitHub 上的提交历史记录,找到要回退的版本,复制 commit id。

本地操作:

使用git log命令查看所有的历史版本,获取 git 的某个历史版本的 id。

id 格式如下:

1
fae6966548e3ae76cfa7f38a461c438cf75ba965

2. 恢复到历史版本

1
git reset --hard fae6966548e3ae76cfa7f38a461c438cf75ba965

3. 强制推到远程服务器

1
git push -f origin master

▷ 克隆或打开默认分支以外的分支

1. 克隆远程仓库

1
2
git clone git://example.com/myproject
cd myproject

2. 查看分支

1
2
3
4
5
6
7
8
9
10
11
$ git branch      # 查看本地分支,显示如下:
* master

$ git branch -a # 查看所有分支,显示如下:
* master
remotes/origin/HEAD -> origin/master
remotes/origin/develop
remotes/origin/feature
remotes/origin/feature-im
remotes/origin/master
remotes/origin/newbranch

如果没有远程信息或信息已过时,可执行以下命令更新远程分支元信息:

1
git fetch origin

如果分支数过多,建议直接指定需要的分支,避免无关信息占用本地空间:

1
git fetch origin feature

3. 切换分支

有 4 种方法:

1
2
3
4
5
6
7
8
9
10
11
12
# 直接切换到该分支
git checkout origin/feature

# 创建一个与远程分支关联的本地分支,并切换到该分支,本地分支可自定义名称
git checkout -b feature origin/feature

# 在本地建立一个和远程分支名字一样的分支,不可自定义名称
git checkout -t origin/feature

# 获取特定远程分支元信息到本地分支,需要手动切换到该分支
git fetch origin feature:feature
git checkout feature

切换后再次查看:

1
2
3
$ git branch      # 查看本地分支,显示如下:
master
* feature

▷ git 开发项目流程

git 开发项目简明流程图

◇ 参考内容

  1. GitHub历史版本回退. https://blog.csdn.net/u013201439/article/details/80140863
  2. Github使用之git回退到某个历史版本. https://blog.csdn.net/yxys01/article/details/78454315
  3. 怎么用git clone 远程的所有分支. https://www.jianshu.com/p/0fe715a7fbb3
  4. git 设置和取消代理. https://gist.github.com/laispace/666dd7b27e9116faece6
  5. 如何重命名 Git 分支. https://www.freecodecamp.org/chinese/news/git-rename-branch-how-to-change-a-local-branch-name/
  6. git clone 含有子模块的项目. https://www.cnblogs.com/codingbit/p/git-clone-with-submodule.html
  7. 使用 git tag 给项目打标签. https://segmentfault.com/a/1190000040523583