如何使用Git Revert [英] How to use Git Revert

查看:383
本文介绍了如何使用Git Revert的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能听起来像一个重复的问题,但当人们提出这个问题时,响应通常是,使用 git reset 按照 git reset 人们回复说你应该使用 git revert 按照 Git - 如何回滚



在你知道它之前,8个不同的人用他们自己独特的方式来保存OP的屁股,所有这些都超出了你的想象。



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

一个场景:你已经犯了两次错误的主人。你推动了,其他人也做了不好的改变。



你想撤消它。这不是你可以自己手动撤销代码的东西,比如说某个向导或者包管理器在整个地方改变了大量的东西 - 你只是想把它全部改回来。



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



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



并且在运行 git revert 之后,您是否还需要做其他事情?你必须提交所做的更改或恢复直接提交回购或什么?



显然你需要再次推送,并且可能会向球队宣布你的球。

解决方案

git revert创建一个新的提交



git revert 这与现有提交相反。



它使文件保持相同的状态,就好像已经恢复的提交不存在一样。例如,请考虑以下简单示例:

  $ cd / tmp / example 
$ git init
初始化/tmp/example/.git/中的空Git存储库
$ echo初始文本> README.md
$ git add README.md
$ git commit -minitial commit
[master(root-commit)3f7522e] initial commit
1 file changed,1插入(+)
创建模式100644 README.md
$ echo错误更新> README.md
$ git commit -ambad update
[master a1b9870] bad update
1个文件已更改,1个插入(+),1个删除( - )

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

$ $ p $ $ $ c $ git revert HEAD
[master 1db4eeb]还原坏的更新
1文件已更改,1个插入(+),1个删除( - )

日志:

  $ git log --oneline 
1db4eeb恢复坏的更新
a1b9870坏的更新
3f7522e初始提交

因此,发生了一致的历史记录,但文件是好像坏的更新从未发生过:

  cat README.md 
初始文本

在历史记录中,要提交的提交位置无关紧要(在上例中,最后一次提交被还原 - 任何提交可以回复)。



关闭问题




你必须做些什么


A git revert >另一个提交,例如推送到远程,以便其他用户可以拉/取/合并更改,你就完成了。


你必须提交


git revert 变更还原已完成或还原直接提交到回购? 是一个提交 - 没有额外的步骤假设恢复单个提交是你想要做的。


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


确实 - 如果遥控器处于不稳定状态 - 到团队的其他成员,他们需要获得修复(恢复提交)才是正确的做法:)。


How is git revert used?

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

Then when someone asks how to use git reset people reply saying you should use git revert as per Git - how to rollback

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.

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

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

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 revert but how?

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??

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

解决方案

git revert makes a new commit

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

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 HEAD
[master 1db4eeb] Revert "bad update"
 1 file changed, 1 insertion(+), 1 deletion(-)

There will be 3 commits in the log:

$ 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 revert is just another commit, so e.g. push to the remote so that other users can pull/fetch/merge the changes and you're done.

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

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

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 :).

这篇关于如何使用Git Revert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆