Git命令参考手册(文本版)

By | 2014年6月3日
git init ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 初始化本地git仓库(创建新仓库)
git config –global user.name “xxx” ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 配置用户名
git config –global user.email “[email protected]” ? ? ? ? ? ? ?# 配置邮件
git config –global color.ui true ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ?? ? ? # git status等命令自动着色
git config –global color.status auto
git config –global color.diff auto
git config –global color.branch auto
git config –global color.interactive auto
git clone git+ssh://[email protected]/VT.git ? ? ? ? ? # clone远程仓库
git status ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ??# 查看当前版本状态(是否修改)
git add xyz ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ?# 添加xyz文件至index
git add . ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ?# 增加当前子目录下所有更改过的文件至index
git commit -m ‘xxx’ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 提交
git commit –amend -m ‘xxx’ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? # 合并上一次提交(用于反复修改)
git commit -am ‘xxx’ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ??# 将add和commit合为一步
git rm xxx ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ??# 删除index中的文件
git rm -r * ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? # 递归删除
git log ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? # 显示提交日志
git log -1 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ?? ?# 显示1行日志 -n为n行
git log -5
git log –stat ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#?显示提交日志及相关变动文件
git log -p -m
git show dfb02e6e4f2f7b573337763e5c0013802e392818 ? ? ? ? # 显示某个提交的详细内容
git show dfb02 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ??# 可只用commitid的前几位
git show HEAD ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? # 显示HEAD提交日志
git show HEAD^ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ??# 显示HEAD的父(上一个版本)的提交日志 ^^为上两个版本 ^5为上5个版本
git tag ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? # 显示已存在的tag
git tag -a v2.0 -m ‘xxx’ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 增加v2.0的tag
git show v2.0 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 显示v2.0的日志及详细内容
git log v2.0 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ??# 显示v2.0的日志
git diff ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ? ? ?# 显示所有未添加至index的变更
git diff –cached ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ???# 显示所有已添加index但还未commit的变更
git diff HEAD^ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ?# 比较与上一个版本的差异
git diff HEAD — ./lib ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? ? ? ?# 比较与HEAD版本lib目录的差异
git diff origin/master..master ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ?# 比较远程分支master上有本地分支master上没有的
git diff origin/master..master –stat ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 只显示差异的文件,不显示具体内容
git remote add origin git+ssh://[email protected]/VT.git ? ? ? # 增加远程定义(用于push/pull/fetch)
git branch ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ?# 显示本地分支
git branch –contains 50089 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 显示包含提交50089的分支
git branch -a ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 显示所有分支
git branch -r ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 显示所有原创分支
git branch –merged ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 显示所有已合并到当前分支的分支
git branch –no-merged ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 显示所有未合并到当前分支的分支
git branch -m master master_copy ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 本地分支改名
git checkout -b master_copy ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 从当前分支创建新分支master_copy并检出
git checkout -b master master_copy ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 上面的完整版
git checkout features/performance ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 检出已存在的features/performance分支
git checkout –track hotfixes/BJVEP933 ? ? ? ? ? ? ? ? ? ?# 检出远程分支hotfixes/BJVEP933并创建本地跟踪分支
git checkout v2.0 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 检出版本v2.0
git checkout -b devel origin/develop ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 从远程分支develop创建新本地分支devel并检出
git checkout — README ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 检出head版本的README文件(可用于修改错误回退)
git merge origin/master ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 合并远程master分支至当前分支
git cherry-pick ff44785404a8e ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 合并提交ff44785404a8e的修改
git push origin master ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 将当前分支push到远程master分支
git push origin :hotfixes/BJVEP933 ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 删除远程仓库的hotfixes/BJVEP933分支
git push –tags ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 把所有tag推送到远程仓库
git fetch ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 获取所有远程分支(不更新本地分支,另需merge)
git fetch –prune ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 获取所有原创分支并清除服务器上已删掉的分支
git pull origin master ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 获取远程分支master并merge到当前分支
git mv README README2 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 重命名文件README为README2
git reset –hard HEAD ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 将当前版本重置为HEAD(通常用于merge失败回退)
git rebase
git branch -d hotfixes/BJVEP933 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 删除分支hotfixes/BJVEP933(本分支修改已合并到其他分支)
git branch -D hotfixes/BJVEP933 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 强制删除分支hotfixes/BJVEP933
git ls-files ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? ? ? ??? ? ? ??? ? ? ??? ? ? ? ???# 列出git index包含的文件
git show-branch ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ??? ? ? ??? ? ? ?? ? ? ? ? # 图示当前分支历史
git show-branch –all ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ??? ? ? ??? ? ? ? ??? ? # 图示所有分支历史
git whatchanged ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? ? ? ??? ? ? ??? # 显示提交历史对应的文件修改
git revert dfb02e6e4f2f7b573337763e5c0013802e392818 ? ? ? # 撤销提交dfb02e6e4f2f7b573337763e5c0013802e392818
git ls-tree HEAD ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ??? ? ? ? ?? ? ? ??# 内部命令:显示某个git对象
git rev-parse v2.0 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ??? ? ? ??? ? ? ??# 内部命令:显示某个ref对于的SHA1 HASH
git reflog ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ??? ? ? ??? ? ? ??? ? ? ? ? ?# 显示所有提交,包括孤立节点
git show HEAD@{5}
git show master@{yesterday} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? # 显示master分支昨天的状态
git log –pretty=format:’%h %s’ –graph ? ? ? ? ? ? ? ? ? ?# 图示提交日志
git show HEAD~3
git show -s –pretty=raw 2be7fcb476
git stash ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ??? ? ? ??? ? ? ??? ? ? ? ?? # 暂存当前修改,将所有至为HEAD状态
git stash list ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ??? ? ? ??? ? ? ? ? ? ? ?# 查看所有暂存
git stash show -p stash@{0} ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ?? ? ? # 参考第一次暂存
git stash apply stash@{0} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ?# 应用第一次暂存
git grep “delete from” ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ??? ? ? ? ? ? ? ? ?# 文件中搜索文本“delete from”
git grep -e ‘#define’ –and -e SORT_DIRENT
git gc
git fsck
———————————————————-风骚的分割线—————————————————————
学会这些命令,你就可以熟练的使用Git工具了,什么?想精通,那是不可能的。

基本上,Git就是以下面的命令顺序学习的。文中笔记是从廖雪峰老师的Git教程中总结出来的,方面查阅命令。详细原理请看Git教程

1、基础

  • git config --global user.name "Your Name"设置你的仓库用户名(用于标识提交者)
  • git config --global user.email "[email protected]"设置你的仓库邮箱(用于标识提交者)
  • git init?初始化一个git仓库
  • git add --all?添加所有更改的文件
  • git add filename1?当然可以指定添加filename1
  • git commit -m "commit message"?添加更改的信息,必须要有,不然报错,不建议不加。
  • git status?查看git当前状态
  • git diff filename1?查看filename1到底修改了哪些内容
  • git log?查看最近的提交日志
  • git log --pretty=oneline?单行显示提交日志
  • git reset --hard commitID?利用git log得到的commitID返回版本
  • git reset --hard HEAD^回到上一个版本
  • git reflog?查看命令的历史,可以找到git log看不到的commitID,因为git log只显示当前的提交日志,如果你提交了一次,退回版本后又后悔了,就能查看上次提交的commitID
  • git checkout -- filename1?利用版本库中的版本替换工作区中的文件。功能有2:
    • 撤销文件修改,分两种情况:
      • 撤销工作区中的修改(没有使用git add命令添加到暂存区)
      • 撤销暂存区中的修改(添加到了暂存区又做了修改)
    • 找回删除的文件
      • 工作区中文件误删了,可以通过此命令从版本库中找回
  • git reset HEAD filename1?撤销add,回到工作区
  • git rm filename1?删除文件
  • git remote add origin https://github.com/pengloo53/learngit.git?将本地库关联到github远程库上
  • git push -u origin master?第一次推送的时候要加上-u参数,可以将本地库的master分支与远程库的master分支关联起来;下次提交就不需要加-u了。
  • git clone https://github.com/pengloo53/learngit.git?克隆远程库到本地

2、分支管理

  • git checkout -b dev创建dev分支并切换到dev。相当于git branch devgit checkout dev两条命令。
  • git branch查看当前分支
  • git merge dev合并指定分支到当前分支,如,你现在master分支,那么执行命令就将dev分支合并到了master分支上。
  • git branch -d dev删除dev分支
  • git log --graph --pretty=oneline --abbrev-commit查看分支合并图
  • git merge --no-ff -m "merge with no-ff" dev禁用「Fast forward」,也就是保留分支的相关信息。
  • git stash?将工作区现场储藏起来,等以后恢复后继续工作。通常用于处理更为着急的任务时,例如:bug。
  • git stash list?查看保存的工作现场
  • git stash apply恢复工作现场
  • git stash drop?删除stash内容
  • git stash pop?恢复的同时直接删除stash内容
  • git stash apply stash@{0}?恢复指定的工作现场,当你保存了不只一份工作现场时。
  • git branch -D feature-vulcan?强行删除分支。用于不需要合并,就地删除的情况。
  • git remote?查看远程库的信息,一般返回origin。
  • git remote -v?查看远程库的详细信息。
  • git push origin master?将本地master分支推送到远程master分支。
    • master分支为主分支,因此要时刻与远程同步;
    • dev分支为开发分支,团队成员都需要在上面工作,所以也需要与远程同步;
    • bug分支只用于在本地修复bug,没有必要推送到远程;
    • feature新功能分支是否推送到远程,取决于你是否和其他人合作在上面开发。
  • git clone https://github.com/pengloo53/learngit.git?将远程库克隆到本地,默认只能看到master分支。
  • git checkout -b dev origin/dev?创建远程dev分支到本地
  • git pull?将远程分支的最新内容抓取下来。
  • git branch --set-upstream dev origin/dev将本地dev分支与远程dev分支之间建立链接。
多人协作工作模式
  1. 首先,可以试图用git push origin branch-name推送自己的修改;
  2. 如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并(如果git pull提示“no tracking information”,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream branch-name origin/branch-name);
  3. 如果合并有冲突,则解决冲突,并在本地提交;
  4. 没有冲突或者解决掉冲突后,再用git push origin branch-name推送就能成功!

3、标签管理

  • git tag v1.0?给当前分支打上标签
  • git tag?查看所有的标签,按时间顺序列出。
  • git log --pretty=oneline --abbrev-commit缩略commitID并单行显示提交信息
  • git tag v0.9 commitID通过上一条命令查看commitID,然后打上标签。用于忘记打标签的情况,因为标签其实就是只想某个commitID的指针,默认情况下,标签打在最新的提交上。
  • git show v0.9?查看标签信息。
  • git tag -a v0.1 -m "version 0.1 released" commitID创建带有说明的标签,-a指定标签名,-m指定说明文字。
  • git tag -d v0.1?删除标签v0.1
  • git push origin v1.0?推送标签1.0到远程
  • git push origin --tags?推送所有的标签到远程
  • git push origin :refs/tags/v0.9?删除远程标签,但是前提是要先在本地删除对应标签。

4、自定义Git

  • git config --global color.ui true?让Git显示颜色
  • .gitignore在这个文件里编辑你要忽略的文件,并提交到Git中,就可以忽略特殊文件的检查。如将*.db写入.gitignore文件中,将忽略所有db文件。可以参考github收集的所有.gitignore
  • git config --global alias.st status将status的别名设置成st,那么git st=git status
  • git config --global alias.unstage 'reset HEAD'?那么git reset HEAD filename=git unstage filename
  • git config --global alias.last 'log -1'?敲git last就显示最后一次提交了。

5、搭建Git服务器

  1. sudo apt-get install git?安装Git;
  2. sudo adduser git?添加Git用户;
  3. sudo git init --bare sample.git?初始化git仓库;
  4. sudo chown -R git:git sample.git修改仓库的所属用户为git;
  5. 将git用户的信息git:x:1001:1001:,,,:/home/git:/bin/bash改成git:x:1001:1001:,,,:/home/git:/bin/git-shell,为了禁用shell登录。
  6. git clone git@server?:/director/sample.git?克隆Git服务器上的仓库