2013-06-25 90 views
3

每當我從我們的「主」分支重新綁定到我的功能分支時,我的功能分支似乎失去了它的跟蹤信息。這個工作流程有什麼問題?git rebase導致功能分支丟失跟蹤信息

$ git clone repo_url 
$ git co -b feature/my_work_task 

做了一堆的工作在這裏

$ git commit -am"Commit my work" 
$ git push # push the branch upstream 
$ git checkout master 
$ git pull # get whatever changes that have been put into master 
$ git co feature/my_work_task 
$ git rebase master # everything seems to be good 

讓我的特性分支的一些額外的改變。

$ git commit -am"more changes to update" 
$ git push # pushing this to my remote repo 

推到遠程回購失敗,出現以下錯誤:

To [email protected]:/username/my-repo.git 
! [rejected]  HEAD -> feature/my_work_task (non-fast-forward) 
error: failed to push some refs to '[email protected]:/username/my-repo.git' 
hint: Updates were rejected because the tip of your current branch is behind 
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') 
hint: before pushing again. 
hint: See the 'Note about fast-forwards' in 'git push --help' for details. 

我會做一個「混帳拉」的建議然後導致:

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 feature/my_work_task origin/<branch> 

如果我設置了上游信息,然後再次嘗試'git pull',現在我會遇到各種文件衝突。

讓功能分支跟上主人變化的最佳方式是什麼?

回答

9

當你推動一個分支後,你不能重定義它。

Rebase'重播'通過主分支的所有更改(git rebase master)。這會重新編寫您的索引。因此,即使您的本地更改全部保留並在主服務器上重播(所有各種提交,無論您重新分配了多少次),只要您推送遠程服務器就會看到不同的索引,從而出現錯誤。

所以,一旦你推動你的分支,你就不能改變主人。

相反:

  • git checkout -b b
  • git commit -am "stuff"
  • git push origin b
  • git checkout master
  • git pull origin master
  • git checkout b
  • git合併master
+0

哦,我明白了。現在,如果我想最終將所有功能分支更改平鋪到master中,並將其推到master中,我可以這樣做:git merge --squash feature/branch_name? –

+0

是的 - 如果你看看你的分支在rebase之前/之後的提交引用,你會發現它們完全改變了。提交引用實際上是當時存儲庫整個狀態的哈希值,所以即使像更改提交提交消息這樣的簡單內容也會改變提交引用。當你從主人合併到你的分支時,你會在頂部添加一個新的提交而不是重寫歷史記錄。 – simonp

+0

只要沒有其他人在使用分支,他就可以'git rebase master'。他只需要強制推動該分支。你確實需要了解你在做什麼。而且,你需要確保其他人不使用該分支,或者如果他們知道他們也會重新分配。但是如果分支機構實際上是私人的,那麼推動分支機構並沒有什麼壞處。 – jszakmeister