git/github使用完整教程(2)分支
?
分支
首先,我們創建dev分支,然后切換到dev分支:
$ git checkout -b dev Switched to a new branch 'dev'git checkout命令加上-b參數表示創建并切換,相當于以下兩條命令:
$ git branch dev $ git checkout dev Switched to branch 'dev'然后,用git branch命令查看當前分支:
$ git branch * devmastergit branch命令會列出所有分支,當前分支前面會標一個*號。
然后,我們就可以在dev分支上正常提交,比如對readme.txt做個修改,加上一行:
Creating a new branch is quick.然后提交:
$ git add readme.txt $ git commit -m "branch test" [dev b17d20e] branch test1 file changed, 1 insertion(+)現在,dev分支的工作完成,我們就可以切換回master分支:
$ git checkout master Switched to branch 'master'切換回master分支后,再查看一個readme.txt文件,剛才添加的內容不見了!因為那個提交是在dev分支上,而master分支此刻的提交點并沒有變:
現在,我們把dev分支的工作成果合并到master分支上:
$ git merge dev Updating d46f35e..b17d20e Fast-forwardreadme.txt | 1 +1 file changed, 1 insertion(+)git merge命令用于合并指定分支到當前分支。合并后,再查看readme.txt的內容,就可以看到,和dev分支的最新提交是完全一樣的。
本地分支沖突
準備新的feature1分支,繼續我們的新分支開發:
$ git switch -c feature1 Switched to a new branch 'feature1'修改readme.txt最后一行,改為:
Creating a new branch is quick AND simple.在feature1分支上提交:
$ git add readme.txt$ git commit -m "AND simple" [feature1 14096d0] AND simple1 file changed, 1 insertion(+), 1 deletion(-)切換到master分支:
$ git switch master Switched to branch 'master' Your branch is ahead of 'origin/master' by 1 commit.(use "git push" to publish your local commits)Git還會自動提示我們當前master分支比遠程的master分支要超前1個提交。
在master分支上把readme.txt文件的最后一行改為:
Creating a new branch is quick & simple.提交:
$ git add readme.txt $ git commit -m "& simple" [master 5dc6824] & simple1 file changed, 1 insertion(+), 1 deletion(-)現在,master分支和feature1分支各自都分別有新的提交,變成了這樣:
這種情況下,Git無法執行“快速合并”,只能試圖把各自的修改合并起來,但這種合并就可能會有沖突,我們試試看:
$ git merge feature1 Auto-merging readme.txt CONFLICT (content): Merge conflict in readme.txt Automatic merge failed; fix conflicts and then commit the result.Git告訴我們,readme.txt文件存在沖突,必須手動解決沖突后再提交。git status也可以告訴我們沖突的文件:
$ git status On branch master Your branch is ahead of 'origin/master' by 2 commits.(use "git push" to publish your local commits)You have unmerged paths.(fix conflicts and run "git commit")(use "git merge --abort" to abort the merge)Unmerged paths:(use "git add <file>..." to mark resolution)both modified: readme.txtno changes added to commit (use "git add" and/or "git commit -a")我們可以直接查看readme.txt的內容:
Git is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage. Git tracks changes of files. <<<<<<< HEAD Creating a new branch is quick & simple. ======= Creating a new branch is quick AND simple. >>>>>>> feature1Git用<<<<<<<,=======,>>>>>>>標記出不同分支的內容,我們修改如下后保存:
Creating a new branch is quick and simple.再提交:
$ git add readme.txt $ git commit -m "conflict fixed" [master cf810e4] conflict fixedgithub分支
當你從遠程倉庫克隆時,實際上Git自動把本地的master分支和遠程的master分支對應起來了,并且,遠程倉庫的默認名稱是origin。要查看遠程庫的信息,用git remote:
$ git remote origin或者,用git remote -v顯示更詳細的信息:
$ git remote -v origin git@github.com:yourname/learngit.git (fetch) origin git@github.com:yourname/learngit.git (push)上面顯示了可以抓取和推送的origin的地址。如果沒有推送權限,就看不到push的地址。
推送分支
推送分支,就是把該分支上的所有本地提交推送到遠程庫。推送時,要指定本地分支,這樣,Git就會把該分支推送到遠程庫對應的遠程分支上:
$ git push origin master如果要推送其他分支,比如dev,就改成:
$ git push origin dev但是,并不是一定要把本地分支往遠程推送,那么,哪些分支需要推送,哪些不需要呢?
-
master分支是主分支,因此要時刻與遠程同步;
-
dev分支是開發分支,團隊所有成員都需要在上面工作,所以也需要與遠程同步;
-
bug分支只用于在本地修復bug,就沒必要推到遠程了,除非老板要看看你每周到底修復了幾個bug;
-
feature分支是否推到遠程,取決于你是否和你的小伙伴合作在上面開發。
多人分支沖突
多人協作的工作模式通常是這樣:
首先,可以試圖用git push origin <branch-name>推送自己的修改;
如果推送失敗,則因為遠程分支比你的本地更新,需要先用git pull試圖合并;
如果合并有沖突,則解決沖突,并在本地提交;
沒有沖突或者解決掉沖突后,再用git push origin <branch-name>推送就能成功!
如果git pull提示no tracking information,則說明本地分支和遠程分支的鏈接關系沒有創建,用命令git branch --set-upstream-to <branch-name> origin/<branch-name>。
實戰:
如果你的小伙伴已經向origin/dev分支推送了他的提交,而碰巧你也對同樣的文件作了修改,并試圖推送:
$ git push origin dev To github.com:yourname/learngit.git! [rejected] dev -> dev (non-fast-forward) error: failed to push some refs to 'git@github.com:yourname/learngit.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.推送失敗,因為你的小伙伴的最新提交和你試圖推送的提交有沖突,解決辦法也很簡單,Git已經提示我們,先用git pull把最新的提交從origin/dev抓下來,然后,在本地合并,解決沖突,再推送:
$ git pull There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details.git pull <remote> <branch>If you wish to set tracking information for this branch you can do so with:git branch --set-upstream-to=origin/<branch> devgit pull也失敗了,原因是沒有指定本地dev分支與遠程origin/dev分支的鏈接,根據提示,設置dev和origin/dev的鏈接:
$ git branch --set-upstream-to=origin/dev dev Branch 'dev' set up to track remote branch 'dev' from 'origin'.再pull:
$ git pull Auto-merging env.txt CONFLICT (add/add): Merge conflict in env.txt Automatic merge failed; fix conflicts and then commit the result.?
總結
以上是生活随笔為你收集整理的git/github使用完整教程(2)分支的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 巨胸美腿一览无遗 韩国美女主播晒旗袍照
- 下一篇: 未来激情狂飙《爆发赛车》游戏模式大公开