Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

`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.

question from:https://stackoverflow.com/questions/65942954/git-rebase-vs-git-reset-hard-are-they-the-same-for-deleting-the-most-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...