GitHub使用(四) - 關於分支Branch


1. 什么是分支Branch?
我初步的理解為:GitHub倉庫默認有一個master的分支,當我們在master分支開發過程中接到一個新的功能需求,我們就可以新建一個分支同步開發而互不影響,開發完成后,在合並merge到主分支master上。
 
2.分支操作
現在我們需要合並兩個分支:"master" 和 "gh-pages",合並之前,我們先了解幾個基本的git命令
①. 打開 Git Shell
 
②. 輸入命令 git branch,我們可以看到我們目前的兩個分支
 
③. 輸入命令git branch new_branch, 創建一個新的分支 new_branch
 
④. 輸入命令 git chekedout gh-pages, 我們可以切換分支
 
or :  新建一個分支並且立即切換到新建的分支上,輸入命令 git checkout -b new_branch_ha
 
⑤.  合並分支,輸入命令git merge gh-pages 報錯! fatal:refusing to merge unrelated histories.
 
Google一下,找到如下解釋 
 * "git merge" used to allow merging two branches that have no common base by default, which led to a brand new history of an existing project created and then get pulled by an unsuspecting maintainer, which allowed an unnecessary parallel history merged into the existing project.  The command has been taught not to allow this by default, with an escape hatch "--allow-unrelated-histories" option to be used in a rare event that merges histories of two projects that started their lives independently.
 * "git pull" has been taught to pass the "--allow-unrelated-histories" option to underlying "git merge".
 
解決方法:
在命令后面加上一句“ --allow-unrelated-histories”,即完整的命令為“git merge gh-pages --allow-unrelated-histories”,可以看到 gh-pages分支上的文件全部合並到了 master分支上
 
接下來,我們需要輸入命令“git push”,可以看到我們的代碼已經同步到倉庫 test.github.com 上啦
 
打開github主頁,進入我們的倉庫 test.github.com ,就可以看到分支 gh-pages 上的文件都已經同步過來啦
 
⑥.  刪除分支,剛剛我們在第4步新建了一個多余的分支“new_branch_ha”需要刪掉,我們首先輸入命令“git branch”查看當前的分支有哪些,然后輸入命令“ git branch -D new_branch_ha”刪除分支“new_branch_ha”,最后我們輸入命令“git branch”查看分支已經被我們成功刪除。

 

  (本文原創,轉載請注明出處!!)
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM