---
title: "Git 常用命令整理（转）"
author: "songzhan"
date: "2015-04-14T10:50:02Z"
tags: []
categories: ["研发管理"]
slug: "git"
draft: false
lastmod: "2015-04-14T10:50:02Z"
id: 2202197
url: "https://songzhan.iteye.com/blog/2202197"
---

## 文章出处：http://justcoding.iteye.com/blog/1830388

## 初始化配置

C代码  [![收藏代码](http://justcoding.iteye.com/images/icon_star.png)]()

01. #配置使用git仓库的人员姓名
02. git config --global user.name "Your Name Comes Here"
03. #配置使用git仓库的人员email
04. git config --global user.email you@yourdomain.example.com
05. #配置到缓存 默认15分钟
06. git config --global credential.helper cache
07. #修改缓存时间
08. git config --global credential.helper 'cache --timeout=3600'
09. git config --global color.ui true
10. git config --global alias.co checkout
11. git config --global alias.ci commit
12. git config --global alias.st status
13. git config --global alias.br branch
14. git config --global core.editor "mate -w"    # 设置Editor使用textmate
15. git config -1 #列举所有配置
16. #用户的git配置文件~/.gitconfig

 

## **查看、添加、提交、删除、找回，重置修改文件**

C代码  [![收藏代码](http://justcoding.iteye.com/images/icon_star.png)]()

01. git help &lt;command&gt;  # 显示command的help
02. git show            # 显示某次提交的内容
03. git show $id
04. git co  -- &lt;file&gt;   # 抛弃工作区修改
05. git co  .           # 抛弃工作区修改
06. git add &lt;file&gt;      # 将工作文件修改提交到本地暂存区
07. git add .           # 将所有修改过的工作文件提交暂存区
08. git rm &lt;file&gt;       # 从版本库中删除文件
09. git rm &lt;file&gt; --cached  # 从版本库中删除文件，但不删除文件
10. git reset &lt;file&gt;    # 从暂存区恢复到工作文件
11. git reset -- .      # 从暂存区恢复到工作文件
12. git reset --hard    # 恢复最近一次提交过的状态，即放弃上次提交后的所有本次修改
13. git ci &lt;file&gt;
14. git ci .
15. git ci -a           # 将git add, git rm和git ci等操作都合并在一起做
16. git ci -am "some comments"
17. git ci --amend      # 修改最后一次提交记录
18. git revert &lt;$id&gt;    # 恢复某次提交的状态，恢复动作本身也创建了一次提交对象
19. git revert HEAD     # 恢复最后一次提交的状态

 

## **查看文件diff**

C代码  [![收藏代码](http://justcoding.iteye.com/images/icon_star.png)]()

1. git diff &lt;file&gt;     # 比较当前文件和暂存区文件差异
2. git diff
3. git diff &lt;$id1&gt; &lt;$id2&gt;   # 比较两次提交之间的差异
4. git diff &lt;branch1&gt;..&lt;branch2&gt; # 在两个分支之间比较
5. git diff --staged   # 比较暂存区和版本库差异
6. git diff --cached   # 比较暂存区和版本库差异
7. git diff --stat     # 仅仅比较统计信息

 

## **查看提交记录**

C代码  [![收藏代码](http://justcoding.iteye.com/images/icon_star.png)]()

1. git log
2. git log &lt;file&gt;      # 查看该文件每次提交记录
3. git log -p &lt;file&gt;   # 查看每次详细修改内容的diff
4. git log -p -2       # 查看最近两次详细修改内容的diff
5. git log --stat      #查看提交统计信息

 

 **tig**

Mac上可以使用tig代替diff和log，`brew install tig`

 

## 取得Git仓库

C代码  [![收藏代码](http://justcoding.iteye.com/images/icon_star.png)]()

1. #初始化一个版本仓库
2. git init
3. #Clone远程版本库
4. git clone git@xbc.me:wordpress.git
5. #添加远程版本库origin，语法为 git remote add \[shortname] \[url]
6. git remote add origin git@xbc.me:wordpress.git
7. #查看远程仓库
8. git remote -v

 

## 提交你的修改

C代码  [![收藏代码](http://justcoding.iteye.com/images/icon_star.png)]()

01. #添加当前修改的文件到暂存区
02. git add .
03. #如果你自动追踪文件，包括你已经手动删除的，状态为Deleted的文件
04. git add -u
05. #提交你的修改
06. git commit –m "你的注释"
07. #推送你的更新到远程服务器,语法为 git push \[远程名] \[本地分支]:\[远程分支]
08. git push origin master
09. #查看文件状态
10. git status
11. #跟踪新文件
12. git add readme.txt
13. #从当前跟踪列表移除文件，并完全删除
14. git rm readme.txt
15. #仅在暂存区删除，保留文件在当前目录，不再跟踪
16. git rm –cached readme.txt
17. #重命名文件
18. git mv reademe.txt readme
19. #查看提交的历史记录
20. git log
21. #修改最后一次提交注释的，利用–amend参数
22. git commit --amend
23. #忘记提交某些修改，下面的三条命令只会得到一个提交。
24. git commit –m &quot;add readme.txt&quot;
25. git add readme\_forgotten
26. git commit –amend
27. #假设你已经使用git add .，将修改过的文件a、b加到暂存区
28. #现在你只想提交a文件，不想提交b文件，应该这样
29. git reset HEAD b
30. #取消对文件的修改
31. git checkout –- readme.txt

 

## **查看、切换、创建和删除分支**

C代码  [![收藏代码](http://justcoding.iteye.com/images/icon_star.png)]()

01. git br -r           # 查看远程分支
02. git br &lt;new\_branch&gt; # 创建新的分支
03. git br -v           # 查看各个分支最后提交信息
04. git br --merged     # 查看已经被合并到当前分支的分支
05. git br --no-merged  # 查看尚未被合并到当前分支的分支
06. git co &lt;branch&gt;     # 切换到某个分支
07. git co -b &lt;new\_branch&gt; # 创建新的分支，并且切换过去
08. git co -b &lt;new\_branch&gt; &lt;branch&gt;  # 基于branch创建新的new\_branch
09. git co $id          # 把某次历史提交记录checkout出来，但无分支信息，切换到其他分支会自动删除
10. git co $id -b &lt;new\_branch&gt;  # 把某次历史提交记录checkout出来，创建成一个分支
11. git br -d &lt;branch&gt;  # 删除某个分支
12. git br -D &lt;branch&gt;  # 强制删除某个分支 (未被合并的分支被删除的时候需要强制)

 

## **分支合并和rebase**

C代码  [![收藏代码](http://justcoding.iteye.com/images/icon_star.png)]()

1. git merge &lt;branch&gt;               # 将branch分支合并到当前分支
2. git merge origin/master --no-ff  # 不要Fast-Foward合并，这样可以生成merge提交
3. git rebase master &lt;branch&gt;       # 将master rebase到branch，相当于：
4. git co &lt;branch&gt; && git rebase master && git co master && git merge &lt;branch&gt;

 

## **Git补丁管理(方便在多台机器上开发同步时用)**

C代码  [![收藏代码](http://justcoding.iteye.com/images/icon_star.png)]()

1. git diff &gt; ../sync.patch         # 生成补丁
2. git apply ../sync.patch          # 打补丁
3. git apply --check ../sync.patch  #测试补丁能否成功

 

## **Git暂存管理**

C代码  [![收藏代码](http://justcoding.iteye.com/images/icon_star.png)]()

1. git stash                        # 暂存
2. git stash list                   # 列所有stash
3. git stash apply                  # 恢复暂存的内容
4. git stash drop                   # 删除暂存区

 

## **Git远程分支管理**

C代码  [![收藏代码](http://justcoding.iteye.com/images/icon_star.png)]()

01. git pull                         # 抓取远程仓库所有分支更新并合并到本地
02. git pull --no-ff                 # 抓取远程仓库所有分支更新并合并到本地，不要快进合并
03. git fetch origin                 # 抓取远程仓库更新
04. git merge origin/master          # 将远程主分支合并到本地当前分支
05. git co --track origin/branch     # 跟踪某个远程分支创建相应的本地分支
06. git co -b &lt;local\_branch&gt; origin/&lt;remote\_branch&gt;  # 基于远程分支创建本地分支，功能同上
07. git push                         # push所有分支
08. git push origin master           # 将本地主分支推到远程主分支
09. git push -u origin master        # 将本地主分支推到远程(如无远程主分支则创建，用于初始化远程仓库)
10. git push origin &lt;local\_branch&gt;   # 创建远程分支， origin是远程仓库名
11. git push origin &lt;local\_branch&gt;:&lt;remote\_branch&gt;  # 创建远程分支
12. git push origin :&lt;remote\_branch&gt;  #先删除本地分支(git br -d &lt;branch&gt;)，然后再push删除远程分支

 

## 基本的分支管理

C代码  [![收藏代码](http://justcoding.iteye.com/images/icon_star.png)]()

01. #创建一个分支
02. git branch iss53
03. #切换工作目录到iss53
04. git chekcout iss53
05. #将上面的命令合在一起，创建iss53分支并切换到iss53
06. git chekcout –b iss53
07. #合并iss53分支，当前工作目录为master
08. git merge iss53
09. #合并完成后，没有出现冲突，删除iss53分支
10. git branch –d iss53
11. #拉去远程仓库的数据，语法为 git fetch \[remote-name]
12. git fetch
13. #fetch 会拉去最新的远程仓库数据，但不会自动到当前目录下，要自动合并
14. git pull
15. #查看远程仓库的信息
16. git remote show origin
17. #建立本地的dev分支追踪远程仓库的develop分支
18. git checkout –b dev origin/develop

 

## **Git远程仓库管理**

C代码  [![收藏代码](http://justcoding.iteye.com/images/icon_star.png)]()

1. git remote -v                    # 查看远程服务器地址和仓库名称
2. git remote show origin           # 查看远程服务器仓库状态
3. git remote add origin git@ github:robbin/robbin\_site.git         # 添加远程仓库地址
4. git remote set-url origin git@ github.com:robbin/robbin\_site.git # 设置远程仓库地址(用于修改远程仓库地址)
5. git remote rm &lt;repository&gt;       # 删除远程仓库

 

## **创建远程仓库**

C代码  [![收藏代码](http://justcoding.iteye.com/images/icon_star.png)]()

1. git clone --bare robbin\_site robbin\_site.git  # 用带版本的项目创建纯版本仓库
2. scp -r my\_project.git git@ git.csdn.net:~      # 将纯仓库上传到服务器上
3. mkdir robbin\_site.git && cd robbin\_site.git && git --bare init # 在服务器创建纯仓库
4. git remote add origin git@ github.com:robbin/robbin\_site.git    # 设置远程仓库地址
5. git push -u origin master                                      # 客户端首次提交
6. git push -u origin develop  # 首次将本地develop分支提交到远程develop分支，并且track
7. git remote set-head origin master   # 设置远程仓库的HEAD指向master分支

 

也可以命令设置跟踪远程库和本地库

C代码  [![收藏代码](http://justcoding.iteye.com/images/icon_star.png)]()

1. git branch --set-upstream master origin/master
2. git branch --set-upstream develop origin/develop

 

参考资料

[远程仓库的使用](http://progit.org/book/zh/ch2-5.html "远程仓库的使用")

[何谓分支](http://progit.org/book/zh/ch3-1.html "何谓分支")

[基本的分支与合并](http://progit.org/book/zh/ch3-2.html "基本的分支与合并")

[分支的管理](http://progit.org/book/zh/ch3-3.html "分支的管理")

[分支式工作流程](http://progit.org/book/zh/ch3-4.html "分支式工作流程")

[远程分支](http://progit.org/book/zh/ch3-5.html "远程分支")

[衍合](http://progit.org/book/zh/ch3-6.html "衍合")

 

参考1： http://www.xbc.me/git-commands/

参考2： http://neverno.me/hello-world/git-commands-github.html

 

在线中文图书： 1. [**Git Magic**](http://www-cs-students.stanford.edu/%7Eblynn/gitmagic/intl/zh_cn/index.html)  2. [**Pro Git**](http://git-scm.com/book/zh/)