常用Git命令清单
本文参考并扩展自http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html
Git基本概念
Git命令几个专用名词的译名如下:
- Workspace:工作区
- Index / Stage:暂存区
- Repository:仓库区(或本地仓库)
- Remote:远程仓库
一、新建代码库
1 2 3 4 5 6 7 8 9 10 11
| $ git init
$ git init [project-name]
$ git clone [url]
$ git clone -b [branch-name] [url]
|
二、配置
Git的设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| $ git config --list
$ git config -e [--global]
$ git config [--global] user.name "[name]" $ git config [--global] user.email "[email address]"
$ git config [--global] core.editor "vim"
$ git config [--global] diff.tool "vimdiff"
$ git config [--global] color.ui true
|
三、增加/删除文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| $ git add [file1] [file2] ...
$ git add [dir]
$ git add .
$ git add -u
$ git rm [file1] [file2] ...
$ git rm --cached [file]
$ git mv [file-original] [file-renamed]
|
四、代码提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| $ git commit -m [message]
$ git commit [file1] [file2] ... -m [message]
$ git commit -a
$ git commit -v
$ git commit --amend -m [message]
$ git commit --amend [file1] [file2] ...
$ git commit -m [message] --no-verify
|
五、分支管理
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 41 42 43 44 45 46 47 48
| $ git branch
$ git branch -r
$ git branch -a
$ git branch [branch-name]
$ git checkout -b [branch]
$ git branch [branch] [commit]
$ git branch --track [branch] [remote-branch]
$ git checkout [branch-name]
$ git checkout -
$ git branch --set-upstream [branch] [remote-branch]
$ git merge [branch]
$ git rebase [branch]
$ git cherry-pick [commit]
$ git branch -d [branch-name]
$ git branch -D [branch-name]
$ git push origin --delete [branch-name] $ git branch -dr [remote/branch]
|
六、标签管理
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
| $ git tag
$ git tag [tag]
$ git tag -a [tag] -m [message]
$ git tag [tag] [commit]
$ git show [tag]
$ git push [remote] [tag]
$ git push [remote] --tags
$ git tag -d [tag]
$ git push [remote] --delete [tag]
$ git checkout -b [branch] [tag]
|
七、查看信息
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 41 42 43 44 45 46 47 48
| $ git status
$ git log
$ git log --stat
$ git log --follow [file] $ git whatchanged [file]
$ git log -p [file]
$ git blame [file]
$ git diff
$ git diff --cached [file]
$ git diff HEAD
$ git diff [first-branch]...[second-branch]
$ git show [commit]
$ git show --name-only [commit]
$ git show [commit]:[filename]
$ git reflog
$ git log --graph --oneline
$ git log --all --oneline --graph
|
八、远程同步
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
| $ git fetch [remote]
$ git remote -v
$ git remote show [remote]
$ git remote add [shortname] [url]
$ git remote set-url [remote] [new-url]
$ git pull [remote] [branch]
$ git push [remote] [branch]
$ git push [remote] --force
$ git push [remote] --all
$ git push [remote] --tags
|
九、撤销操作
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
| $ git checkout [file]
$ git checkout [commit] [file]
$ git checkout .
$ git reset [file]
$ git reset --hard
$ git reset [commit]
$ git reset --hard [commit]
$ git reset --keep [commit]
$ git revert [commit]
$ git checkout -- [file]
$ git reset HEAD [file]
|
十、高级操作
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
| $ git archive
$ git clean -f
$ git clean -fd
$ git add -i
$ git log --name-status
$ git log -S "[search-term]"
$ git log -- [file]
$ git diff --shortstat
$ git log --graph
$ git shortlog -sn
$ git log --author="[author-name]"
$ git log --since="[date]" --until="[date]"
|
十一、Git工作流最佳实践
- 主分支(master/main):保持稳定,只用于发布版本
- 开发分支(develop):日常开发的主要分支
- 功能分支(feature/*):开发新功能
- 修复分支(hotfix/*):修复生产环境的紧急问题
- 发布分支(release/*):准备发布的分支
十二、常见问题解决
- 冲突解决:当合并分支时出现冲突,需要手动编辑冲突文件,然后提交
- 撤销错误提交:使用
git reset 或 git revert
- 忘记添加文件:使用
git commit --amend 追加文件
- 误删除分支:使用
git reflog 找回丢失的提交
- 忽略文件:在
.gitignore 文件中添加需要忽略的文件或目录
十三、参考资料