10

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?

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
  • 3
    Which editor are you using to edit this list of commits? There's probably a global search and replace command available to you. For example in vim you might use `:%s/pick/squash/g` – Jonah Jan 29 '15 at 15:50
  • You can use the `GIT_SEQUENCE_EDITOR` variable to do that. Take a look at http://stackoverflow.com/a/27697274/974186 – René Link Jan 29 '15 at 15:55
  • Would you like to keep all of the commit messages? If you don't mind entering a new commit message, there is an easy way. – Sven Marnach Jan 29 '15 at 16:08

1 Answers1

10

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.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841