二十、Git 快速参考手册
云少二十、Git 快速参考手册
我们制作了一份 Git 快速参考手册供随时预览
Git cheat sheet 让你不用再去记所有的 git 命令
配置
列出当前配置
列出 repository 配置
1
| $ git config --local --list
|
列出全局配置
1
| $ git config --global --list
|
列出系统配置
1
| $ git config --system --list
|
设置用户名
1
| $ git config --global user.name “[firstname lastname]”
|
设置用户邮箱
1
| $ git config --global user.email “[valid-email]”
|
设置 git 命令输出为彩色
1
| $ git config --global color.ui auto
|
设置 git 使用的文本编辑器
1
| $ git config --global core.editor vi
|
配置文件
Repository 配置对应的配置文件路径[–local]
用户全局配置对应的配置文件路径 [–global]
系统配置对应的配置文件路径 [–local]
创建
复制一个已创建的仓库
1 2 3 4 5
| $ git clone ssh://user@domain.com/repo.git
$ git clone http://domain.com/user/repo.git
|
创建一个新的本地仓库
本地修改
显示工作路径下已修改的文件
显示与上次提交版本文件的不同
把当前所有修改添加到下次提交中
把对某个文件的修改添加到下次提交中
提交本地的所有修改
提交之前已标记的变化
附加消息提交
1
| $ git commit -m 'message here'
|
提交,并将提交时间设置为之前的某个日期
1
| git commit --date="`date --date='n day ago'`" -am "Commit Message"
|
修改上次提交
请勿修改已发布的提交记录
修改上次提交的 committer date
1
| GIT_COMMITTER_DATE="date" git commit --amend
|
修改上次提交的 author date
1
| git commit --amend --date="date"
|
把当前分支中未提交的修改移动到其他分支
1 2 3
| git stash git checkout branch2 git stash pop
|
将 stashed changes 应用到当前分支
删除最新一次的 stashed changes
搜索
从当前目录的所有文件中查找文本内容
在某一版本中搜索文本
提交历史
从最新提交开始,显示所有的提交记录 ( 显示 hash, 作者信息,提交的标题和时间 )
显示所有提交 ( 仅显示提交的 hash 和 message )
显示某个用户的所有提交
1
| $ git log --author="username"
|
显示某个文件的所有修改
仅显示远端 <remote/master>
分支与远端 <origin/master>
分支提交记录的差集
1
| $ git log --oneline <origin/master>..<remote/master> --left-right
|
谁,在什么时间,修改了文件的什么内容
显示 reflog
删除 reflog
分支与标签
列出所有的分支
列出所有的远端分支
切换分支
创建并切换到新分支
1
| $ git checkout -b <branch>
|
基于当前分支创建新分支
1
| $ git branch <new-branch>
|
基于远程分支创建新的可追溯的分支
1
| $ git branch --track <new-branch> <remote-branch>
|
删除本地分支
1
| $ git branch -d <branch>
|
强制删除一个本地分支
将会丢失未合并的修改
1
| $ git branch -D <branch>
|
给当前版本打标签
给当前版本打标签并附加消息
更新与发布
列出当前配置的远程端
显示远程端的信息
1
| $ git remote show <remote>
|
添加新的远程端
1
| $ git remote add <remote> <url>
|
下载远程端版本,但不合并到HEAD中
下载远程端版本,并自动与HEAD版本合并
1
| $ git remote pull <remote> <url>
|
将远程端版本合并到本地版本中
1
| $ git pull origin master
|
以rebase方式将远端分支与本地合并
1
| git pull --rebase <remote> <branch>
|
将本地版本发布到远程端
1
| $ git push remote <remote> <branch>
|
删除远程端分支
1 2 3
| $ git push <remote> :<branch> (since Git v1.5.0) or git push <remote> --delete <branch> (since Git v1.7.0)
|
发布标签:
合并与重置( Rebase )
将分支合并到当前 HEAD 中
将当前 HEAD 版本重置到分支中
请勿重置已发布的提交!
退出重置:
解决冲突后继续重置:
在编辑器中手动解决冲突后,标记文件为已解决冲突
:
1 2
| $ git add <resolved-file> $ git rm <resolved-file>
|
合并提交
1
| $ git rebase -i <commit-just-before-first>
|
把上面的内容替换为下面的内容:
原内容
1 2 3
| pick <commit_id> pick <commit_id2> pick <commit_id3>
|
替换为
1 2 3
| pick <commit_id> squash <commit_id2> squash <commit_id3>
|
撤销
放弃工作目录下的所有修改:
移除缓存区的所有文件(i.e. 撤销上次git add
):
放弃某个文件的所有本地修改:
1
| $ git checkout HEAD <file>
|
重置一个提交(通过创建一个截然不同的新提交)
将 HEAD 重置到指定的版本,并抛弃该版本之后的所有修改:
1
| $ git reset --hard <commit>
|
用远端分支强制覆盖本地分支
1
| git reset --hard <remote/branch> e.g., upstream/master, origin/my-feature
|
将 HEAD 重置到上一次提交的版本,并将之后的修改标记为未添加到缓存区的修改
将HEAD重置到上一次提交的版本,并保留未提交的本地修改
1
| $ git reset --keep <commit>
|
删除添加.gitignore
文件前错误提交的文件
1 2 3
| $ git rm -r --cached . $ git add . $ git commit -m "remove xyz file"
|