Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git branch diverged after rebase

Tags:

git

I have rebased a branch locally which was already pushed.

Git is advising that my branch and remote have diverged and that:

"and have 109 and 73 different commits each, respectively"

Will pushing my branch resolve this - i.e. is this to be expected after a rebase?

like image 298
Marty Wallace Avatar asked Oct 06 '22 08:10

Marty Wallace


People also ask

How do I fix my diverged branch?

This can be properly resolved by committing changes from the merge first by git commit , then adding and committing the unstaged changes as usual (e.g. by git commit -a ). Show activity on this post. Replace 123 with number of commits your branch has diverged from origin.

What does diverged mean in git?

A branch in git is a series of interrelated commits. If two branches follow a non-linear path then they diverge each other.

What are divergent branches in git?

Git Rebase Instead of a Merge As you may already know, Git allows for the creation of multiple branches that diverge from a “master” branch. On these divergent branches, multiple changes, and iterations and commits may be made. Then those changes can be integrated into the master timeline.

Does rebasing affect other branches?

This one is easy: If you're not pushing, you can only affect local things. If only local branch is rebased, should I commit to origin again after the rebase? This one has an embedded misconception. When you make commits, you're still only affecting local things.


1 Answers

When you rebase a branch, you have to rewrite the commits for any commit which is above the commits in the branch onto which you are rebasing. This is because one of the properties of a commit is its parent (or parents). When you rebase, you're changing the parent of the oldest local commit on your branch - and thus changing the commit hashes of all of your local commits, since this change bubbles up through the commits transitively.

Since you'd already pushed the branch, you should have merged in the source branch, rather than rebasing against it. It is possible to "force push" your new branch (using the -f flag), but a normal push won't work, because the integrity of the branches history will be disturbed. If you are collaborating with others on this branch, force pushing is a bad idea, as it will cause other collaborators to become very confused when their history suddenly doesn't match.

TL;DR - If you're not collaborating, push the branch using push -f. If you are, reset the branch to the previous state, and merge in the source branch, instead.

like image 90
Jason LeBrun Avatar answered Oct 12 '22 23:10

Jason LeBrun