Git常用命令速查表
Git是目前世界上最先进的分布式版本控制系统,本文整理了日常开发中最常用的Git命令,方便快速查阅。
仓库操作
1 2 3 4 5 6 7 8
| $ git init
$ git init [project-name]
$ git clone [url]
|
配置管理
1 2 3 4 5 6 7 8 9
| $ git config --list
$ git config -e [--global]
$ git config [--global] user.name "[name]" $ git config [--global] user.email "[email address]"
|
文件操作
添加文件
1 2 3 4 5 6 7 8 9 10 11 12
| $ git add [file1] [file2] ...
$ git add [dir]
$ git add .
$ git add -p
|
删除和重命名文件
1 2 3 4 5 6 7 8
| $ 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
| $ 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] ...
|
分支管理
分支操作
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
| $ git branch
$ git branch -r
$ git branch -a
$ git branch [branch-name]
$ git checkout -b [branch]
$ git checkout -b [branch] [remote-branch]
$ git branch [branch] [commit]
$ git branch --track [branch] [remote-branch]
$ git checkout [branch-name]
$ git checkout -
$ git branch --set-upstream-to=origin/<branch> <branch>
|
分支合并与删除
1 2 3 4 5 6 7 8 9 10 11 12
| $ git merge [branch]
$ git cherry-pick [commit]
$ 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
| $ git tag
$ git tag [tag]
$ git tag [tag] [commit]
$ git tag -d [tag]
$ git push origin :refs/tags/[tagName]
$ git show [tag]
$ git push [remote] [tag]
$ git push [remote] --tags
$ git checkout -b [branch] [tag]
|
查看信息
基本信息查看
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| $ git status
$ git log
$ git log --stat
$ git log -S [keyword]
$ git log [tag] HEAD --pretty=format:%s
$ git log [tag] HEAD --grep feature
|
文件历史查看
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| $ git log --follow [file] $ git whatchanged [file]
$ git log -p [file]
$ git log -5 --pretty --oneline
$ git shortlog -sn
$ git blame [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
| $ git diff
$ git diff --cached [file]
$ git diff HEAD
$ git diff [first-branch]...[second-branch]
$ git diff --shortstat "@{0 day ago}"
$ git show [commit]
$ git show --name-only [commit]
$ git show [commit]:[filename]
$ git reflog
|
远程同步
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| $ git fetch [remote]
$ git remote -v
$ git remote show [remote]
$ git remote add [shortname] [url]
$ git pull [remote] [branch]
$ git push [remote] [branch]
$ git push [remote] --force
$ git push [remote] --all
|
撤销操作
文件恢复
1 2 3 4 5 6 7 8
| $ git checkout [file]
$ git checkout [commit] [file]
$ git checkout .
|
版本回退
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| $ git reset [file]
$ git reset --hard
$ git reset [commit]
$ git reset --hard [commit]
$ git reset --keep [commit]
$ git revert [commit]
|
暂存操作
1 2 3
| $ git stash $ git stash pop
|
实用技巧
生成一个可供发布的压缩包
查看提交者统计信息
1 2 3 4 5
| $ git shortlog -sn --no-merges | head -n 5
$ git shortlog -sn --no-merges --since="2022-01-01" --before="2023-01-01"
|
查看仓库大小
总结
Git是一个功能强大的版本控制工具,掌握这些常用命令可以帮助你更高效地进行代码管理和团队协作。随着使用的深入,你会发现Git还有更多高级功能等待探索。