Search code examples
gitgit-rebasegit-squash

Is there a way to non-interactively rebase and squash everything in my feature branch?


Sometimes I have a bunch of small commits in a feature branch, and I'd like to just squash them all together, but not merge into the parent branch yet.

I know I can (in this example parent branch is master):

git rebase -i master

and then tag all of the commits after the first as "squash".

Is there a way to get the same result non-interactively?

Essentially I want to create a new commit whose tree is identical to the tree now at the head of my feature branch and whose parent is the ancestor commit that I've specified (eg: master), and then change the feature branch to point at that new commit. There should never be any conflicts (I occasionally get some with rebase -i), and with -m should be completely non-interactive. Ideally, without -m the commit message should default to a concatenation of the commits being squashed.

How can I do this?


Solution

  • My take to squash is to use git reset --soft. Say you want to squash the last 5 commits:

    git reset --soft HEAD~5
    git commit -m "Feature X"
    

    And if you also want to rebase all of that done in a (almost) single shot, you can still do it with git reset --soft

    How to squash/rebase in a single shot