PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/git/ch3-5.html

https://bitbucket.org/thncr/manuals
HTML | 163 lines | 127 code | 36 blank | 0 comment | 0 complexity | 5f32c32fcedb7a76b24eeb9a3b2c06cd MD5 | raw file
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  2. "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-type" content="text/html; charset=utf-8">
  6. <title>Pro Git - Pro Git 3.5 Git 分支 远程分支</title>
  7. <link rel="stylesheet" href="/stylesheets/master.css" type="text/css" media="screen" charset="utf-8"/>
  8. <script src="/javascripts/jquery.js" type="text/javascript" charset="utf-8"></script>
  9. <script src="/javascripts/jquery.github.js" type="text/javascript" charset="utf-8"></script>
  10. <link rel='alternate' type='application/rss+xml' href='http://feeds.feedburner.com/ProGitBook' />
  11. </head>
  12. <body>
  13. <div id='wrapper'>
  14. <div id='header'>
  15. <h1>Pro Git</h1>
  16. <h2>professional version control</h2>
  17. </div>
  18. <div id='menu'>
  19. <ul>
  20. <li><a href='/'>Home</a></li>
  21. <li><a href='/book'>Book</a></li>
  22. <li><a href='/blog.html'>Blog</a></li>
  23. <li><a href='/about.html'>About</a></li>
  24. <li><a href='/support.html'>Support Us</a></li>
  25. <li><a href='http://github.com/progit' target='_blank' rel='me'>GitHub</a></li>
  26. <li><a href='http://twitter.com/progitbook' target='_blank' rel='me'>Twitter</a></li>
  27. </ul>
  28. </div>
  29. <div id="message">
  30. This is an in-progress translation. <br/>
  31. To help translate the book, please
  32. fork the book <a href="http://github.com/progit/progit">at GitHub</a> and push your contributions.
  33. </div>
  34. <div id='content'>
  35. <h2 id='id251'>远程分支</h2>
  36. <p>远程分支remote branch是对远程仓库状态的索引它们是一些无法移动的本地分支只有在进行 Git 的网络活动时才会更新远程分支就像是书签提醒着你上次连接远程仓库时上面各分支的位置</p>
  37. <p>我们用 <code>(远程仓库名)/(分支名)</code> 这样的形式表示远程分支比如我们想看看上次同 <code>origin</code> 仓库通讯时 <code>master</code> 的样子就应该查看 <code>origin/master</code> 分支如果你和同伴一起修复某个问题但他们先推送了一个 <code>iss53</code> 分支到远程仓库虽然你可能也有一个本地的 <code>iss53</code> 分支但指向服务器上最新更新的却应该是 <code>origin/iss53</code> 分支</p>
  38. <p>可能有点乱我们不妨举例说明假设你们团队有个地址为 <code>git.ourcompany.com</code> Git 服务器如果你从这里克隆Git 会自动为你将此远程仓库命名为 <code>origin</code>并下载其中所有的数据建立一个指向它的 <code>master</code> 分支的指针在本地命名为 <code>origin/master</code>但你无法在本地更改其数据接着Git 建立一个属于你自己的本地 <code>master</code> 分支始于 <code>origin</code> <code>master</code> 分支相同的位置你可以就此开始工作见图 3-22</p>
  39. <p><center><img src="/figures/ch3/18333fig0322-tn.png"></center><br/> 3-22. 一次 Git 克隆会建立你自己的本地分支 master 和远程分支 origin/master它们都指向 origin/master 分支的最后一次提交</p>
  40. <p>要是你在本地 <code>master</code> 分支做了会儿事情与此同时其他人向 <code>git.ourcompany.com</code> 推送了内容更新了上面的 <code>master</code> 分支那么你的提交历史会开始朝不同的方向发展不过只要你不和服务器通讯你的 <code>origin/master</code> 指针不会移动见图 3-23</p>
  41. <p><center><img src="/figures/ch3/18333fig0323-tn.png"></center><br/> 3-23. 在本地工作的同时有人向远程仓库推送内容会让提交历史发生分歧</p>
  42. <p>可以运行 <code>git fetch origin</code> 来进行同步该命令首先找到 <code>origin</code> 是哪个服务器本例为 <code>git.ourcompany.com</code>从上面获取你尚未拥有的数据更新你本地的数据库然后把 <code>origin/master</code> 的指针移到它最新的位置见图 3-24</p>
  43. <p><center><img src="/figures/ch3/18333fig0324-tn.png"></center><br/> 3-24. git fetch 命令会更新 remote 索引</p>
  44. <p>为了演示拥有多个远程分支不同的远程服务器的项目是个什么样我们假设你还有另一个仅供你的敏捷开发小组使用的内部服务器 <code>git.team1.ourcompany.com</code>可以用第二章中提到的 <code>git remote add</code> 命令把它加为当前项目的远程分支之一我们把它命名为 <code>teamone</code>表示那一整串 Git 地址见图 3-25</p>
  45. <p><center><img src="/figures/ch3/18333fig0325-tn.png"></center><br/> 3-25. 把另一个服务器加为远程仓库</p>
  46. <p>现在你可以用 <code>git fetch teamone</code> 来获取小组服务器上你还没有的数据了由于当前该服务器上的内容是你 <code>origin</code> 服务器上的子集Git 不会下载任何数据而只是简单地创建一个名为 <code>teamone/master</code> 的分支来指向 <code>teamone</code> 服务器上 <code>master</code> 所指向的更新 <code>31b8e</code>见图 3-26</p>
  47. <p><center><img src="/figures/ch3/18333fig0326-tn.png"></center><br/> 3-26. 你在本地有了一个指向 teamone 服务器上 master 分支的索引</p>
  48. <h3 id='id252'>推送</h3>
  49. <p>要想和其他人分享某个分支你需要把它推送到一个你拥有写权限的远程仓库你的本地分支不会被自动同步到你引入的远程分支中除非你明确执行推送操作换句话说对于无意分享的你尽可以保留为私人分支而只推送那些协同工作的特性分支</p>
  50. <p>如果你有个叫 <code>serverfix</code> 的分支需要和他人一起开发可以运行 <code>git push (远程仓库名) (分支名)</code></p>
  51. <pre><code>$ git push origin serverfix
  52. Counting objects: 20, done.
  53. Compressing objects: 100% (14/14), done.
  54. Writing objects: 100% (15/15), 1.74 KiB, done.
  55. Total 15 (delta 5), reused 0 (delta 0)
  56. To git@github.com:schacon/simplegit.git
  57. * [new branch] serverfix -&gt; serverfix</code></pre>
  58. <p>这其实有点像条捷径Git 自动把 <code>serverfix</code> 分支名扩展为 <code>refs/heads/serverfix:refs/heads/serverfix</code>意为取出我的 serverfix 本地分支推送它来更新远程仓库的 serverfix 分支我们将在第九章进一步介绍 <code>refs/heads/</code> 部分的细节不过一般使用的时候都可以省略它也可以运行 <code>git push origin serverfix:serferfix</code> 来实现相同的效果它的意思是提取我的 serverfix 并更新到远程仓库的 serverfix通过此语法你可以把本地分支推送到某个命名不同的远程分支若想把远程分支叫作 <code>awesomebranch</code>可以用 <code>git push origin serverfix:awesomebranch</code> 来推送数据</p>
  59. <p>接下来当你的协作者再次从服务器上获取数据时他们将得到一个新的远程分支 <code>origin/serverfix</code></p>
  60. <pre><code>$ git fetch origin
  61. remote: Counting objects: 20, done.
  62. remote: Compressing objects: 100% (14/14), done.
  63. remote: Total 15 (delta 5), reused 0 (delta 0)
  64. Unpacking objects: 100% (15/15), done.
  65. From git@github.com:schacon/simplegit
  66. * [new branch] serverfix -&gt; origin/serverfix</code></pre>
  67. <p>值得注意的是 fetch 操作抓来新的远程分支之后你仍然无法在本地编辑该远程仓库换句话说在本例中你不会有一个新的 <code>serverfix</code> 分支有的只是一个你无法移动的 <code>origin/serverfix</code> 指针</p>
  68. <p>如果要把该内容合并到当前分支可以运行 <code>git merge origin/serverfix</code>如果想要一份自己的 <code>serverfix</code> 来开发可以在远程分支的基础上分化出一个新的分支来</p>
  69. <pre><code>$ git checkout -b serverfix origin/serverfix
  70. Branch serverfix set up to track remote branch refs/remotes/origin/serverfix.
  71. Switched to a new branch &quot;serverfix&quot;</code></pre>
  72. <p>这会切换到新建的 <code>serverfix</code> 本地分支其内容同远程分支 <code>origin/serverfix</code> 一致你可以在里面继续开发了</p>
  73. <h3 id='id253'>跟踪分支</h3>
  74. <p>从远程分支检出的本地分支称为_跟踪分支(tracking branch)_跟踪分支是一种和远程分支有直接联系的本地分支在跟踪分支里输入 <code>git push</code>Git 会自行推断应该向哪个服务器的哪个分支推送数据反过来在这些分支里运行 <code>git pull</code> 会获取所有远程索引并把它们的数据都合并到本地分支中来</p>
  75. <p>在克隆仓库时Git 通常会自动创建一个 <code>master</code> 分支来跟踪 <code>origin/master</code>这正是 <code>git push</code> <code>git pull</code> 一开始就能正常工作的原因当然你可以随心所欲地设定为其它跟踪分支比如 <code>origin</code> 上除了 <code>master</code> 之外的其它分支刚才我们已经看到了这样的一个例子<code>git checkout -b [分支名] [远程名]/[分支名]</code>如果你有 1.6.2 以上版本的 Git还可以用 <code>--track</code> 选项简化</p>
  76. <pre><code>$ git checkout --track origin/serverfix
  77. Branch serverfix set up to track remote branch refs/remotes/origin/serverfix.
  78. Switched to a new branch &quot;serverfix&quot;</code></pre>
  79. <p>要为本地分支设定不同于远程分支的名字只需在前个版本的命令里换个名字</p>
  80. <pre><code>$ git checkout -b sf origin/serverfix
  81. Branch sf set up to track remote branch refs/remotes/origin/serverfix.
  82. Switched to a new branch &quot;sf&quot;</code></pre>
  83. <p>现在你的本地分支 <code>sf</code> 会自动向 <code>origin/serverfix</code> 推送和抓取数据了</p>
  84. <h3 id='id254'>删除远程分支</h3>
  85. <p>如果不再需要某个远程分支了比如搞定了某个特性并把它合并进了远程的 <code>master</code> 分支或任何其他存放稳定代码的地方可以用这个非常无厘头的语法来删除它<code>git push [远程名] :[分支名]</code>如果想在服务器上删除 <code>serverfix</code> 分支运行下面的命令</p>
  86. <pre><code>$ git push origin :serverfix
  87. To git@github.com:schacon/simplegit.git
  88. - [deleted] serverfix</code></pre>
  89. <p>服务器上的分支没了你最好特别留心这一页因为你一定会用到那个命令而且你很可能会忘掉它的语法有种方便记忆这条命令的方法记住我们不久前见过的 <code>git push [远程名] [本地分支]:[远程分支]</code> 语法如果省略 <code>[本地分支]</code>那就等于是在说在这里提取空白然后把它变成<code>[远程分支]</code></p>
  90. <div id='nav'>
  91. <a href='ch3-4.html'>prev</a> | <a href='ch3-6.html'>next</a>
  92. </div>
  93. <div class='clearfix'></div>
  94. </div>
  95. </div>
  96. <div id='footer'>
  97. Book translated into
  98. <a href="/book/de">German</a>,
  99. <a href="/book/zh">Chinese</a>,
  100. <a href="/book/ja">Japanese</a> and
  101. <a href="/book/nl">Dutch</a>.<br/>
  102. Partial translations available in
  103. <a href="/book/ar">Arabic</a>,
  104. <a href="/book/cs">Czech</a>,
  105. <a href="/book/es">Spanish</a>,
  106. <a href="/book/fr">French</a>,
  107. <a href="/book/id">Indonesian</a>,
  108. <a href="/book/it">Italian</a>,
  109. <a href="/book/mk">Macedonian</a>,
  110. <a href="/book/pl">Polish</a>,
  111. <a href="/book/th">Thai</a> and
  112. <a href="/book/ru">Russian</a>
  113. .<br/>
  114. All content under <a href="http://creativecommons.org/licenses/by-nc-sa/3.0/us/">Creative Commons Attribution-Non Commercial-Share Alike 3.0 license</a>.
  115. <br/>Theme and code by <a href="http://github.com/mbleigh">Michael Bleigh</a>. Hosted by <a href='http://github.com/progit/progit.github.com/' target='_blank'>GitHub</a> and powered by <a href='http://github.com/mojombo/jekyll'>Jekyll</a>.
  116. </div>
  117. <script type="text/javascript">
  118. var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
  119. document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
  120. </script>
  121. <script type="text/javascript">
  122. try {
  123. var pageTracker = _gat._getTracker("UA-82337-15");
  124. pageTracker._trackPageview();
  125. } catch(err) {}</script>
  126. </body>
  127. </html>