参考资料如下:
常用 Git 命令清单
Git教程
Git系列之Refs 与 Reflog
使用git stash命令保存和恢复进度
一般来说,日常使用只要记住下图 6 个命令,就可以了。但是熟练使用,恐怕要记住 60~100 个命令。
下面是常用 Git 命令清单。几个专用名词的译名如下:
Workspace:工作区Index / Stage:暂存区Repository:仓库区(或本地仓库)Remote:远程仓库
一、新建代码库
$
git init
$
git init
[project-name
]
$
git clone
[url
]
$
git clone --depth
=1
[url
]
二、配置
Git的设置文件为 .gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。
$
git config --list
$
git config -e
[--global
]
$
git config
[--global
] user.name
"[name]"
$
git config
[--global
] user.email
"[email address]"
说明:设置提交代码时的用户信息时,可选参数包含 --global、--system、--local。
--global 表示给整个计算机一次性设置--system 表示给当前用户一次性设置--local 表示给当前项目一次性设置
上述三个配置优先级从低到高,即高优先级配置会覆盖低优先级配置。
三、增加/删除文件
$
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
]
$
git reset
[file
]
说明:
使用 git rm 来删除文件,同时还会将这个删除操作记录下来,git commit 之后仓库区中相应文件也就被删除了;而使用 rm 来删除文件,仅仅是删除了物理文件,没有将其从 git 的记录中删除,使用 git checkout [file] 还能恢复文件。
git rm 删除过的文件,执行 git commit 提交时,会自动将删除该文件的操作提交上去。用 rm 命令直接删除的文件,执行 git commit 提交时,则不会将删除该文件的操作提交上去。不过不要紧,即使已经通过 rm 将某个文件删除掉了,也可以再通过 git rm 命令重新将该文件从 git 的记录中删除掉。
git mv [file-original] [file-renamed] 相当于执行了三条命令:
$
mv [file-original
] [file-renamed
]
$
git rm [file-original
]
$
git add
[file-renamed
]
四、代码提交
$
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 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 branch -D
[branch-name
]
$
git push origin --delete
[branch-name
]
$
git push origin :
[branch-name
]
$
git branch -dr
[origin/branch-name
]
六、标签
$
git tag
$
git tag
[tag
]
$
git tag
[tag
] [commit
]
$
git tag -d
[tag
]
$
git push origin :refs/tags/
[tagName
]
$
git show
[tag
]
$
git push
[origin
] [tag
]
$
git push
[origin
] --tags
$
git checkout -b
[branch
] [tag
]
七、查看信息
$
git status
$
git log
$
git log --stat
$
git log -S
[keyword
]
$
git log --follow
[file
]
$
git whatchanged
[--stat
] [file
]
$
git log -p
[file
]
$
git log -5 --pretty --oneline
$
git shortlog -sn
$
git blame
[file
]
$
git diff
$
git diff --cached
[file
]
$
git diff HEAD
$
git diff [branch1
] [branch2
]
$
git diff [commit1
] [commit2
]
$
git diff --shortstat
"@{0 day ago}"
$
git show
[commit
]
$
git show --name-only
[commit
]
$
git show
[commit
]:
[filename
]
$
git reflog
注意:当某一分支的版本历史信息较多时,git log 会分页展示信息。若不想继续查看历史信息,在末行的命令行模式 : 下输入 q,然后 enter 即可。
八、远程同步
$
git fetch
[remote
]
$
git remote -v
$
git remote show
[remote
]
$
git remote add
[shortname
] [url
]
$
git pull
[remote
] [branch
]
$
git push
[remote
] [local_branch
] : [remote_branch
]
$
git push
[remote
] [branch
]
$
git push
[remote
] --force
$
git push
[remote
] --all
九、撤销
$
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 stash
[save
'message']
$
git stash list
$
git stash pop
$
git stash pop --index
[stashId
]
$
git stash pop stash@
{stashId
}
$
git stash apply
$
git stash apply --index
[stashId
]
$
git stash drop
[stashId
]
$
git stash
clear