Laurence Gonsalves
Laurence Gonsalves

Reputation: 143234

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?

Upvotes: 1

Views: 358

Answers (1)

eftshift0
eftshift0

Reputation: 30242

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

Upvotes: 4

Related Questions