Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to squash a lot of commits automatically?

Tags:

git

squash

commit

I have a lot of commits that I want to squash together into one commit. Of course I may replace pick with squash for every commit, but I have a hundreds commits. Is there a way to do this automatically?

like image 956
Bartłomiej Semańczyk Avatar asked Jan 29 '15 15:01

Bartłomiej Semańczyk


People also ask

How could you squash multiple commits together without using?

You can do this fairly easily without git rebase or git merge --squash . In this example, we'll squash the last 3 commits. Both of those methods squash the last three commits into a single new commit in the same way. The soft reset just re-points HEAD to the last commit that you do not want to squash.

How do you squash old commits in one?

The first one is to use the git merge command with the squash flag (two dashes there). And the second one is through an interactive rebase. The first option (merge) is very simple to perform.


1 Answers

If you have a sequence of commits

... - C1 - C2 - C3 - C4 - C5 <- HEAD

and you want to squash C2 to C5 into a single commit, you can reset your branch to C1 while keeping the state of your working directory and staging area, ans then commit again:

git reset --soft C1
git commit

This will require you to re-enter a commit message. You can of course use git log before resetting and copy the parts of the commit messages you want to keep.

If you want to squash a feature branch into a single commit ontop of the master branch, another option is to use the --squash option to git merge.

like image 155
Sven Marnach Avatar answered Oct 13 '22 21:10

Sven Marnach