Search code examples
gitsquash

Squashing all of my commits (including merges) into one commit without altering history


Say I have 100 commits in my branch that I've been working on for 3 weeks. Occasionally (every day, really) I pull from origin/master and merge it into my branch.

How could I (easily) go about squashing all of my commits into one commit without messing up history? If I squash all of my commits into one somehow, would I destroy the merged origin/master pulls when my pull request gets moved into origin/master?


Solution

  • "Squashing" and "preserving history" are approximately direct opposites in terminology.

    If you mean that you want to make a single commit that includes only your changes and not the ones from upstream master, you would probably want to rebase onto origin/master and then squash from there. You could do all of this from a single invocation of interactive rebase:

    git fetch origin
    git rebase -i origin/master
    

    and then change all of the lines after the first from pick to squash.