Search code examples
gitversion-controlrebase

Remove duplicate commits introduced after bad rebase(s)


I have 2 branches, master & feature. Master occasionally receives small tweaks that are needed to go live quickly. Once these tweaks are done, master is rebased into feature so that feature is kept up to date.

The files changed on master are generally not related to the bulk of the development on feature but I've been getting a high number of complicated 3 way merge conflicts.

After looking at the commit log of feature, I've found the problem to be several duplicates and I'm attempting to solve it with an interactive rebase.

My question is:

Is this the best solution for the problem, and if so can I just drop the duplicates and essentially build the history how I expect it to look? Also which duplicates would I drop, higher(older original) or lower (newer duplicates).

Abstract listing taken from git rebase master -i:

pick 0eb277c Commit A
pick ced2556 Commit B
pick 640e049 Commit C
pick b248ff7 Commit D
pick 9903094 Commit E
pick ebc279d Commit A
pick 313385b Commit F
pick 0d55178 Commit G
pick c8f09f9 Commit H
pick e877be3 Commit I
pick 9859aa0 Commit J
pick c3c8e0f Commit K
pick 8abc68c Commit L
pick 84a5c89 Commit A
pick 30570e4 Commit B
pick 937ff2f Commit C
pick 8e6d911 Commit D
pick 1dd3a09 Commit E
pick fe79288 Commit F
pick 9e790bb Commit G
pick 0924916 Commit H
pick 90d59d7 Commit I
pick ba06c55 Commit J
pick 7452fad Commit K  ** Dupes end here** and features then has 30+ more commits.
pick d1dca3d Commit M
pick 6c85f76 Commit N
pick ad53b78 Commit O
pick f166471 Commit P

Solution

  • To remove the duplicate commits you must go through the list generated by git rebase master -i and drop the duplicates by navigating up and down the list using the arrow keys pressing d on the commits you want to drop.

    (master can be replaced with a commmit hash or any valid re-base point)

    Once you have deleted all the duplicates and have a commit list that looks how you expect the history to look, exit the rebase screen by pressing esc and typing !wq and pressing enter.

    The re-base will then continue and apply the list of commits you picked onto your re-base point.