Search code examples
gitgit-rebasegit-reset

`git rebase` vs `git reset --hard` ... are they the same for deleting the most recent N commits?


I understand that, in general, git rebase -i and git reset are used for very different things.

But in the context of deleting the most recent N commits, it seems to me they are (or can be) identical. I even did an experiment (with two different local copies of my repository) and as far as I can tell they are the same. I'm just posting this question in case someone out there can point out where maybe I am missing something. So here is what I did.

I had a need to remove the most recent 11 commits (never mind why).

I tried two ways of doing it, and they appear to be completely identical; just wondering if I am missing anything in that they are not really identical:

  1. git rebase -i HEAD~12
  • delete commit lines from the 11 commits that I don't want, keeping only the commit just before them (the one at the top of the interactive rebase file).
  • rebase succeeded
  1. git reset --hard HEAD~12

These two methods appear to have done exactly the same thing. Is that correct?

Thanks much. Just trying to understand git a little deeper than I do now.


Solution

  • One difference is in their treatment of a dirty tree (files modified but not committed).

    git reset --hard will trash said changes, while git rebase -i will ask you to stash them first.