开发者

How do I reverse a list of commits in git? [duplicate]

This question already has answers here: How to remove selected commit log entries from a Git repository while keeping their changes? (9 answers) Closed 2 years ago.

I have a source tree with git and I need to reverse all my changes that I have done since September, I know the id of the commit, and I can also generate a list of commits that I want to revert with git rev-list, the question is how do I feed that list to git so that it uncommits all of them?

Or if there is a better way to do this I would really app开发者_如何学JAVAreciate it.


If you want to destroy all changes since a given commit, you can reset the branch you are working on (for example master) to the commit. What git reset <commit> does is that it moves your HEAD to the given commit. The following commits get discarded.

A---B---C---D-master

then

git reset --hard <SHA1-of-B>

gives you:

A---B-master

--hard is for discarding your recent changes from the index and your local files.

If you have multiple branches, this might get a bit more complicated, but you get the idea.

Note that the discarded commits may be recoverable with git reflog, but you won't be likely to see them if you're not trying to. Eventually, the discarded commits will disappear for good, especially if you're using git gc now and then.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜