Git log entry shows twice but with a different commit-id

Viewed 285

My git log showing like below.

3c61cc9 Commit A
dcf34cd Commit B
821cfe8 Commit A
7a5a58f Commit C
0987a57 Commit D
12721a1 Commit B

Here A & B each appear twice with different commit-ID, Also git diff 3c61cc9 821cfe8 is identical, no difference between each commit. So is there a way to remove these duplicates?

1 Answers

Assuming you like the way your 3c61cc9 and 821cfe8 look, just do

git reset 821cfe8

Then those top two commits will be "deleted". and you'll just have ACDB on your history.

BUT, if you want your history to be ABCD by deleting 821cfe8 and 12721a1 (depending on how you got into your current state, whether you've pushed all these commits, and your teams collaboration rules) you can try rebuilding your history like this:

  1. checkout the most-recent commit you care about - whatever came before 12721a1
  2. cherry-pick each new commit you want add
  3. reset your branch to point the the new "latest" commit

E.g.:

$ git checkout <commit right before 12721a1> 
$ git cherry-pick 0987a57 # (commit D)
$ git cherry-pick 7a5a58f # (commit C)
$ git cherry-pick dcf34cd # (commit B)
$ git cherry-pick 3c61cc9 # (commit A)
$ git checkout <branch>
$ get reset <commit you switched to 2 steps ago>

Note: none of this technically "deletes" the commits it just takes them off of that branch's history.

Related