简体   繁体   English

如何在git中反转提交列表?

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

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? 我有一个带有git的源树,我需要撤消自9月以来所做的所有更改,我知道提交的ID,并且我还可以生成要使用git rev-list还原的提交列表,问题是我如何将列表提供给git,以便取消提交所有列表?

Or if there is a better way to do this I would really appreciate it. 或者,如果有更好的方法可以做到这一点,我将不胜感激。

This has already be answered by for your sake, found with a simple search: 为您着想,已经通过简单的搜索找到了答案:

How to remove selected commit log entries from a Git repository while keeping their changes? 如何在保留其更改的同时从Git存储库中删除选定的提交日志条目?

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. 如果要破坏自给定提交以来的所有更改,则可以将正在处理的分支(例如master)重置为该提交。 What git reset <commit> does is that it moves your HEAD to the given commit. git reset <commit>所做的是将HEAD移至给定的提交。 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. --hard用于丢弃索引和本地文件中的最新更改。

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. 请注意,使用git reflog可以恢复被丢弃的提交,但是如果您不尝试的话,您将不太可能看到它们。 Eventually, the discarded commits will disappear for good, especially if you're using git gc now and then. 最终,被丢弃的提交将永久消失,特别是如果您不时使用git gc话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM