shrewdbeans shrewdbeans - 29 days ago 7
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

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

The problem I have now is that
develop
has a strange Git history. All of the commits from
feature
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

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.
run

git reset <SHA-1> --hard

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