leedm777
leedm777

Reputation: 24032

How can I squash commits on the current Git branch using the fewest steps?

When I'm working in a branch for a new feature, I find myself frequently in the situation where I want to rebase my branch off of master, and squash all my commits to a single commit. I haven't really found a smooth way to do it. Usually it involves some combination of:

# checkout master
git checkout master
git pull

# merge-squash my changes onto that
git merge --squash my-feature-branch

# force my branch to the squashed commit
git checkout my-feature-branch
git reset --hard master

# put master back to the way it was
git checkout master
git reset --hard HEAD^

I'd like something that runs a bit smoother than this, especially without the extra checkouts. Any suggestions?

Upvotes: 0

Views: 98

Answers (2)

user456814
user456814

Reputation:

One option you have is to simply use a soft reset. The advantage of this over an interactive rebase is that it will work efficiently for an arbitrary number of commits to squash (even hundreds or thousands):

git checkout feature
git reset --soft <commit-where-you-branched-off-master>
git commit -m "Your message here"
git rebase master

Upvotes: 2

pqnet
pqnet

Reputation: 6588

try

git rebase -i remote/master

then replace all pick with squash (or just s) except for the first one. Save and exit. Commits will be squashed, the editor will be shown again to edit the commit message. Push on master.

Upvotes: 1

Related Questions