Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retroactively squashing a subtree merge

Tags:

git

I merged an external library into my project using the subtree merge strategy. I didn't pass the --squash option to 'git merge', so I now have the entire library history (which is huge) bloating the repository. How can I retroactively squash the merge?

My situation is:

Z--Y--..--B--A
              \ 
           D---E---F---G master

I would like to get to:

           D---E---F---G master

Where E is the commit containing the merge.

I guess the solution will involve 'git rebase' but I'm not sure how to use it properly in this case.

like image 462
wanderingbear Avatar asked Jun 06 '11 08:06

wanderingbear


2 Answers

Remember that if you already published E or later commits in master to remote, then DON'T do rebase on them, as this will break other's code and you'll need to manually fix their repositories too.

Rebase creates new revisions, it can't magically modify existing (that's why it uses SHA1, so you're sure that your commit contain what generated this particular SHA1 and is not modified)

But if you're prepared to it, then proceed with my example.

  1. First you need to go back to commit right before you merged library, create named branch from there, let's say 'lib-squash'
  2. git merge --squash library; git commit
  3. git checkout master
  4. git rebase -i -p --onto lib-squash <sha1 old merge commit>
  5. optionally git branch -D library and git branch -d lib-squash

This leaves you with clean history, but you just rewrote history, you need to push with --force flag, and then every person would need to fix their repository:

  1. git fetch
  2. git checkout master
  3. git reset --hard origin/master, assuming you don't have other local commits than in remote repository
  4. if you have any local commits, you need to "rebase onto" these commits too.

Unfortunately I haven't found article about detaching historical commits without affecting future commits.

like image 190
MBO Avatar answered Nov 15 '22 04:11

MBO


Rebase should help you with it. This article should give you enough pointers to sort it out

like image 23
Wolfwyrd Avatar answered Nov 15 '22 05:11

Wolfwyrd