Git常用命令总结

时间:2022-12-10 08:14:38

1. Git仓库的创建

  • 在当前目录新建一个Git仓库
<code class="language-Bash hljs java 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 init

$ cd TestGit
$ git init
Initialized empty Git repository in /Users<span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/***/</span>TeskGit/.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: 1px solid 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: 1px solid 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>
  • 在指定目录下新建一个Git仓库
<code class="language-Bash hljs java 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 init [Directory Name]

$ git init TestGit
Initialized empty Git repository in /Users<span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/***/</span>TeskGit/.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: 1px solid 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><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: 1px solid 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>
  • 从Github等地方克隆一个仓库到当前目录(可能需要输入密码,以ssh方式克隆),如果不指定分支,则默认从Refactor仓库的Default branch(一般为master)克隆
<code class="language-Bash 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;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 命令形式:git clone [url] -b [branch name]</span>

$ git clone ssh://git@github.com/**/Refacor -b master
Cloning into <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Refacor'</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span>
Saving password to keychain failed
Identity added: /Users/**/.ssh/id_rsa_github ((null))
remote: Counting objects: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>, done.
remote: 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;">3</span> (delta <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</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;">3</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</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: 1px solid 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><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: 1px solid 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>

2. 关联远程仓库

  • 将本地仓库与远程仓库关联,首先要初始化一个本地仓库,url可以在github的仓库获得,origin为远程仓库在本地的别名,即origin=git@github.com:*/Refacor.git
<code class="language-Bash 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;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># 命令形式:git remote add [remote repository aliase] [url]</span>

$ git remote <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">add</span> origin git@github<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.com</span>:***/Refacor<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.git</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: 1px solid 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><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: 1px solid 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="language-Bash 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;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># 命令形式:git remote [-v]</span>

$ git remote
origin
$ git remote -v
origin git@github<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.com</span>:***/Refacor<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>:***/Refacor<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: 1px solid 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><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: 1px solid 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>
  • 删除本地仓库关联的远程仓库
<code class="language-Bash 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-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 命令形式:git remote rm [remote repository aliase]</span>

<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>git remote rm origin
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>git remote</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: 1px solid 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><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: 1px solid 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>
  • 关联远程仓库后,从远程仓库取内容,并根据远端仓库在本地创建了两个分支,master和develop
<code class="language-Bash hljs coffeescript 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;"># 命令形式:git fetch</span>

$ git fetch
<span class="hljs-attribute" style="box-sizing: border-box; color: rgb(0, 136, 0);">remote</span>: Counting <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(0, 136, 0);">objects</span>: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>, done.
<span class="hljs-attribute" style="box-sizing: border-box; color: rgb(0, 136, 0);">remote</span>: 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;">3</span> (delta <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>), pack-reused <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>
Unpacking <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(0, 136, 0);">objects</span>: <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>), done.
From github.<span class="hljs-attribute" style="box-sizing: border-box; color: rgb(0, 136, 0);">com</span>:***/Refacor
* [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> branch] develop<span class="hljs-function" style="box-sizing: border-box;"> -></span> origin/develop
* [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> branch] master<span class="hljs-function" style="box-sizing: border-box;"> -></span> 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: 1px solid 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><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: 1px solid 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>
  • 显示远程仓库的信息
<code class="language-Bash hljs sql 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 remote <span class="hljs-operator" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">show</span> [remote repository name]

$ git remote <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">show</span> origin
* remote origin
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Fetch</span> URL: git@github.com:***/Refacor.git
Push URL: git@github.com:***/Refacor.git
HEAD branch: master
Remote branches:
develop tracked
master tracked
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Local</span> branches configured <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'git pull'</span>:
develop merges <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">with</span> remote develop
master merges <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">with</span> remote master
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Local</span> refs configured <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'git push'</span>:
develop pushes <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> develop (up <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">date</span>)
master pushes <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> master (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">local</span> out <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">of</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">date</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: 1px solid 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></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: 1px solid 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></ul>

3. Git分支的创建、切换、删除、关联

  • 创建本地分支并切换到新创建的分支,-b代表新创建一个分支,-B用在本地分支已经存在的情况下,强行创建一个新分支并将原来的分支覆盖,checkout主要是分支切换,创建分支使用branch命令
<code class="language-Bash 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-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 命令形式:git checkout [-b or -B] [local branch name]</span>

$ git checkout -b <span class="hljs-operator" style="box-sizing: border-box;">a</span>
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;">'a'</span>
$ git checkout -b <span class="hljs-operator" style="box-sizing: border-box;">a</span>
fatal: A branch named <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'a'</span> already exists.
$ git checkout -B <span class="hljs-operator" style="box-sizing: border-box;">a</span>
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;">and</span> reset branch <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'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: 1px solid 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: 1px solid 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>
  • 切换本地分支
<code class="language-Bash 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-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 命令形式:git checkout [local branch name]</span>

<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>git checkout develop
<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;">'develop'</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: 1px solid 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><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: 1px solid 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>
  • 查看当前所在的本地分支,创建本地分支,local branch name不存在时是查看当前所在的本地分支,存在时是创建一个新的本地分支
<code class="language-Bash 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-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 命令形式:git branch [local branch name]</span>

<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>git branch
* a
develop
master

<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>git branch b</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: 1px solid 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: 1px solid 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>
  • 查看远程分支和所有分支,-r是查看远程分支,-a是查看所有分支
<code class="language-Bash 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-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 命令形式:git branch [-r] [-a]</span>

<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>git branch -r
origin/develop
origin/master
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>git branch -a
develop
* master
remotes/origin/develop
remotes/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: 1px solid 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><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: 1px solid 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>
  • 删除本地分支
<code class="language-Bash 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;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 命令形式:git branch -d [local branch name]</span>

$ git branch <span class="hljs-operator" style="box-sizing: border-box;">-d</span> test
Deleted branch test (was <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>b05e43).</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: 1px solid 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><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: 1px solid 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="language-Bash 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-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 命令形式:git checkout -b [local branch name] origin/[remote branch name]</span>

$ git checkout -b develop origin/develop
Branch develop <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 develop <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;">'develop'</span>

$ git checkout -b master origin/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.
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;">'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: 1px solid 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><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: 1px solid 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>
  • 关联远程分支
<code class="language-Bash hljs oxygene 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 branch --<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">set</span>-upstream-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> [remote repository name/remote branch]

$ git branch --<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">set</span>-upstream-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> origin/develop
Branch develop <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">set</span> up <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> track remote branch develop <span class="hljs-keyword" style="color: rgb(0, 0, 136); 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: 1px solid 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><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: 1px solid 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>
  • 删除远程分支,git push origin –delete [remote branch name]是直接删除掉远程仓库的分支,git branch -dr [remote/branch]是删除本地分支与远程分支的关联关系。
<code class="language-Bash 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;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 命令形式:git push origin --delete [remote branch name] or git branch -dr [remote/branch]</span>

$ git <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">push</span> origin --<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">delete</span> develop
To git<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@github</span>.com:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">**</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*/</span>Refacor.git
- [deleted] develop

$ git branch -dr origin/develop
Deleted remote-tracking branch origin/develop (was d6813fd).</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: 1px solid 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: 1px solid 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>

注:本地可以有多个分支,远程也可以有多个分支,本地多个分支可以关联远程多个分支,但是,本地分支最好与远程分支同名,以免出现问题。

4.从远程仓库取内容,向远程仓库提交内容

向远程仓库提交内容之前,要理解三个概念:本地文件,缓冲区,本地仓库。平常修改的文件内容都是本地文件,在往远程仓库提交之前先要提交到缓冲区,再从缓冲区提交到本地仓库,然后本地仓库才能往远程仓库提交。本地的内容分为三大部分,它们是相互独立的,理解了这四个概念就明白为什么要执行Git add,git commit,git push命令了。

  • 从远程仓库取内容,git fetch默认情况下是当前的本地分支从其关联的远程分支上取内容,可以使用git branch先查看当前的本地分支,使用git status(包括本地分支和远程分支都能看到)查看其关联的远程分支。从下面的例子中可以看出本地分支为develop,远程分支也为develop
<code class="language-Bash hljs sql 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 fetch,git <span class="hljs-operator" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">merge</span>,git pull

$ git branch
* develop
master
$ git status
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">On</span> branch develop
Your branch <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is</span> up-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span>-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">date</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">with</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'origin/develop'</span>.
nothing <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">commit</span>, working directory clean
$ git <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">fetch</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: 1px solid 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><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: 1px solid 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>
  • 将从远程仓库取下的内容与本地仓库的内容进行合并,合并之前必须将本地的修改提交到本地仓库。
<code class="language-Bash 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-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 命令形式:git merge</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 正常情况下</span>
$ git merege
Already 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>.

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 没有提交修改到本地仓库的情况下</span>
$ git <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">merge</span>
Updating d6813fd.<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.2444</span>abc
error: Your <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">local</span> changes <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;">the</span> following <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">files</span> would be overwritten <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">by</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">merge</span>:
README.md
Please, commit your changes <span class="hljs-operator" style="box-sizing: border-box;">or</span> stash them <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">before</span> you can <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">merge</span>.
Aborting

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 本地仓库与取下的远程仓库内容有冲突的情况下</span>
$ git <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">merge</span>
Auto-merging README.md
CONFLICT (content): Merge conflict <span class="hljs-operator" style="box-sizing: border-box;">in</span> README.md
Automatic <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">merge</span> failed; fix conflicts <span class="hljs-operator" style="box-sizing: border-box;">and</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> commit <span class="hljs-operator" style="box-sizing: border-box;">the</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">result</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: 1px solid 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><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: 1px solid 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时常常会碰到的情况,冲突解决主要是在将本地仓库内容与远程仓库取下的内容merge后,进入merge后的文件进行修改,将冲突解决,然后重新add,commit,push即可。

冲突内容:

<code class="hljs asciidoc 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;"># Refacor
<<<<<<< HEAD
<span class="hljs-header" style="box-sizing: border-box;">branch develop
=======</span>
branch
>>>>>>> refs/remotes/origin/develop</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: 1px solid 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: 1px solid 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>

修改后的内容:

<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;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># Refacor</span>
branch develop</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: 1px solid 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><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: 1px solid 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="language-Bash 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;">$ vim README<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.md</span> 
$ git <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">add</span> .
$ git commit -m <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"feat: handle conflict"</span>
[develop d4c01a6] feat: handle conflict
$ 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;">4</span>, done.
Delta compression using up to <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</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;">4</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>), <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">416</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;">4</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>)
<span class="hljs-label" style="box-sizing: border-box;">remote:</span> 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.
To git@github<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.com</span>:***/Refacor<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;">2444</span>abc.<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.d</span>4c01a6 develop -> develop</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: 1px solid 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><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: 1px solid 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>
  • 直接从远程仓库取内容并合并
<code class="language-Bash 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;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># 命令形式:git pull</span>
$ git pull
<span class="hljs-label" style="box-sizing: border-box;">remote:</span> Counting objects: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>, done.
<span class="hljs-label" style="box-sizing: border-box;">remote:</span> 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>), 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;">3</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>), done.
From github<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.com</span>:***/Refacor
d4c01a6.<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.afa</span>74f2 develop -> origin/develop
Updating d4c01a6.<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.afa</span>74f2
Fast-forward
README<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.md</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;">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(-)</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: 1px solid 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><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: 1px solid 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>

备注:git pull = git fetch + git merge,git pull功能很强大,能自动合并冲突,合并后需要手动解决冲突,最好不要直接使用git pull,因为许多细节都看不到。

  • 将本地文件修改提交到缓冲区,git add filename是将单个文件提交到缓冲区,可以提交多个文件,中间用空格分开,也可提交目录,git add . 是将所有修改内容提交到缓冲区
<code class="language-Bash 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;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># 命令形式:git add [filename1] [filename2] or [directoryname] or [.]</span>
$ 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;">.md</span>
$ 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;">.md</span> a<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>
$ git <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">add</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: 1px solid 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><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: 1px solid 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="language-Bash 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;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># 命令形式:git commit [file1] [file2] -m [comment]</span>
$ git commit -m <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"feat: edit README.md"</span>
[develop d6813fd] feat: edit README<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.md</span>
<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(-)

$ git commit README<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.md</span> a<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> -m <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"feat: commit test"</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: 1px solid 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: 1px solid 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>
  • 将本地仓库内容提交到远程仓库,默认情况下,如果你的本地分支与远程分支同名且关联,git push就可以,但如果不是你需要加上更多的东西。我本地仓库有master和develop分支,远程仓库有master和develop分支,我本地仓库的develop分支与远程仓库的master分支关联。所以直接git push是不可以的。最好不要这么干,我只是测试一下。下面的另一个例子是我将本地仓库的develop分支与远程仓库的develop分支关联并提交修改到远程仓库。
<code class="language-Bash 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;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 命令形式:git push [remote repository name] [remote branch]</span>
git push
fatal: The upstream branch <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">of</span> your current branch <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">does</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">not</span> match
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">the</span> <span class="hljs-property" style="box-sizing: border-box;">name</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">of</span> your current branch. To push <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">the</span> upstream branch
<span class="hljs-function_start" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">on</span></span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">the</span> remote, use

git push origin HEAD:master

To push <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">the</span> branch <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">of</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">the</span> same <span class="hljs-property" style="box-sizing: border-box;">name</span> <span class="hljs-function_start" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">on</span></span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">the</span> remote, use

git push origin develop

$ git push origin HEAD:master
Counting objects: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</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;">296</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.com:***/Refacor.git
f7ea1a0..d6813fd HEAD -> 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: 1px solid 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></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: 1px solid 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></ul>
<code class="language-Bash hljs sql 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 branch <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--set-upstream-to origin/develop</span>Branch develop <span class="hljs-operator" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">set</span> up <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> track remote branch develop <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">from</span> origin.$ git status<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">On</span> branch developYour branch <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is</span> ahead <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">of</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'origin/develop'</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">by</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">commit</span>.  (use <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"git push"</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> publish your <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">local</span> commits)nothing <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">commit</span>, working directory clean$ git <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">fetch</span>$ git <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">merge</span>Already up-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span>-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">date</span>.$ git pushTotal <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>)<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">To</span> git@github.com:***/Refacor.git   f7ea1a0..d6813fd  develop -> develop</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: 1px solid 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><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: 1px solid 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>

5. Git配置

  • 查看Git的所有配置信息
<code class="language-Bash 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;">命令形式: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;">list</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;">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: 1px solid 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><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: 1px solid 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="language-Bash 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;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># 命令形式:git config --get [variable]</span>

$ git config --<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">get</span> user.name
test

$ 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;">get</span> user.email
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">global</span>@test.com</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: 1px solid 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><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: 1px solid 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>
  • 配置用户名和邮件,加上–global是配置全局的,否则是配置当前仓库的,当前仓库的配置会覆盖全局配置
<code class="language-Bash 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-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 命令形式:git config [--global] [user.name or user.email]</span>

<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>git config --global user.name <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"test global"</span>

<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>git config user.name <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"test"</span>

<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>git config --global user.email <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"global@test.com"</span>

<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>git config user.email <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"local@test.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: 1px solid 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><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: 1px solid 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>

6. 查看提交记录

  • 查看提交记录,当前分支的版本历史
<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;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#命令形式:git log</span>

$ git log
commit d6813fdf94c37d51e952aee42155654fe333ef28
<span class="hljs-label" style="box-sizing: border-box;">Author:</span> *** <***@gmail<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> Fri Oct <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">22</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">17</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2016</span> +<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0800</span>

feat: edit README<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.md</span>

commit f7ea1a04e423e135d92c4c93e4f188e7257b0cad
<span class="hljs-label" style="box-sizing: border-box;">Author:</span> *** <***@gmail<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> Fri Oct <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">22</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">39</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;">2016</span> +<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0800</span>

feat:develop

commit <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">68506</span>c5f153fc303fa33ceb733bf15b6da690b00
<span class="hljs-label" style="box-sizing: border-box;">Merge:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span>b7d7c8 f777a9b
<span class="hljs-label" style="box-sizing: border-box;">Author:</span> *** <***@gmail<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> Fri Oct <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">22</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">25</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2016</span> +<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0800</span>

feat: test

commit <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span>b7d7c87c32e144ea1364c1a0aa0fe16f074c8d5
<span class="hljs-label" style="box-sizing: border-box;">Author:</span> *** <***@gmail<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> Fri Oct <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">22</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</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;">2016</span> +<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0800</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: 1px solid 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></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: 1px solid 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></ul>
  • 显示commit历史,以及每次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;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># 命令形式:git log --stat</span>

$ git log --stat
commit afa74f2de6caede85b6b4beeefc3ec338ed42986
<span class="hljs-label" style="box-sizing: border-box;">Author:</span> *** <***@gmail<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> Fri Oct <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">22</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">17</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;">32</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2016</span> +<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0800</span>

Update README<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.md</span>

README<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.md</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;">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(-)


commit <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span>b313361b73cd76505c05e6b6ad3b0c6204357ce
<span class="hljs-label" style="box-sizing: border-box;">Author:</span> *** <***@gmail<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> Fri Oct <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">22</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">17</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">25</span>:<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;">2016</span> +<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0800</span>

feat: conflict

README<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.md</span> | <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</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: 1px solid 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></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: 1px solid 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></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;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 命令形式:git reflog</span>

$ git reflog
afa74f2 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</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> develop
d6813fd HEAD@{<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;">merge</span> refs/remotes/origin/master: Fast-forward
f777a9b 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> develop <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span> master
afa74f2 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>}: pull: Fast-forward
d4c01a6 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>}: commit (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">merge</span>): feat: handle conflict
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span>b31336 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>}: commit: feat: conflict
d6813fd HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>}: commit: feat: edit README.md
f7ea1a0 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>}: commit: feat:develop
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">68506</span>c5 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>}: commit (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">merge</span>): feat: test
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span>b7d7c8 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span>}: commit: feat:branch test
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0b0</span>5e43 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</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> develop
f777a9b HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">11</span>}: pull: Fast-forward
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0b0</span>5e43 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>}: checkout: moving <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> develop <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;">0b0</span>5e43 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">13</span>}: checkout: moving <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</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;">to</span> develop
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0b0</span>5e43 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">14</span>}: checkout: moving <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> develop <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-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0b0</span>5e43 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>}: checkout: moving <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</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;">to</span> develop
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0b0</span>5e43 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>}: checkout: moving <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> origin/develop <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-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0b0</span>5e43 HEAD@{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">17</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;">0b0</span>5e4388c3cb16d23a59f7238d59894c7f1fb8</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: 1px solid 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></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: 1px solid 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></ul>
  • 显示缓存区和工作区的差异
<code class="language-Bash hljs autohotkey 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 diff

$ git diff
diff --git <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">a</span>/<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">a</span>.txt b/<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">a</span>.txt
index d00491f..cd6c7f8 <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100644</span>
--- <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">a</span>/<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">a</span>.txt
+++ b/<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">a</span>.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> @@
-<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>dfa</code>