Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run git rebase interactive mode to remove duplicate commits

I made a mistake when I rebase to a recent commit. ( I forgot to git fetch --all first, then rebase), and I've committed and pushed to the remote branch since. Now I did the rebase properly by fetching first, solved the conflicts, and pushed to the remote branch. Now, it seems all my recent commits are showing up twice. What I would like to do is to use git rebase interactive mode, pick all the commits that I want, then rebase properly to the commit sha code.

Is this the way to do this? and if I start git rebase -i, which sha code should I use, the original branching point sha code? or the most recent sha code?

like image 553
David Zhao Avatar asked Apr 04 '12 20:04

David Zhao


People also ask

How do I use git interactive rebase?

You can run rebase interactively by adding the -i option to git rebase . You must indicate how far back you want to rewrite commits by telling the command which commit to rebase onto. Remember again that this is a rebasing command — every commit in the range HEAD~3..

How do I type in interactive rebase?

Just right-click on the commit you want to edit and select Interactively Rebase from Here… Select reword on the commit and click Start Rebasing. Edit the commit message and click Resume Rebasing.

How do I remove interactive rebase?

Interactive rebase for the last 31 commits (it doesn't hurt if you pick way too many). Simply take the commits that you want to get rid of and mark them with "d" instead of "pick". Now the commits are deleted effectively undoing the rebase (if you remove only the commits you just got when rebasing).


2 Answers

Give git rebase the number of commits back in time. For 10 commit back in history:

git rebase -i HEAD~10

Then simply delete the lines for the commits you want to remove in the text editor that is shown. Saving the file and exiting will start the rebase and the lines that were removed will no longer be in the history once the rebase has completed.

like image 151
ralphtheninja Avatar answered Oct 10 '22 02:10

ralphtheninja


I successfully removed the duplicate commits. Here is what I did: hard reset to the branching point, pull from origin, start git rebase -i SHACODE (original branching point), pick the commits that I want to keep, git rebase to the current SHACODE.

like image 27
David Zhao Avatar answered Oct 10 '22 02:10

David Zhao