Git branch diverged


I am new to git, although I understand its concept I don’t think I understand its practice as well.

I was given a branch uitest to work on, because I may have not done the push, commit, pull correctly, I have now diverged the branch.

Because I am new here, and I also don’t want to override other devs’ code as my code or changes are simply experimental to get used to git and how it works I wouldn’t mind discarding my changes as I have a copy of everything I need to re-do everything.

$ git status
# On branch uitest
# Your branch and 'origin/uitest' have diverged,
# and have 47 and 6 different commits each, respectively.
nothing to commit (working directory clean)

How would I "undiverge"? discard my changes and pull the most recent changes from the latest changes and then continue working without messing other peoples work.

Also, as I am new to this, be a little descriptive about your answer as it may not make much sense to me.


There a few ways.


First, you could simply merge origin/uitest, but that doesn’t leave a clean history in that it introduces what looks like a branch and merge even though it was meant to be the same branch all along. I believe Linus liked to call these kind of merge commits “pointless”. Unfortunately, this is also the easiest approach.


Rebasing tends to be a more advanced topic and can introduce a whole host of other problems if you aren’t careful. That said, it’s also a great way to get clean history without the pointless commits. In this case, you could do:

git rebase origin/uitest

from you uitest branch, and it would take all the work you did, and put it on top of the work in origin/uitest.

There are a couple of catches though. First, if you’ve merged any other branches into your branch, git rebase will drop them. You need to pass the -p flag to keep any merge commits you introduced, but that’s not always the right thing to do either. If all you did was commit your own changes, you should be fine with the command I’ve given about.

Second, any time you use rebase you should always remember to never rebase public commits. Rebase will change the commit ids because the parents have changed. If people are merging you work, and you rebase it, they’ll end up with several copies of your commits in the history–which is badness. So be careful about when you apply this technique.

All that said, you want to make git rebase your friend. It’s a powerful and useful tool, but as with any power tool, it can be dangerous.

Discard your work

If you simply want to discard what you’ve done, you can run:

git reset --hard @{u}


git reset --hard origin/uitest

That will reset your uitest branch to match the upstream or origin/uitest. It will discard your commits.

Personally, I’d rebase the work or at least give it a try. If it fails, or becomes to complicated from merge conflicts, you can always abort with git rebase --abort, and then fallback to merge or discarding your changes (although merging will likely show you the same merge conflicts).

Answered By – John Szakmeister

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published