获得一个Git管理的工作区
使用Git开始工作,首先需要一个Git管理的工作区,这个工作去可以是自己init创建的,也可以是从远程仓库clone下来的。
自己初始化一个仓库
<code class="language-bash 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;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 新建一个目录作为工作目录</span> $ mkdir git_demo $ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cd</span> git_demo <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 在本地初始化git仓库</span> $ git init</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><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>
这时,我们已经创建了一个本地仓库,但是,一般我们和其他人共同开发一个项目,则需要添加一个远程仓库。现在假设我已经才github上面建立了一个叫做git_demo的空仓库,现在需要将其添加到本地仓库。
<code class="language-bash 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;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 添加一个叫origin的远程仓库</span> $ git remote add origin git@github.com:JavyZheng/git_demo.git <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 添加个README吧</span> $ vim README.md $ git add README.md $ git commit -m <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"first commit with README"</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 推送到远程仓库</span> $ git push -u origin 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></ul><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仓库。
从远程仓库获得工作目录
大多数时候,我们没有机会从头init的仓库,而是远程仓库已经存在,我们要参与到项目中,这时只需要将远程仓库clone下来就好。
<code class="language-bash 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;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 把刚刚推送上去的仓库clone下来</span> $ git clone git@github.com:JavyZheng/git_demo.git $ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cd</span> git_demo $ git status <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 默认远程仓库名为origin</span> On branch master Your branch is up-to-date with <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'origin/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></ul><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>
添加,修改,推送
不管是自己重新创建还是从远程仓库clone,我们现在得到了一个可以开展工作的工作区,这个工作区被git仓库所管理。
进行一些修改后,可以通过add, commit, push来推送到远程仓库
<code class="language-bash 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;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 添加所有修改</span> $ git add . <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 提交修改</span> $ git commit -m <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"add some files"</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 推送到远程仓库</span> $ git push </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></ul><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></ul>
分支
通常情况下,当我们需要添加一个新功能的时候,是不能在主分支上直接修改的,这时,需要创建一个新的分支,等功能开发并测试完成之后,再合并到主分支。
假设我们需要新建一个临时分支iss1来处理一个问题。
<code class="language-bash 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;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 新建一个iss1分支</span> $ git branch iss1 <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 切换到iss1分支</span> $ git checkout iss1 Switched to branch <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'iss1'</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 查看分支,当前已经在iss1分支上面</span> $ git branch * iss1 master <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 在当前分支上进行一些修改</span> $ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"file3"</span> >> file3 <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 添加并提交修改到本地</span> $ git add file3 $ git commit -m <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"add file3"</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 推送到远程,因为现在远程还没有iss1分支,所以需要set-upstream</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 这样,在远程仓库就有了iss1分支,之后可以直接push</span> $ git push --set-upstream origin iss1 <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## iss1解决后,把修改合并会master,并删除iss1分支</span> $ git checkout master $ git merge iss1 $ git branch <span class="hljs-operator" style="box-sizing: border-box;">-d</span> iss1 $ git push <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 删除远程分支</span> $ git push origin :iss1 To git@github.com:JavyZheng/git_demo.git - [deleted] iss1 </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><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li></ul><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><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li></ul>
拉取别人的提交
在我们完成了某项功能的修改,需要push的远程的时候,协作者可能已经提交了他们的修改,这时,我们需要先把最新的提交拉取下来,加入我们的修改,再重新提交上去。
<code class="language-bash 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 push <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## push被驳回了,因为有其他人已经提交了更新</span> ! [rejected] master -> master (fetch first) error: failed to push some refs to <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'git@github.com:JavyZheng/git_demo.git'</span> hint: Updates were rejected because the remote contains work that you <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">do</span> hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'git pull ...'</span>) before pushing again. hint: See the <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Note about fast-forwards'</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'git push --help'</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> details. <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 拉取远程提交内容并合并到当前工作区</span> $ git pull <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 重新push到远程</span> $ git push</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><div class="save_code tracking-ad" data-mod="popu_249" style="box-sizing: border-box; position: absolute; height: 60px; right: 30px; top: 5px; color: rgb(255, 255, 255); cursor: pointer; z-index: 2;"><a target=_blank target="_blank" style="color: rgb(202, 0, 0); box-sizing: border-box;"><img src="http://static.blog.csdn.net/images/save_snippets.png" style="border: none; box-sizing: border-box; max-width: 100%;" alt="" /></a></div><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>
处理冲突
当拉取协作者的提交时,很可能不同开发者修改了同一个文件的同一部分,这时候,就会出现冲突,我们需要手动解决这些冲突,再重新提交上去。
<code class="language-bash 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 pull remote: Counting objects: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</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;">2</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>), done. remote: Total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</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;">5</span> (delta <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>), pack-reused <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> Unpacking 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;">5</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>), done. From github.com:JavyZheng/git_demo <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>d4f14a..e2e17d3 master -> origin/master <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 尝试自动合并file1</span> Auto-merging file1 <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 发现冲突,需要手动解决冲突</span> CONFLICT (content): Merge conflict <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> file1 Automatic merge failed; fix conflicts and <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> commit the result. <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 此时,git已经把可能冲突的地方都写进了文件</span> $ vim file1 <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 可以看见冲突的地方</span> <<<<<<< HEAD file1 + add <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> ======= file1 + del <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span> >>>>>>> e2e17d311ec33700e94ce5dd694aa340920deb7c <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## vim里手动解决冲突后,add进来</span> $ git add file1 $ git commit -m <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"resolve confict in file1"</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## 推送到远程分支</span> $ git push</code>