alper
alper

Reputation: 3410

What is the difference between `git rebase -i HEAD~N` and `git reset --soft HEAD~N`?

We can squash the last N commits in Git. As I understand, we can squash last N commits using git rebase -i HEAD~N or git reset --soft HEAD~N.

In the answers for this question (Squash my last X commits together using Git) most upvoted answer advices to use git reset --soft HEAD~N, which is not the accepted one. Accepted answer recommends git rebase -i HEAD~N. Hence I get confused, which one would be prefered to be used.

How those these approaches differ from each other? Which one is recommended or safe to be used?

Upvotes: 8

Views: 2151

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521239

Both commands you gave can be used to rewrite the history of a branch. The soft reset option:

git reset --soft HEAD~N

will move the HEAD pointer of the branch back N commits, while simutaenously placing the work from those commits into the stage. If you were to then commit from this point, you would have squashed the commits into a single new commit.

The rebase option:

git rebase -i HEAD~N

will bring up a window showing the previous N commits. On each line, for each commit, you may choose to pick (keep), squash, reword, etc. You could achieve the same result as the soft reset option as above by choosing squash for the commits in question. However, interactive rebase is much more powerful than this.

Which method you use depends on the complexity of your operation. We might view the soft reset as a poor man's version of interactive rebase, the latter which is heavy duty.

Upvotes: 3

Mureinik
Mureinik

Reputation: 311308

git rebase -i allows you to interactively rebase the commit range - you can choose which commits to take, which to drop, reorder them, manually intervene at some point, and, as you noted, squash them etc.

git reset --soft just removes all the commits, leaving the index intact.

Upvotes: 2

Related Questions