关闭

Git workflow

标签: 版本控制
291人阅读 评论(0) 收藏 举报
分类:

Git是什么

Git能干什么?

Git用来做版本控制,这就好比当初在小日本的公司,每次修改文件,都需要在文件第一页新增修改履历,详细记录修改内容,如果多人同时操作,你可以想象下维护的成本有多高。基本每天就在整理这些破事。

所以说,版本控制是项目开发的重中之重。那么问题来了,版本控制哪家强?一句话,Git是目前世界上最先进的分布式版本控制系统(没有之一)。其实也可以把分布式三个字去掉。

集中式版本控制

集中式的版本控制工具以SVN为代表,他有一个中央服务器,控制着所有的版本管理,其它所有的终端,可以对这个中央库进行操作,中央库保证版本的唯一性。

这里写图片描述

这样有一个非常不好的地方,就是如果中央服务器被天灾军团攻陷了,那么整个版本就gg了。因为终端只能对一部分代码进行修改,获取各种信息,需要不断与中央服务器通信。

  1. 容灾性差
  2. 通信频繁

分布式版本控制

分布式版本控制的典型,就是Git,它跟集中式的最大区别就是它的终端可以获取到中央服务器的完整信息,就好像做了一个完整的镜像,这样,我可以在终端做各种操作、获取各种信息而不需要与服务器通信,同时,就算服务器被炸,各个终端还有完整的备份。分布式的思路,在版本控制上,具有得天独厚的优势,当然,这只是git优势的冰山一角。

Git安装与配置

安装

Git的安装就不解释了,相信程序猿都有能力独立解决这样一个问题,Linux和Mac下基本都已经自带Git了,window,建议不要用Git了,命令行实在是受不了。

配置

在终端输入:

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  ~  git <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--version</span>
git <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">version</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.8</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.4</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

来查看当前Git的版本,同时,输入:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  ~  git config --list --global
user<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.name</span>=xuyisheng
user<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.email</span>=xuyisheng@hujiang<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.com</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">push</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.default</span>=current</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

或者:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  ~  git config user<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.name</span>
xuyisheng</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

用来查看当前的配置信息,如果是新安装的,那么需要对global信息进行配置。配置的方法有两种,一个个变量配置,或者一起配置:

单独配置:

<code class="hljs brainfuck has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">➜</span>  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">~</span>  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">git</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">config</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">global</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">user</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">.</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">name</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">xys</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

一起配置:

<code class="hljs brainfuck has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">➜</span>  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">~</span>  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">git</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">config</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">global</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">add</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">user</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">.</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">name</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">xys</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

增加多个键值对

删除配置

<code class="hljs brainfuck has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">➜</span>  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">~</span>  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">git</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">config</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">global</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">unset</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">user</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">.</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">name</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">xys</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

配置别名

这个功能在shell中是很常用的。我们可以做一些别名来取代比较复杂的指令。 
比如:

<code class="hljs vbnet has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">git config --<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">global</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">alias</span>.st status</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

我们使用st来取代status。

附上一个比较吊的:

<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">git config --global alias.lg <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"log --color --graph --pretty=format:'<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%Cred</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%h</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%Creset</span> -<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%C</span>(yellow)<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%d</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%Creset</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%s</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%Cgreen</span>(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%cr</span>) <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%C</span>(bold blue)<<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%an</span>><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%Creset</span>' --abbrev-commit"</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

PS:用git log –graph命令可以看到分支合并图。

配置文件

git的配置文件其实我们是可以找到的,就在.git目录下:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  testGit git:(master) ls -a
.          ..         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.git</span>       README<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>
➜  testGit git:(master) cd <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.git</span>
➜  <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.git</span> git:(master) ls
HEAD        description index       logs        packed-refs
config      hooks       info        objects     refs
➜  <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.git</span> git:(master)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

我们打开config文件:

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[core]
    repositoryformatversion = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>
    filemode = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>
    bare = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">false</span>
    logallrefupdates = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>
    ignorecase = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>
    precomposeunicode = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">false</span>
[remote <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"origin"</span>]
    url = git<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@github</span>.<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">com:</span>xuyisheng/testGit.git
    fetch = +refs/heads/*<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:refs/remotes/origin/*</span>
[branch <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"master"</span>]
    remote = origin
    merge = refs/heads/master
[branch <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"dev"</span>]
    remote = origin
    merge = refs/heads/dev
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li></ul>

这里就是我们所有的配置了。

创建Git仓库

版本控制就是为了管理代码,代码就要放在仓库中。创建仓库有二种方法:

Git init

<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  MyWork  mkdir gitTest
➜  MyWork  <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cd</span> gitTest
➜  gitTest  git init
Initialized empty Git repository <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> /Users/hj/Downloads/MyWork/gitTest/.git/
➜  gitTest git:(master)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

创建一个目录,并cd到目录下,通过调用git init来将现有目录初始化为git仓库,或者直接在git init后面跟上目录名,同样也可以创建一个新的仓库。

git clone

git clone用于clone一个远程仓库到本地,这个我们后面再将。

创建好仓库后,目录下会生成一个.git的隐藏文件夹,这里就是所有的版本记录,默认不要对这个文件夹进行修改。

提交修改

add && commit

在仓库中,我们创建代码,并将其提交:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  gitTest git:(master) touch README<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>
➜  gitTest git:(master) ✗ open README<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>
➜  gitTest git:(master) ✗ git <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">add</span> README<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>
➜  gitTest git:(master) ✗ git commit -m <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"add readme"</span>
[master (root-commit) c19081b] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">add</span> readme
 <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> file changed, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> insertion(+)
 create mode <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100644</span> README<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

我们创建了一个README文件,并通过git add <文件名>的方式进行add操作,最后通过git commit操作进行提交,-m参数,指定了提交的说明。

这两个命令应该是最常使用的git命令。

查看修改

在版本控制中,非常核心的一点,就是需要指定,我们做了哪些修改,比如之前我们创建了一个README文件,并在里面写了一句话: 
this is readme。

下面我们修改这个文件: 
this is readme,modify。

接下来,我们使用git status命令来查看当前git仓库哪些内容被修改了:

<code class="hljs vala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  gitTest git:(master) git status
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># On branch master</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># Changes not staged for commit:</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#   (use "git add <file>..." to update what will be committed)</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#   (use "git checkout -- <file>..." to discard changes in working directory)</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#   modified:   README.txt</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#</span>
no changes added to commit (use <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"git add"</span> and/or <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"git commit -a"</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>

我们可以发现,git提示我们: 
modified: README.txt,更进一步,我们可以使用git diff命令来查看具体的修改:

<code class="hljs applescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">diff <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--git a/README.txt b/README.txt</span>
index <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2744</span>f40..f312f1a <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100644</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--- a/README.txt</span>
+++ b/README.txt
@@ -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> +<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> @@
-this <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is</span> readme
\ No newline <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">at</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">end</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">of</span> <span class="hljs-type" style="box-sizing: border-box;">file</span>
+this <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is</span> readme, modify
\ No newline <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">at</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">end</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">of</span> <span class="hljs-type" style="box-sizing: border-box;">file</span>
(END)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>

这样就查看了具体的修改。

下面我们继续add、commit:

<code class="hljs vala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  gitTest git:(master) ✗ git add README.txt
➜  gitTest git:(master) ✗ git status
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># On branch master</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># Changes to be committed:</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#   (use "git reset HEAD <file>..." to unstage)</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#   modified:   README.txt</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#</span>
➜  gitTest git:(master) ✗ git commit -m <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"modify readme"</span>
[master <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1629</span>cdc] modify readme
 <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> file changed, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> insertion(+), <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> deletion(-)
➜  gitTest git:(master) git status
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># On branch master</span>
nothing to commit, working directory clean</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul>

add和commit之后,我们都使用status来查看下状态,可以发现,在commit之后,git提示我们,工作区是干净的。

版本记录

在项目中,一个仓库通常可能有非常多次的add、commit过程,这些记录都会被记录下来,我们可以使用git log来查看这些记录:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">commit <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1629</span>cdcf2307bf26c0c5467e10035c2bd751e9d0
<span class="hljs-label" style="box-sizing: border-box;">Author:</span> xuyisheng <xuyisheng@hujiang<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.com</span>>
<span class="hljs-label" style="box-sizing: border-box;">Date:</span>   Sun Jun <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">28</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">14</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">45</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">14</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span> +<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0800</span>

    modify readme

commit c19081b6a48bcd6fb243560dafc7a35ae5e74765
<span class="hljs-label" style="box-sizing: border-box;">Author:</span> xuyisheng <xuyisheng@hujiang<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.com</span>>
<span class="hljs-label" style="box-sizing: border-box;">Date:</span>   Sun Jun <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">28</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">14</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">00</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span> +<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0800</span>

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">add</span> readme
(END)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li></ul>

每条记录都对应一个commit id,这是一个40个16进制的sha-1 hash code。用来唯一标识一个commit。

同时,我们也可以使用gitk命令来查看图形化的log记录:

这里写图片描述

git会自动将commit串成一个时间线。每个点,就代表一个commit。点击这些点,就可以看见相应的修改信息。

工作区与暂存区

Git通常是工作在三个区域上:

  1. 工作区
  2. 暂存区
  3. 历史区

其中工作区就是我们平时工作、修改代码的区域,而历史区,用来保存各个版本,而暂存区,则是Git的核心所在。

暂存区保存在我们前面讲的那个.git的隐藏文件夹中,是一个叫index的文件。

这里写图片描述

当我们向Git仓库提交代码的时候。add操作实际上是将修改记录到暂存区,我们来看gitk:

这里写图片描述

可以发现,我们在本地已经生成了一个记录,但是还没有commit,所以当前HEAD并没有指向我们的修改,修改还保存在暂存区。

当我们commit之后,再看gitk:

这里写图片描述

这时候,HEAD就已经移到了我们的修改上,也就是说,我们的提交生成了一个新的commit。git commit操作就是将暂存区的内容全部提交。如果内容不add到暂存区,那么commit就不会提交修改内容。

PS 这里需要说明一个概念,git管理的是修改,而不是文件,每个sha-1的值,也是根据内容计算出来的。

版本回退

如果这个世界一直只需要git add、commit,就不会有这么多的问题了,但是,愿望是美好的,现实是残酷的。如何处理版本的回退和修改,是使用git非常重要的一步。

checkout && reset

我们来考虑几种情况:

  1. 文件已经修改,但是还没有git add
  2. 文件已经add到暂存区,又作了修改
  3. 文件的修改已经add到了暂存区

分别执行以下操作:

<code class="hljs brainfuck has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">➜</span>  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">gitTest</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">git:(master)</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">✗</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">git</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">checkout</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">README</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">.</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">txt</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
  1. 修改被删除,完全还原到上次commit的状态,也就是服务器版本
  2. 最后的修改被删除,还原到上次add的状态,也就是修改前的暂存区状态

总的来说,就是还原到上次add、commit的状态。

而对于第三种情况,我们可以使用git reset:

<code class="hljs vala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  gitTest git:(master) ✗ git status
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># On branch master</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># Changes to be committed:</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#   (use "git reset HEAD <file>..." to unstage)</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#   modified:   README.txt</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#</span>
➜  gitTest git:(master) ✗ git reset<span class="hljs-constant" style="box-sizing: border-box;"> HEAD </span>README.txt
Unstaged changes after reset:
M   README.txt
➜  gitTest git:(master) ✗ git status
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># On branch master</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># Changes not staged for commit:</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#   (use "git add <file>..." to update what will be committed)</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#   (use "git checkout -- <file>..." to discard changes in working directory)</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#   modified:   README.txt</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#</span>
no changes added to commit (use <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"git add"</span> and/or <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"git commit -a"</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li></ul>

通过git reset HEAD README.txt,我们就把暂存区的文件清除了,这样,在本地就是add前的状态,通过checkout操作,就可以进行修改回退了。

PS : git checkout其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除

回退版本

当我们的仓库有了大量的提交的时候,我们可以通过git log来查看(可以指定 –pretty=oneline 来优化显示效果)。

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">e7ae095cafc8ddc5fda5a5d8b23d0bcaaf74ac39 modify again
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5427</span>c66703abfeaba3706f938317251ef2567e8b <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">delete</span> test.txt
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">08098</span>a21a918cfbd6377fc7a03a08cac0e6bcef6 <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">add</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">new</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">file</span>
b687b06fbb66da68bf8e0616c8049f194f03a062 e
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8038</span>c502e6f5cbf34c8096eb27feec682b75410b update
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">34</span>ad1c36b97f090fdf3191f51e149b404c86e72f modify again
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1629</span>cdcf2307bf26c0c5467e10035c2bd751e9d0 modify readme
c19081b6a48bcd6fb243560dafc7a35ae5e74765 <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">add</span> readme</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>

那么我们如果回退到指定版本呢?在Git中,用HEAD表示当前版本,上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本就不要这样写了,写成HEAD~100即可。

下面我们就可以回退了:

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  testGit git:(master) git reset <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--hard HEAD^</span>
HEAD is now <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">at</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5427</span>c66 <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">delete</span> test.txt</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

要回退到哪个版本,只要HEAD写对就OK了。你可以写commit id,也可以HEAD^,也可以HEAD^^。

前进版本

有时候,如果我们回退到了旧的版本,但是却后悔了,想回到后面某个新的版本,但这个时候,我的commit id已经忘了,怎么办呢? 
没事,通过这个指令:

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5427</span>c66 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>}: reset: moving <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span> HEAD^
e7ae095 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>}: checkout: moving <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> dev <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span> master
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7986</span>a59 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>}: checkout: moving <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> master <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span> dev
e7ae095 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>}: checkout: moving <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> dev <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span> master
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7986</span>a59 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>}: checkout: moving <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7986</span>a59bd8683acb560e56ff222324cd49edb5e5 <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span> dev
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7986</span>a59 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>}: checkout: moving <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> master <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span> origin/dev
e7ae095 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>}: clone: <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> git@github.com:xuyisheng/testGit.git</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

这样我们就可以找到commit id用来还原了。

git reflog记录的就是你的操作历史。

文件操作

git rm

如果我们要删除git仓库中的文件,我们要怎么做呢?

我们创建一个新的文件并提交,再删除这个文件:

<code class="hljs vala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  gitTest git:(master) rm test.txt
➜  gitTest git:(master) ✗ git status
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># On branch master</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># Changes not staged for commit:</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#   (use "git add/rm <file>..." to update what will be committed)</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#   (use "git checkout -- <file>..." to discard changes in working directory)</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#   deleted:    test.txt</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#</span>
no changes added to commit (use <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"git add"</span> and/or <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"git commit -a"</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>

git提示我们delete一个文件,这时候你可以选择:

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">gitTest</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">git</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:(master)</span> ✗ <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">git</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">checkout</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">test</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.txt</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

这样就撤销了删除操作,或者使用:

<code class="hljs vala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  gitTest git:(master) ✗ git rm test.txt
rm <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'test.txt'</span>
➜  gitTest git:(master) ✗ git status
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># On branch master</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># Changes to be committed:</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#   (use "git reset HEAD <file>..." to unstage)</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#   deleted:    test.txt</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#</span>
➜  gitTest git:(master) ✗ git commit -m <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"delete test.txt"</span>
[master <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5427</span>c66] delete test.txt
 <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> file changed, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> insertions(+), <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> deletions(-)
 delete mode <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100644</span> test.txt</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>

通过git rm指令,删除文件,最后提交修改,就真实的删除了文件。

文件暂存

这里的暂存不是前面说的暂存区,而是只一次备份与恢复操作。举个例子,当前我们在dev分支上进行一个新功能的开发,但是开发到一半,测试提了一个issue,这时候,我们需要创建一个issue分支来修改这个bug,但是,当前dev分支是不干净的,新功能开发到一半,直接从dev上拉分支,代码是不完善的,可能会编译不过。 
so,你可以使用:

<code class="hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">git stash</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

指令来将当前修改暂存,这样就可以切换到其他分支或者就在当前干净的分支上checkout了。 
比如你checkout了一个issue分支,修改了bug,使用git merge合并到了master分支,删除issue分支,切换到dev分支,想继续之前的新功能开发。 
这时候,就需要恢复现场了:

首先,通过:

<code class="hljs php has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">git stash <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">list</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

我们可以查看当前暂存的内容记录。

然后,通过git stash apply或者git stash pop来进行恢复,它们的区别是,前者不会删除记录(当然你可以使用git stash drop来删除),而后者会。

远程仓库

既然git是分布式的仓库管理,那么我们肯定是需要多台服务器进行各种操作的,一般在开发中,我们会用一台电脑做中央服务器,各个终端从中央服务器拉取代码,提交修改。

那么我们如何去搭建一个git远程服务器呢,答案是不要搭建,个人开发者可以通过github来获取免费的远程git服务器,或者是国内的开源中国之类的,同样也提供免费的git服务器,而对于企业用户,可以通过gitlab来获取git远程服务器。

身份认证

当本地git仓库与git远程仓库通信的时候,需要进行SSH身份认证。

打开根目录下的.ssh目录:

<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  ~  cd .ssh
➜  .ssh  ll
total 40
-<span class="ruby" style="box-sizing: border-box;">rw-------  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> xys  staff   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.7</span>K  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">53</span> github_rsa
</span>-<span class="ruby" style="box-sizing: border-box;">rw-r--r--  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> xys  staff   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">402</span>B  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">53</span> github_rsa.pub
</span>-<span class="ruby" style="box-sizing: border-box;">rw-------  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> xys  staff   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.6</span>K  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span> 09<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">38</span> id_rsa
</span>-<span class="ruby" style="box-sizing: border-box;">rw-r--r--  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> xys  staff   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">409</span>B  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span> 09<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">42</span> id_rsa.pub
</span>-<span class="ruby" style="box-sizing: border-box;">rw-r--r--  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> xys  staff   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2.0</span>K  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">13</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">34</span> known_hosts</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>

如果没有id_rsa和id_rsa.pub这两个文件,就通过如下的命令生成:

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">ssh<span class="hljs-attribute" style="box-sizing: border-box;">-keygen</span> <span class="hljs-attribute" style="box-sizing: border-box;">-t</span> rsa <span class="hljs-attribute" style="box-sizing: border-box;">-C</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"youremail@example.com"</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

id_rsa和id_rsa.pub这两个文件,就是SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,用在github上表明身份。

在github的ssh key中增加刚刚生成的key:

这里写图片描述

同步协作

现在你在本地建立了git仓库,想与远程git仓库同步,这样github的远程仓库可以作为你本地的备份,也可以让其他人进行协同工作。

我们先在github上创建一个repo(仓库):

这里写图片描述

创建之后,github给我们提示:

这里写图片描述

github告诉了我们如何在本地创建一个新的repo或者将既存的repo提交到远程git。

由于我们已经有了本地的git,所以,安装提示:

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  gitTest git:(master) git remote <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">add</span> origin git@github.com:xuyisheng/testGit.git
➜  gitTest git:(master) git push -u origin master
Counting objects: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">18</span>, done.
Delta compression <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> up <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span> threads.
Compressing objects: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>% (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>), done.
Writing objects: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>% (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">18</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">18</span>), <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.39</span> KiB | <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">bytes</span>/s, done.
Total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">18</span> (delta <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>), reused <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> (delta <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)
To git@github.com:xuyisheng/testGit.git
 * [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">new</span> branch]      master -> master
Branch master <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">set</span> up <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span> track remote branch master <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> origin.</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>

现在我们再看github上的仓库:

这里写图片描述

README已经提交上去了。

git remote add origin git@github.com:xuyisheng/testGit.git这条指令中的origin,就是远程仓库的名字,你也可以叫别的,但是默认远程仓库都叫做origin,便于区分。

PS: 这里还需要注意下的是git push的-u参数,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来。不过后面的push就不需要这个参数了。

之后我们再做修改:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  gitTest git:(master) ✗ git <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">add</span> README<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>
➜  gitTest git:(master) ✗ git commit -m <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"modify again"</span>
[master e7ae095] modify again
 <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> file changed, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> insertions(+), <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> deletion(-)
➜  gitTest git:(master) git <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">push</span>
Counting objects: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>, done.
Delta compression using up to <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span> threads.
Compressing objects: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>% (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>), done.
Writing objects: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>% (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>), <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">285</span> bytes | <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> bytes/s, done.
Total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> (delta <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>), reused <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> (delta <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)
To git@github<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.com</span>:xuyisheng/testGit<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.git</span>
   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5427</span>c66.<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.e</span>7ae095  master -> master</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li></ul>

可以直接使用git push或者git push origin master来指定仓库和分支名。

clone远程仓库

记得我们之前说的,创建本地仓库的几种方式,其中有一种是clone:

我们可以通过git clone指令来clone一个远程仓库:

这里写图片描述

下面就显示了远程仓库的地址,一般我们使用SSH的方式,当然,也可以使用其他方式,然并卵。

PS: 使用https除了速度慢以外,还有个最大的麻烦是每次推送都必须输入口令,但是在某些只开放http端口的公司内部就无法使用ssh协议而只能用https。

直接使用:

<code class="hljs r has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  MyWork  git clone git@github.com:xuyisheng/testGit.git
Cloning into <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'testGit'</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span>
remote: Counting objects: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21</span>, done.
remote: Compressing objects: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>% (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span>), done.
remote: Total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21</span> (delta <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>), reused <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21</span> (delta <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>), pack-reused <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>
Receiving objects: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>% (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21</span>), done.
Resolving deltas: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>% (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>), done.
Checking connectivity... done</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>

分支管理

个人认为,创建分支是git最大的魅力。 
git中的分支就好像现在的平行宇宙,不同的分支互不干扰,相互独立,你就像一个上帝一样,可以随时对任意一个分支进行操作,可以今天去这个branch玩,明天去另一个branch,玩腻了,再把两个分支合并,一起玩。

举个比较恰当的例子,我现在要开发一个新功能,需要3个月的时间,但是我不能每天都把未完成的代码提交到大家都在用的分支上,这样人家拉取了我的代码就无法正常工作了,但是我又不能新建一个仓库,这也太浪费了,所以我可以新建一个分支,在这个分支上开发我的功能,同时能够备份我的代码,当开发完毕后,直接合并分支,整个新功能就一下子加入了大家的分支。

创建分支

你的每次提交,Git都把它们串成一条时间线,这条时间线就是一个分支。不创建分支时,只有一条时间线,在Git里,这个分支叫主分支,即master分支。HEAD严格来说不是指向提交,而是指向master,master才是指向提交的,所以,HEAD指向的就是当前分支。

这里写图片描述

这里的时间线就看的非常清楚了。

我们通过如下指令来创建分支:

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  gitTest git:(master) git checkout -b dev
Switched <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span> <span class="hljs-operator" style="box-sizing: border-box;">a</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">new</span> branch <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'dev'</span>
➜  gitTest git:(dev)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

-b参数代表创建并切换到该分支,相当于:

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>git branch dev
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>git checkout dev
<span class="hljs-constant" style="box-sizing: border-box;">Switched</span> to branch <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'dev'</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

不加-b参数就是直接切换到已知分支了。

查看分支

通过git branch指令可以列出当前所有分支:

<code class="hljs markdown has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  gitTest git:(dev) git branch
<span class="hljs-bullet" style="box-sizing: border-box;">* </span>dev
  master</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

当前分支上会多一个*

合并分支

切换到dev分支后,我们进行修改,add并commit:

此时我们再切换到master分支,再查看当前修改,你会发现,dev分支上做的修改这里都没有生效。必须的,不然平行宇宙就不平行了。我们通过如下指令来进行分支的合并:

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  gitTest git:(master) git <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">merge</span> dev
Updating e7ae095.<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.7986</span>a59
Fast-forward
 README.txt | <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> ++-
 <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">file</span> changed, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> insertions(+), <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> deletion(-)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

这样再查看master分支下的文件,dev上的修改就有了。

删除分支

使用完毕后,我们不再需要这个分支了,所以,放心的删除吧:

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">gitTest</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">git</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:(master)</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">git</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">branch</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">-d</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">dev</span>
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">Deleted</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">branch</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">dev</span> (<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">was</span> 7986<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">a59</span>).
➜  <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">gitTest</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">git</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:(master)</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">git</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">branch</span>
* <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">master</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

PS : 当分支还没有被合并的时候,如果删除分支,git会提示: 
error: The branch ‘feature-vulcan’ is not fully merged. 
If you are sure you want to delete it, run ‘git branch -D dev. 
也就是提示我们使用-D参数来进行强行删除。

看完分支的操作,有人可能会问,创建这么多分支,git会不会很累,当然不会,git并不是创建整个文件的备份到各个分支,而是创建一个指针指向不同的分支而已。切换分支,创建分支,都只是改变指针指向的位置。

分支是非常好的团体协作方式,一个项目中通常会有一个master分支来进行发布管理,一个dev分支来进行开发,而不同的开发者checkout出dev分支来进行开发,merge自己的分支到dev,当有issue或者新需求的时候,checkout分支进行修改,可以保证主分支的安全,即使修改取消,也不会影响主分支。

查看远程分支

当你从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且,远程仓库的默认名称是origin。 
通过如下指令,我们可以查看远程分支:

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">gitTest</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">git</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:(master)</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">git</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">remote</span>
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">origin</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

或者:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  gitTest git:(master) git remote -v
origin  git@github<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.com</span>:xuyisheng/testGit<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.git</span> (fetch)
origin  git@github<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.com</span>:xuyisheng/testGit<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.git</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">push</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

来显示更详细的信息。

推送分支

要把本地创建的分支同步到远程仓库上,我们可以使用:

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  gitTest git:(master) git checkout -b dev
Switched <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span> <span class="hljs-operator" style="box-sizing: border-box;">a</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">new</span> branch <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'dev'</span>
➜  gitTest git:(dev) git push origin dev
Everything up-<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span>-<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">date</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

这样就把一个dev分支推送到了远程仓库origin中。

抓取远程分支

当我们将远程仓库clone到本地后即使远程仓库有多个分支,但实际上,本地只有一个分支——master。

<code class="hljs r has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  MyWork  git clone git@github.com:xuyisheng/testGit.git
Cloning into <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'testGit'</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span>
remote: Counting objects: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">24</span>, done.
remote: Compressing objects: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>% (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">11</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">11</span>), done.
remote: Total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">24</span> (delta <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>), reused <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span> (delta <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>), pack-reused <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>
Receiving objects: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>% (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">24</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">24</span>), done.
Resolving deltas: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>% (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>), done.
Checking connectivity... done
➜  MyWork  cd testGit
➜  testGit git:(master) git branch
* master</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>

现在,我要在dev分支上开发,就必须创建远程origin的dev分支到本地,用这个命令创建本地dev分支:

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  testGit git:(master) git checkout -b dev origin/dev
Branch dev <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">set</span> up <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span> track remote branch dev <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> origin.
Switched <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span> <span class="hljs-operator" style="box-sizing: border-box;">a</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">new</span> branch <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'dev'</span>
➜  testGit git:(dev)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

这样就把本地创建的dev分支与远程的dev分支关联了。 
后面你可以使用git push origin dev继续提交代码到远程的dev分支。

这种情况下,如果其他人也提交了到dev分支,那么你的提交就会与他的提交冲突,因此,你需要使用git pull先将远程修改拉取下来,git会自动帮你在本地进行merge,如果没有冲突,这时候再提交,就OK了,如果有冲突,那么必须手动解除冲突再提交。

Tag

Tag的概念非常类似于branch。但是branch是可以不断改变、merge的,Tag不行,Tag可以认为是一个快照,用于记录某个commit点的历史快照。

创建标签

非常简单:

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">testGit</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">git</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:(master)</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">git</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">tag</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">version1</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

默认Tag会打在最后的提交上。但是你也可以通过commit id来指定要打的地方。

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">testGit</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">git</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:(master)</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">git</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">tag</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">version0</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">b687b06fbb66da68bf8e0616c8049f194f03a062</span>
➜  <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">testGit</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">git</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:(master)</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">git</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">tag</span>
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">version0</span>
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">version1</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

PS : 实际上commit id不需要写很长,通过前6、7位,git就可以查找到相应的id了。

还可以创建带有说明的标签,用-a指定标签名,-m指定说明文字:

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">git <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">tag</span> <span class="hljs-attribute" style="box-sizing: border-box;">-a</span> v1 <span class="hljs-attribute" style="box-sizing: border-box;">-m</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"version1"</span> b687b06fbb66da68bf8e0616c8049f194f03a062</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

通过如下指令来查看详细信息:

<code class="hljs xml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">git show <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">tagname</span>></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

查看标签

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">testGit</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">git</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:(master)</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">git</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">tag</span>
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">version1</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

删除标签

-d参数就可以了,与branch类似。

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  testGit git:(master) git <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">tag</span>
version0
version1
➜  testGit git:(master) git <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">tag</span> <span class="hljs-attribute" style="box-sizing: border-box;">-d</span> version0
Deleted <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">tag</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'version0'</span> (was b687b06)
➜  testGit git:(master) git <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">tag</span>
version1</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

推送到远程

将本地tag推送到远程参考上:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  testGit git:(master) git <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">push</span> origin version0
Total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> (delta <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>), reused <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> (delta <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)
To git@github<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.com</span>:xuyisheng/testGit<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.git</span>
 * [new tag]         version0 -> version0</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

或者:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  testGit git:(master) git <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">push</span> origin --tags
Total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> (delta <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>), reused <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> (delta <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)
To git@github<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.com</span>:xuyisheng/testGit<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.git</span>
 * [new tag]         version1 -> version1</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

来push所有的tag。

删除远程Tag

当Tag已经push到远程仓库后,我们要删除这个Tag,首先需要先删除本地Tag:

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  testGit git:(master) git <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">tag</span> <span class="hljs-attribute" style="box-sizing: border-box;">-d</span> version0
Deleted <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">tag</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'version0'</span> (was b687b06)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

再push到远程,带指令有所不同:

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">➜  testGit <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">git:</span>(master) git push origin <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:refs/tags/version0</span>
<span class="hljs-constant" style="box-sizing: border-box;">To</span> git<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@github</span>.<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">com:</span>xuyisheng/testGit.git
 - [deleted]         version<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

以上。

学完这些,对付基本的Git使用,就没什么问题了,后面会再出一篇Git workflow plus,介绍一下Git的高级 操作。

来源:http://blog.csdn.net/eclipsexys/article/details/46670699

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:104206次
    • 积分:1432
    • 等级:
    • 排名:千里之外
    • 原创:17篇
    • 转载:154篇
    • 译文:0篇
    • 评论:8条
    文章分类
    最新评论