简体   繁体   中英

Git branch diverged from master

I had feature branch and then master branch .

Last week i worked on feature branch and then on feature branch i did this yesterday

git rebase master .

Other person aslo commited osme stuff in master branch today.

So today i did this

master# git pull
master# git rebase feature

Now i get this

$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 18 and 11 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
nothing to commit, working directory clean

What should i do. can i do git push -f

It would be extremely informative to try to create some diagrams explaining exactly what you did, so let's give that a shot. Let's assume that you when you created your feature branch from the remote master, it had only 3 commits. This would mean the two local branches would start off looking like the following:

A <- B <- C master
A <- B <- C feature

Let's also assume that during the last week, you made some commits to your local feature branch, and other developers on your team also made commits to the remote master. After pulling your master branch via master# git pull , your two local branches would look like this:

A <- B <- C <- 1 <- 2 <- 3 master
A <- B <- C <- D <- E feature

Here, the commits 1, 2, and 3 came from other developers, while commits D and E came from you.

Next, if you did a local rebase of master on the feature via master# git rebase feature , the two branches would look like this:

A <- B <- C <- D <- E <- 1' <- 2' <- 3' master
A <- B <- C <- D <- E feature

The master commits 1, 2 and 3 have now become 1', 2' and 3' because the are actually different commits made after replaying the feature on your master branch.

Finally, let's compare your local master branch with the remote master:

A <- B <- C <- D <- E <- 1' <- 2' <- 3' local master
A <- B <- C <- 1 <- 2 <- 3 remote master

When you did that git status call, Git told you that your local master has diverged from the remote. What this means is that the local and remote master share a common ancestor commit, but each has different commits beyond that point. This also means that you cannot fast forward the remote master. Your only options at this point are to force push via git push -f , or to git merge . As Oliver Charleworth pointed out, forcing your local branch to the remote is usually a bad idea because it can overwrite all the changes from other developers using master. Your remaining option is to merge your local master with the remote.

The easier (and cleaner) way to proceed moving forward is just to do all your work in the feature branch and then merge it into master when the time comes to do that. Typically, you should only be rebasing a feature on (or merging a feature into) master, not the other way around.

No, if you push -f, you will obliterate changes that were committed to master by the other developer.

You have two options:

1) Merge with master.

git merge master

2) Rebase onto master

git rebase <sha1> onto --master

<sha1> would be the original commit on master that you branched off from.

These options are very different from each other, and you need to fully understand what they do. Which one you need to do depends entirely on what your intentions are, going forward. If you explain exactly what you're trying to achieve, some further recommendations could be made.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM