Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git remove duplicate commits

Tags:

git

After some playing with remotes I ended up with all my commits being doubled. E.g. instead of

C3107
..
C3
C2
C1

I got

C3107
C3107
..
C3
C3
C2
C2
C1
C1

where doubled commits has same names but different hashes. The problem is I noticed it too late and not I added a good bunch of commits on top of it.

Is there a way to remove duplicate commits and not not loose ones I added over?

P.S.: If it will help I have a copy of a repository before my experiments with remotes.

Thanks a lot in advance.

UPDATE As many of you asked here is how ended up like this: I have a repo R1 then I created another one R2. At my local copy which was up to date with R1 I changed origins to R2 and tried to push but some large files were rejected by github. So I did git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' which made git think repositories are different. Then I pushed all to R2 made some commits and decided to switch back to R1 changed origin again and pushed. Then I added some more commits to R1.

like image 200
user487772 Avatar asked Dec 16 '22 02:12

user487772


2 Answers

With a bit of shell magic, grepping and a lot of confidence, then the answer is probably "yes". But I'd be too leery of completely messing things up to squash over 3000 commits with a single command!

However, you can do it (moderately) interactively by using interactive rebasing. It's a bit laborious, but you have good control over what's happening and you get good feedback from git.

like image 172
Paul Hicks Avatar answered Dec 24 '22 09:12

Paul Hicks


You can run the following command:

git rebase -i <commit id where you want to start squashing>

This will bring up an interactive session. Switch everything but the first commit on the stack to fixup or squash.

pick 07520cd Caught file exists issue. # this is last commit
fixup 3b71b9f Added README.            # fixup will squash the commit

# Rebase b041966..3b71b9f onto b041966
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

I have a git koan (koan 8) that walks you through git rebase -i.

like image 40
joel3000 Avatar answered Dec 24 '22 08:12

joel3000