gitgit-mergesquash

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?


Solution

  • 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>