由于最近刚刚快速过完了一遍《Pro Git》,因此想要将个人比较常用的 git 命令整理出来,以便需要时翻看。因此本文是一个个人向的笔记,不会涉及对 git 原理的介绍,这部分的具体内容可以参考《Pro Git》。
准备阶段
初始化配置
1 2 3 4 5 6 7 8
| ~/.gitconfig
git config --global user.name <your_name>
git config --globale user.email <your_email>
|
新建仓库
1 2 3 4 5
| git init <repo_name>
git clone <repo_url>
|
工作阶段
查看日志
1 2 3 4 5 6 7 8 9
| git log
git reflog
git status
|
提交文件
1 2 3 4 5 6 7 8 9
| git add <file_path>
git add -p <file_path>
git commit
git commit --amend
|
变更文件
1 2 3 4 5 6 7 8 9
|
git rm <file_path>
git rm --cached <file_path>
git mv <file_path> <new_file_path>
|
版本回退
1 2 3 4 5 6 7
|
git reset --<reset_flag> <commit>
git revert <commit>
|
差异比较
1 2 3 4 5
| git diff
git diff --cached
|
分支
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
| git branch
git branch <branch_name>
git checkout -b <branch_name>
git switch <branch_name>
git merge <branch_name>
git rebase <branch_name>
git branch -d <branch_name>
git branch -D <branch_name>
git branch -m <new_name>
|
远程仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| git remote add <remote_name> <remote_url>
git remote -v
git push <remote_name> <local_branch>:<remote_branch>
git fetch <remote_name>
git pull <remote_name> <remote_branch>:<local_branch>
git pull --rebase <remote_name> <remote_branch>:<local_branch>
|
标签
1 2 3 4 5 6 7 8 9 10 11 12 13
| git tag -a <tag_name>
git show <tag_name>
git tag <tag_name>
git tag <tag_name> <commit>
git push origin <tag_name>
|
补丁
1 2 3 4 5 6 7 8 9 10
| git show <commit> > 1.patch
git apply 1.patch
git format-patch -3
git am <file_path>
|
常用引用与限定符
1 2 3 4 5 6 7 8 9 10
| HEAD
HEAD^
HEAD~n
-- <file_path>
|
实用工作场景
提取仓库中的特定提交到当前分支
1 2 3 4 5 6
| git cherry-pick <commit>
git add <file_path>
git cherry-pick --continue
|
暂存工作区更改
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git stash push -m <message>
git stash pop
git stash list
git stash apply stash@{n}
git stash drop stash@{n}
|
提交大文件
1 2 3 4 5 6 7 8 9 10
| sudo apt install git-lfs git lfs install
git lfs track '*.bin'
git add largefile.bin git commit
|
创建无历史记录的全新分支
1 2 3 4 5 6 7 8 9
| git checkout --orphan <branch_name>
git rm -rf --cached .
|
调试手段
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| git blame <file_path>
git checkout master
git bisect reset
git bisect start
git bisect good <good_commit> git bisect bad <bad_commit>
git bisect bad/good
|
参考资料
【GeekHour】一小时Git教程_哔哩哔哩_bilibili
前言 · Pro Git 第二版 简体中文