git 中的 svn revert 等价物是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21693295/
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-19 09:42:59  来源:igfitidea点击:

What's the svn revert equivalent in git?

gitsvn

提问by Richard77

I'm trying to find the equivalent of svn Revertin Git. In svn, when I rigth-click a file then click revert, that will undo all the local edits. the file goes back to the last commit. I can't find the exact same command in Git. Since I'm new, I don't want to mess-up my work.

我试图svn Revert在 Git 中找到等价物。在 中svn,当我右键单击一个文件然后单击 时revert,这将撤消所有本地编辑。该文件返回到最后一个commit. 我在 Git 中找不到完全相同的命令。因为我是新人,我不想搞砸我的工作。

Let say I made a commit Init. Then I made some changes. Now I'd like to go back to the Initstate.

假设我做了一个提交Init。然后我做了一些改变。现在我想回到Init状态。

Thanks for helping

谢谢你的帮助

采纳答案by John Szakmeister

Because of the staging area, it's not quite as simple as an svn revert. Depending on what you've done, and what your goal is, you need to use either git checkoutor git reset. You need to be careful with git resetthough, as you can re-write history and lose work.

由于暂存区的原因,它不像svn revert. 根据您所做的工作以及您的目标,您需要使用git checkoutgit resetgit reset但是,您需要小心,因为您可能会重写历史记录并丢失工作。

Let's say you have the commit graph:

假设您有提交图:

A <-- B <-- C <-- D

And you're currently at Dwith some changes. To simply discard all changes (staged or not), you can use git reset --hard HEADor git reset --hard D. Both will reset the state of your working copy.

而您目前正在经历D一些变化。要简单地放弃所有更改(暂存或不暂存),您可以使用git reset --hard HEADgit reset --hard D。两者都将重置您的工作副本的状态。

If you want the state of your working copy to be identical to A, but not lose commits Band C, then you should use: git checkout A -- .from the top of your working copy. That'll set the state of your working copy to the contents of A. At this point, your working tree is dirty, so you'll need to commit to record the fact that you wanted to go back. The commit graph at this point would be:

如果您希望您的工作副本的状态与 相同A,但不丢失提交BC,那么您应该使用:git checkout A -- .从您的工作副本的顶部。这会将您的工作副本的状态设置为A. 此时,您的工作树是脏的,因此您需要承诺记录您想要返回的事实。此时的提交图将是:

A <-- B <-- C <-- D <-- A'

Where A'is a new commit (with a different id) that brings your work back to the equivalent of commit A.

A'新的提交(具有不同的 id)在哪里,它会将您的工作恢复到与 commit 等效的状态A

If you want to lose commit Band C, you can use git reset --hard A. This will move your branch pointer back to commit A. You are now re-writing history, so be careful. If you're working with other team members on this branch, you don't want to do this. If it's your own branch, you likely need to use git push -f, where the -fstands for --force, when pushing it to your remote repo. You also need to make sure that you've set push.defaultto either currentor upstreamto avoid pushing all matching branches and accidentally rewinding someone's work.

如果你想丢失 commit Band C,你可以使用git reset --hard A. 这会将您的分支指针移回 commit A。你现在正在重写历史,所以要小心。如果您与此分支的其他团队成员一起工作,您不想这样做。如果它是您自己的分支,则在将其推送到您的远程存储库时,您可能需要使用git push -f,其中-f代表--force。您还需要确保您已设置push.defaultcurrentupstream避免推送所有匹配的分支并意外倒回某人的工作。

FWIW, I wrote up a blog post about reverting changes in Gitseveral years ago. It's still relevant today. You may find it useful.

FWIW,几年前我写了一篇关于在 Git 中恢复更改博客文章。它今天仍然有意义。你可能会发现它很有用。

回答by Daniel

As long as the files are not staged I usually use

只要文件没有暂存我通常使用

git checkout -- .

You can also use

你也可以使用

git checkout HEAD -- path/to/some/file.txt

to checkout a single file from HEAD, or any other commit

HEAD或任何其他提交签出单个文件

回答by Carl Norum

git reset --hard Init

Will get your HEADback to Init. You may want to make a branch or tag first, to save whatever other work you'd done.

会让你HEAD回到Init. 您可能想先创建一个分支或标记,以保存您所做的任何其他工作。