常用 Git 命令清单
专用名词的译名如下:
- Workspace:工作区
- Index / Stage:暂存区
- Repository:仓库区(或本地仓库)
- Remote:远程仓库
一、新建代码库
1 2 3 4 5 6 7 8
| # 在当前目录新建一个Git代码库 $ git init
# 新建一个目录,将其初始化为Git代码库 $ git init [project-name]
# 下载一个项目和它的整个代码历史 $ git clone [url]
|
二、配置
Git的设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。
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 13 14 15 16 17 18 19 20 21
| $ git add [file1] [file2] ...
$ git add [dir]
$ git add .
$ git add -p
$ 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]
# 提交工作区自上次commit之后的变化,直接到仓库区 $ git commit -a
# 提交时显示所有diff信息 $ git commit -v
# 使用一次新的commit,替代上一次提交 # 如果代码没有任何新变化,则用来改写上一次commit的提交信息 $ git commit --amend -m [message]
# 重做上一次commit,并包括指定文件的新变化 $ 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 33 34 35 36 37 38 39 40 41 42
| $ 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 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
# 新建一个tag在当前commit $ 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 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| # 显示有变更的文件 $ git status
# 显示当前分支的版本历史 $ git log
# 显示commit历史,以及每次commit发生变更的文件 $ git log --stat
# 搜索提交历史,根据关键词 $ git log -S [keyword]
# 显示某个commit之后的所有变动,每个commit占据一行 $ git log [tag] HEAD --pretty=format:%s
# 显示某个commit之后的所有变动,其"提交说明"必须符合搜索条件 $ git log [tag] HEAD --grep feature
# 显示某个文件的版本历史,包括文件改名 $ git log --follow [file] $ git whatchanged [file]
# 显示指定文件相关的每一次diff $ git log -p [file]
# 显示过去5次提交 $ git log -5 --pretty --oneline
# 显示所有提交过的用户,按提交次数排序 $ git shortlog -sn
# 显示指定文件是什么人在什么时间修改过 $ git blame [file]
# 显示暂存区和工作区的差异 $ git diff
# 显示暂存区和上一个commit的差异 $ git diff --cached [file]
# 显示工作区与当前分支最新commit之间的差异 $ 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
# 显示最近几次提交 以彩色对比的形式 $ git log --graph --pretty=oneline --abbrev-commit
|
八、远程同步
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 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
|
$ git checkout [file]
$ git checkout [commit] [file]
$ git checkout .
$ git reset [file]
$ git reset --hard
$ git reset [commit]
$ git reset --hard [commit] 注意:重置之后需要强制更新, $ git push origin HEAD --force
$ git reset --keep [commit]
$ git revert [commit]
$ git stash $ git stash pop
|
十、其他