如何使用 Git Revert

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19032296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:02:57  来源:igfitidea点击:

How to use Git Revert

git

提问by Luke Puplett

How is git revertused?

如何git revert使用?

This might sound like a duplicate question but when people ask it, the response is often, use git resetas per Revert to a commit by a SHA hash in Git?

这可能听起来像一个重复的问题,但是当人们问它时,回答通常是,git reset按照在 Git 中使用 SHA 哈希还原到提交?

Then when someone asks how to use git resetpeople reply saying you should use git revertas per Git - how to rollback

然后当有人问如何使用时,git reset人们回复说你应该git revert按照Git使用——如何回滚

Before you know it, 8 different people appeared with their own unique ways to save the OP's ass, all of which is over your head.

不知不觉中,8 个不同的人出现了,他们用自己独特的方式来拯救 OP 的屁股,所有这些都超出了你的想象。

So lets try and stick the brief and write a Dummies Guide to git revert.

因此,让我们尝试坚持简要并编写一份傻瓜指南git revert

A scenario: you've committed twice to master and its bad. You've pushed and other people have your bad changes.

一个场景:你已经两次提交给 master 并且它很糟糕。你已经推动,其他人有你的不好的变化。

You want to undo it. It's not something you can hand-undo in code yourself, say some wizard or package manager changed tons of stuff all over the place - you just want to put it all back how it was.

你想撤销它。这不是您可以自己在代码中手动撤消的东西,例如某个向导或包管理器在整个地方更改了大量内容-您只想将其全部恢复原状。

This is what source control is all about. I'm sure its easy.

这就是源代码控制的全部内容。我确定这很容易。

Okay, you're going to use git revertbut how?

好的,您将要使用,git revert但是如何使用?

And after running git revertdo you have to do something else after? Do you have to commit the changes revert made or does revert directly commit to the repo or what??

跑完之后git revert你还需要做其他事情吗?您是否必须提交所做的更改还原或还原直接提交到 repo 或什么?

Obviously you'll need to push again and probably announce your balls-up to the team.

显然,您需要再次推动并可能向团队宣布您的球。

回答by AD7six

git revert makes a new commit

git revert 进行新的提交

git revertsimply creates a new commit that is the opposite of an existing commit.

git revert只需创建一个与现有提交相反的新提交。

It leaves the files in the same state as if the commit that has been reverted never existed. For example, consider the following simple example:

它使文件处于相同状态,就好像已恢复的提交从未存在过一样。例如,请考虑以下简单示例:

$ cd /tmp/example
$ git init
Initialized empty Git repository in /tmp/example/.git/
$ echo "Initial text" > README.md
$ git add README.md
$ git commit -m "initial commit"
[master (root-commit) 3f7522e] initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
$ echo "bad update" > README.md 
$ git commit -am "bad update"
[master a1b9870] bad update
 1 file changed, 1 insertion(+), 1 deletion(-)

In this example the commit history has two commits and the last one is a mistake. Using git revert:

在这个例子中,提交历史有两次提交,最后一次是错误的。使用 git revert:

$ git revert HEAD
[master 1db4eeb] Revert "bad update"
 1 file changed, 1 insertion(+), 1 deletion(-)

There will be 3 commits in the log:

日志中将有 3 次提交:

$ git log --oneline
1db4eeb Revert "bad update"
a1b9870 bad update
3f7522e initial commit

So there is a consistent history of what has happened, yet the files are as if the bad update never occured:

因此,发生的事情有一致的历史记录,但文件好像从未发生过错误更新:

cat README.md 
Initial text

It doesn't matter where in the history the commit to be reverted is (in the above example, the last commit is reverted - any commit can be reverted).

要还原的提交在历史记录中的哪个位置无关紧要(在上面的示例中,最后一个提交已还原 - 任何提交都可以还原)。

Closing questions

结束语

do you have to do something else after?

之后你还需要做其他事情吗?

A git revertis justanother commit, so e.g. push to the remote so that other users can pull/fetch/merge the changes and you're done.

一个git revert刚刚另一个承诺,所以如推到远程,以便其他用户可以拉/读取/合并的变化,你就大功告成了。

Do you have to commit the changes revert made or does revert directly commit to the repo?

您是否必须提交所做的更改还原或还原直接提交到存储库?

git revertisa commit - there are no extra steps assuming reverting a single commit is what you wanted to do.

git revert提交 - 假设还原单个提交是您想要做的,则没有额外的步骤。

Obviously you'll need to push again and probably announce to the team.

显然,您需要再次推动并可能向团队宣布。

Indeed - if the remote is in an unstable state - communicating to the rest of the team that they need to pull to get the fix (the reverting commit) would be the right thing to do :).

确实 - 如果遥控器处于不稳定状态 - 与团队其他成员沟通他们需要拉动以获取修复(恢复提交)将是正确的做法:)。

回答by Johnny Z

Use git revert like so:

像这样使用 git revert:

git revert <insert bad commit hash here>

git revert <insert bad commit hash here>

git revertcreates a new commit with the changes that are rolled back. git reseterases your git history instead of making a new commit.

git revert使用回滚的更改创建新提交。git reset删除您的 git 历史记录而不是进行新的提交。

The steps after are the same as any other commit.

之后的步骤与任何其他提交相同。

回答by ToxicFrog

The reason resetand reverttend to come up a lot in the same conversations is because different version control systems use them to mean different things.

在相同的对话中出现很多的原因resetrevert趋势是因为不同的版本控制系统使用它们来表示不同的东西。

In particular, people who are used to SVN or P4 who want to throw away uncommitted changes to a file will often reach for revertbefore being told that they actually want reset.

特别是,习惯了 SVN 或 P4 的人想要丢弃对文件的未提交更改,通常会revert在被告知他们实际上想要reset.

Similarly, the revertequivalent in other VCSes is often called rollbackor something similar - but "rollback" can also mean "I want to completely discard the last few commits", which is appropriate for resetbut not revert. So, there's a lot of confusion where people know what they want to do, but aren't clear on which command they should be using for it.

类似地,revert其他 VCS 中的等价物通常被称为rollback或类似的东西 - 但“回滚”也可能意味着“我想完全丢弃最后几次提交”,这适用于reset但不适用于revert. 因此,当人们知道他们想要做什么,但不清楚他们应该使用哪个命令时,存在很多混乱。

As for your actual questions about revert...

至于你关于恢复的实际问题......

Okay, you're going to use git revert but how?

好的,您将使用 git revert 但如何使用?

git revert first-bad-commit..last-bad-commit

git revert first-bad-commit..last-bad-commit

And after running git revert do you have to do something else after? Do you have to commit the changes revert made or does revert directly commit to the repo or what??

在运行 git revert 之后你还需要做其他事情吗?您是否必须提交所做的更改还原或还原直接提交到 repo 或什么?

By default, git revertprompts you for a commit message and then commits the results. This can be overridden. I quote the man page:

默认情况下,git revert提示您输入提交消息,然后提交结果。这可以被覆盖。我引用手册页

--edit

With this option, git revert will let you edit the commit message prior to committing the revert. This is the default if you run the command from a terminal.

--no-commit

Usually the command automatically creates some commits with commit log messages stating which commits were reverted. This flag applies the changes necessary to revert the named commits to your working tree and the index, but does not make the commits. In addition, when this option is used, your index does not have to match the HEAD commit. The revert is done against the beginning state of your index.

This is useful when reverting more than one commits' effect to your index in a row.

- 编辑

使用此选项,git revert 将允许您在提交还原之前编辑提交消息。如果您从终端运行命令,则这是默认设置。

--不提交

通常,该命令会自动创建一些带有提交日志消息的提交,说明哪些提交已被还原。此标志应用将命名提交还原到您的工作树和索引所需的更改,但不进行提交。此外,使用此选项时,您的索引不必与 HEAD 提交匹配。恢复是针对索引的开始状态完成的。

当连续将多个提交的效果恢复到您的索引时,这很有用。

In particular, by default it creates a new commit for each commit you're reverting. You can use revert --no-committo create changes reverting all of them without committing those changes as individual commits, then commit at your leisure.

特别是,默认情况下,它会为您要还原的每个提交创建一个新提交。您可以使用revert --no-commit来创建更改以还原所有更改,而无需将这些更改作为单独的提交进行提交,然后在闲暇时提交。

回答by nnsense

The question is quite old but revert is still confusing people (like me)

这个问题已经很老了,但恢复仍然让人们感到困惑(比如我)

As a beginner, after some trial and error (more errors than trials) I've got an important point:

作为初学者,经过反复试验(错误多于试验)后,我得到了一个重要的观点:

  • git revertrequires the id of the commit you want to removekeeping it into your history

  • git resetrequires the commit you want to keep, and will consequentially remove anything after that from history.

  • git revert需要您要删除的提交的 ID 将其保留在您的历史记录中

  • git reset需要您要保留的提交,并且因此会从历史记录中删除此后的任何内容。

That is, if you use revertwith the first commit id, you'll find yourself into an empty directory and an additional commit in history, while with reset your directory will be.. reverted back to the initial commit and your history will get as if the last commit(s) never happened.

也就是说,如果您使用revert第一个提交 ID,您会发现自己进入一个空目录并在历史记录中进行了额外的提交,而重置您的目录将......恢复到初始提交,您的历史记录将变得好像最后一次提交从未发生过。

To be even more clear, with a log like this:

更清楚的是,使用这样的日志:

# git log --oneline

cb76ee4 wrong
01b56c6 test
2e407ce first commit

Using git revert cb76ee4will by defaultbring your files back to 01b56c6 and will add a further commit to your history:

默认情况下,使用git revert cb76ee4会将您的文件带回 01b56c6 并将进一步提交到您的历史记录:

8d4406b Revert "wrong"
cb76ee4 wrong
01b56c6 test
2e407ce first commit

git reset 01b56c6will instead bring your files back to 01b56c6 and will clean up any other commit after that from your history :

git reset 01b56c6而是将您的文件带回 01b56c6 并从您的历史记录中清除任何其他提交:

01b56c6 test
2e407ce first commit

I know these are "the basis" but it was quite confusing for me, by running reverton first id ('first commit') I was expecting to find my initial files, it taken a while to understand, that if you need your files back as 'first commit' you need to use the next id.

我知道这些是“基础”,但对我来说很困惑,通过revert在第一个 id(“第一次提交”)上运行我期待找到我的初始文件,花了一段时间才明白,如果你需要你的文件回来作为“第一次提交”,您需要使用下一个 ID。

回答by jrc16

I reverted back a few commits by running 'git revert commit id' such as:

我通过运行“git revert commit id”恢复了一些提交,例如:

git revert b2cb7c248d416409f8eb42b561cbff91b0601712

Then i was prompted to commit the revert (just as you would when running 'git commit'). My default terminal program is Vim so i ran:

然后我被提示提交还原(就像你在运行 'git commit' 时所做的那样)。我的默认终端程序是 Vim,所以我运行:

:wq 

Finally i pushed the change to the repository with:

最后,我将更改推送到存储库:

git push