2

I accidentally ended up creating duplicates of all of my projects commits and pushing to remote, my project jumped from 500 commits to 1000. I've checked all the links to remove duplicate commits, but none of these make sense for me. If I attempt to rebase, I simply get a merge conflicts every single commit , and I'm not about to spend a couple hours resolving conflicts since the beginning of time. My history looks like:

enter image description here

Is there anyway to just go in and forcefully delete duplicate commits? I really don't want to spend hours rebasing from ~1000 commits ago manually squashing every duplicate commit.

Even better, is there a way I can just undo this? This was the result of a filter-branch command that I didn't realize duplicated every single commit

Community
  • 1
  • 1
Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177

1 Answers1

3

You can try rebasing but you said you had issues with it, or you can try a sort of an undo with git reflog. I leave both options bellow:

Undo with Git Reflog

You mentioned that you would like to undo the results of the filter command, so you can try to run:

> $ git reflog

d363d7c HEAD@{0}: commit: minor typo
87c9470 HEAD@{1}: pull: Fast-forward
a74a14e HEAD@{2}: checkout: moving from clojure-script-begginer to master
ce79d4e HEAD@{3}: commit: article for review
a74a14e HEAD@{4}: checkout: moving from master to clojure-script-begginer
a74a14e HEAD@{5}: pull: Fast-forward
14ec10b HEAD@{6}: checkout: moving from cors to master
e71cf74 HEAD@{7}: commit: Config URL commented out

If you see a list like this and you can notice an entry before the filter command was ran you can just reset your branch to that point:

> git reset --hard <COMMIT ID>

COMMIT ID would be any number on the first column of git reflog

 Rebasing

1. Create another branch

Don't execute any of the steps in the branch you currently have the 100 commits, as we're going to go into dangerous zone quite for quite a bit.

Assuming you're at master:

> git checkout -b fixing-duplicates

 2. Interactive rebase to delete duplicates

Run:

> git rebase -i HEAD~1000

A text editor (the default one) will open and it will show all previous 100 commits. Delete the lines of the duplicated commits until you only have the commits you want. Save and exit.

At this point I don't expect you to have rebase conflicts.

3. Push the branch

Just push the branch upstream:

> git push origin fixing-duplicates

4. Check if the branch is ok

Give the branch a look and check if this is really what you want. if it is, just run:

> git push origin fixing-duplicates:master -f

If your GitHub instance prevents force pushing to the default branch, change the default branch to fixing-duplicates and run the command again. Then just switch the default branch back to master.

Let me know if this helped.

bitoiu
  • 6,893
  • 5
  • 38
  • 60