November 4th, 2020

Git : Duplicate commits

One situation that can cause duplication of commits is doing a rebase on a branch which changes commits that are already in trunk.

The rebase doesn't change the existing commit, it actually copies the commit. This means there is now an entirely new commit in the history when compared to trunk.

When merging branch back to trunk, Git needs to resolve the difference in history between trunk and the branch. The resolution is to add all commits from the new commit onwards onto the end of trunk's commit history.

Once the duplicate commits are in trunk they are there to stay. Doing a rebase of trunk, and dropping the duplicate commits, might work but it is risky. My preference would be to have a messy commit history. Doing a rebase on trunk and re-writing history has the potential to create a lot more problems.

Rebase comes with the warning to not rebase commits that someone else might have. In this case, someone else did have the commits - trunk.