Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git squash history after merge

I merged an upstream of a large project with my local git repo. Prior to the merge I had a small amount of history that was easy to read through, but after the merge a massive amount of history is now in my repo. I have no need for all the history commits from the upstream repo.

There have been other commits made after this upstream merge that I would like to keep. How do I squash all that history that was merged from the upstream into one commit while keeping the commits made after the upstream merge?

like image 457
E-rich Avatar asked Dec 26 '12 17:12

E-rich


People also ask

Does squash and merge delete history?

Squash merging condenses the history of changes in your default branch, so it's important to work with your team to decide when you should squash merge or when you want to keep the full commit history of a topic branch.

Can you squash after merge?

To "squash" in Git means to combine multiple commits into one. You can do this at any point in time (by using Git's "Interactive Rebase" feature), though it is most often done when merging branches.

Does git merge keep history?

In the Conceptual Overview section, we saw how a feature branch can incorporate upstream changes from main using either git merge or git rebase . Merging is a safe option that preserves the entire history of your repository, while rebasing creates a linear history by moving your feature branch onto the tip of main .

How do I squash history in git?

The Ways of Achieving Git Squash The easiest one is to take advantage of Git repository servers like GitHub that typically have this built in within the pull/merge request feature. Usually you just need to tick a box saying you want to squash or to choose squash merge strategy and you're good to go.


2 Answers

I was able to squash several commits after multiple merges from the master branch using the strategy found here: https://stackoverflow.com/a/17141512/1388104

git checkout my-branch            # The branch you want to squash git branch -m my-branch-old       # Change the name to something old git checkout master               # Checkout the master branch git checkout -b my-branch         # Create a new branch git merge --squash my-branch-old  # Get all the changes from your old branch git commit                        # Create one new commit 

You will have to force an update if you need to push your squashed branch to a remote repository that you have previously pushed to, e.g. git push origin my-branch -f

like image 118
Aaron Swan Avatar answered Sep 27 '22 18:09

Aaron Swan


The solution I ended up using was to manually recreate the history. I did this mainly because I didn't want to spend too much time looking for an elegant solution and there wasn't that much history (around 30 commits I'd have to manually merge).

So, I created a branch before I merged the huge upstream:

git checkout -b remove-history-fix <commit ID before merge> 

Then re-merged the upstream using the --squash option.

git merge --squash <upstream tag> 

Then manually cherry-picked the commits after the merge from the old branch (the one with the huge amount of upstream history).

git cherry-pick <commit ID> 

After all those commits were merged into my remove-history-fix branch, I removed the branch with the upstream history.

git branch -D <upstream-history-branch> 
like image 37
E-rich Avatar answered Sep 27 '22 18:09

E-rich