深入理解git分支分叉(branch diverged)的問題

問:

I googled and read many posts, but none could make me understand the branch divergence problem yet.

If I've remote tracking branch, I often get into the following:

$ git status
# On branch feature/worker-interface
# Your branch and 'origin/feature/worker-interface' have diverged,
# and have 1 and 4 different commit(s) each, respectively.
答:

First, you can use the cherry command to see what commits differ between branches, and in what ways. So in this case, running g cherry origin/feature/worker-interface shows us the status of commits on the current branch and how they stack up against origin/feature/worker-interface. You will find 1 repo which you forgot to commit.

首先,你可以使用git cherry命令查看提交的分支的不同,也就是說你可以git cherry origin/master 查看,顯示在當前分支origin/master下提交的狀態,你將發現有1個版本庫你忘記了提交。

Now, lets see whats happening with the 'origin/feature/worker-interface' and its commits.For this we can run a log command with a special format gl ..origin/feature/worker-interface --oneline

現在,讓我們看看origin/master到底發生了什麼情況,我們用git log ..origin/master --online 查看。

Here we see 4 commits that don't exist in our current branch

我們發現有4個提交在我們的當前分支下不存在。

So now we have a good idea of what’s happened. you’ve made 1 commits on your local master branch, and it looks like there are 4 commits on origin/feature/worker-interface which you don’t have merged in yet. So, you could just blindly merge things together and go on your way (if they merge without conflict), but I’d like to show you how to deal with it in a more controlled manner.

所以我們很想知道這到底是什麼原因導致了這種情況的發生。你在本地的master分支提交了一次,但是看上去應該是四次提交,這四次提交你都還沒有進行合併過。那麼,你可能盲目的把它們進行合併,但我將告訴你這應該怎麼更好的處理這個問題:

1) First, create a branch that points to your current HEAD: gco -b local_changes 2) Now that we have that to keep track of those changes, we switch back to feature/worker-interface: gco feature/worker-interface 3) At this point, reset the feature/worker-interface branch to get rid of the 1 commit. 4) There you go! You can check your status of branch git status you will be prompted asnothing to commit


1),你應該創建一個分支指向你當前的HEAD ,如果有分支可以不用創建(git checkout -b local_changes)

2) 現在我們可以追蹤到分支上的變化,我們切回到origin/master ,git checkout origin/master

3)之後git reset origin/master 刪除origin/master 分支上的一次提交

4)OK,完工。git status查看下。

參考:http://stackoverflow.com/questions/9189413/good-and-clear-understanding-of-the-git-branches-diverged-issue

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章