外观
版本管理
Git 常用命令
目前最常用的代码管理工具是 Git。
Git 分支相关命令
bash
# 列出本地分支
git branch
# 列出所有分支(含远程)
git branch -a
# 创建新分支
git branch <branch-name>
# 切换分支(旧式命令)
git checkout <branch-name>
# 切换分支(新式命令,Git 2.23+)
git switch <branch-name>
# 创建并切换到新分支
git switch -c <branch-name>
# 合并分支到当前分支
git merge <branch-name>
# 变基当前分支到目标分支
git rebase <target-branch>
# 拣选某次提交到当前分支
git cherry-pick <commit-hash>
# 删除已合并的分支
git branch -d <branch-name>
# 强制删除未合并的分支
git branch -D <branch-name>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
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
Git 提交相关命令
bash
# 查看工作区状态
git status
# 暂存指定文件
git add <file>
# 暂存所有变更
git add -A
# 提交暂存区
git commit -m "提交说明"
# 跳过暂存区直接提交(仅已跟踪文件)
git commit -a -m "提交说明"
# 修改最近一次提交(未推送时使用)
git commit --amend
# 查看提交历史(单行简洁模式)
git log --oneline
# 查看提交历史(含分支图)
git log --oneline --graph --all
# 查看工作区与暂存区的差异
git diff
# 查看暂存区与最近提交的差异
git diff --cached
# 查看某次提交的详情
git show <commit-hash>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
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
Git 撤销相关命令
bash
# 撤销工作区的修改(恢复文件到最近一次暂存或提交的状态)
git restore <file>
# 撤销暂存区的文件(取消 add,保留工作区修改)
git restore --staged <file>
# 重置到指定提交,保留工作区修改
git reset --soft <commit-hash>
# 重置到指定提交,丢弃之后的所有修改(慎用)
git reset --hard <commit-hash>
# 撤销某次提交(生成一个新的反向提交)
git revert <commit-hash>
# 移除未跟踪的文件
git clean -n # 预览将要删除的文件
git clean -fd # 强制删除未跟踪的文件和目录1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Git 远程相关命令
bash
# 克隆远程仓库
git clone <repository-url>
# 查看远程仓库
git remote -v
# 添加远程仓库
git remote add <name> <url>
# 删除远程仓库
git remote remove <name>
# 拉取远程变更(不自动合并)
git fetch <remote>
# 拉取并合并远程变更
git pull <remote> <branch>
# 推送本地分支到远程
git push <remote> <branch>
# 推送并设置上游跟踪
git push -u <remote> <branch>
# 删除远程分支
git push <remote> --delete <branch>
# 推送所有标签
git push --tags1
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
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
Git 其他命令
bash
# 储藏工作区修改
git stash # 暂存当前修改
git stash list # 查看储藏列表
git stash pop # 恢复最近一次储藏并删除
git stash apply stash@{n} # 恢复指定储藏但不删除
# 标签管理
git tag # 列出标签
git tag <tag-name> # 创建轻量标签
git tag -a <tag-name> -m "说明" # 创建附注标签
git tag -d <tag-name> # 删除本地标签
# 查看文件逐行归属
git blame <file>
# 二分查找引入 bug 的提交
git bisect start
git bisect good <commit> # 标记已知正常版本
git bisect bad <commit> # 标记已知有 bug 版本
# 重复测试后标记 good/bad,Git 会逐步缩小范围
git bisect reset # 结束二分查找
# 全局配置 Git 用户信息
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
# 指定 Git 需要打开编辑器时调用什么程序
git config --global core.editor vim
# 设置 HTTP/HTTPS 代理(以 7890 端口为例)
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy1
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
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