shrewdbeans shrewdbeans -4 years ago 216
Git Question

Duplicate commits after rebase have been merged into the develop branch

I recently pulled down a remote branch that another developer was working on, let's call it

. I then made the mistake of doing a rebase onto
- the main working branch - which I've now come to know is something that you shouldn't do. The
branch has since been merged into

The problem I have now is that
has a strange Git history. All of the commits from
seem to be duplicated, they appear twice. However, they have different commit IDs.

My history looks a bit like this now (IDs are for demo purposes):

0007 commit from feature #3 <--- these commits are duplicated
0006 commit from feature #2
0005 commit from feature #1
0004 different commit from another branch #2
0004 different commit from another branch #1
0002 commit from feature #3
0002 commit from feature #2
0001 commit from feature #1

I've made a silly mistake! Is there anything I can do about this? The history looks ugly, but all the correct code seems to be there. Can I remove the duplicated commits? Or is there any other way to clean up the history?

Please write your answer for a less experienced Git user.

Answer Source

Use the git reflog to revert your changes.

Read all about it in here (how to recover previous head/ how to undo changes):

What to do?

Type git reflog and find out the "last good" sha-1 which you want to go back to.

git reset <SHA-1> --hard

And you are back in the previous commit before you did your mistake.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download