git 如何将文件重置或恢复为特定版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/215718/
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
How can I reset or revert a file to a specific revision?
提问by Hates_
I have made some changes to a file which has been committed a few times as part of a group of files, but now want to reset/revert the changes on it back to a previous version.
我对作为一组文件的一部分提交了几次的文件进行了一些更改,但现在想将对其的更改重置/恢复到以前的版本。
I have done a git log
along with a git diff
to find the revision I need, but just have no idea how to get the file back to its former state in the past.
我已经完成了 agit log
和 agit diff
以找到我需要的修订版,但只是不知道如何将文件恢复到过去的状态。
回答by Greg Hewgill
Assuming the hash of the commit you want is c5f567
:
假设您想要的提交的哈希是c5f567
:
git checkout c5f567 -- file1/to/restore file2/to/restore
The git checkoutman page gives more information.
在git的结帐手册页提供了更多的信息。
If you want to revert to the commit before c5f567
, append ~1
(where 1 is the number of commits you want to go back, it can be anything):
如果您想恢复到之前的提交c5f567
,请追加~1
(其中 1 是您想要返回的提交次数,它可以是任何内容):
git checkout c5f567~1 -- file1/to/restore file2/to/restore
As a side note, I've always been uncomfortable with this command because it's used for both ordinary things (changing between branches) and unusual, destructive things (discarding changes in the working directory).
作为旁注,我一直对这个命令感到不舒服,因为它用于普通事物(在分支之间更改)和不寻常的破坏性事物(丢弃工作目录中的更改)。
回答by Chris Lloyd
You can quickly review the changes made to a file using the diff command:
您可以使用 diff 命令快速查看对文件所做的更改:
git diff <commit hash> <filename>
Then to revert a specific file to that commit use the reset command:
然后要将特定文件恢复到该提交,请使用 reset 命令:
git reset <commit hash> <filename>
You may need to use the --hard
option if you have local modifications.
--hard
如果您有本地修改,您可能需要使用该选项。
A good workflow for managaging waypoints is to use tags to cleanly mark points in your timeline. I can't quite understand your last sentence but what you may want is diverge a branch from a previous point in time. To do this, use the handy checkout command:
管理航点的一个很好的工作流程是使用标签在时间线中干净地标记点。我不太明白你的最后一句话,但你可能想要的是从前一个时间点分出一个分支。为此,请使用方便的 checkout 命令:
git checkout <commit hash>
git checkout -b <new branch name>
You can then rebase that against your mainline when you are ready to merge those changes:
然后,当您准备好合并这些更改时,您可以将其重新设置为针对您的主线:
git checkout <my branch>
git rebase master
git checkout master
git merge <my branch>
回答by foxxtrot
You can use any reference to a git commit, including the SHA-1 if that's most convenient. The point is that the command looks like this:
您可以使用任何对 git commit 的引用,包括 SHA-1(如果最方便的话)。关键是命令看起来像这样:
git checkout [commit-ref] -- [filename]
git checkout [commit-ref] -- [filename]
回答by Greg Hewgill
git checkout -- foo
That will reset foo
to HEAD. You can also:
那将重置foo
为 HEAD。你也可以:
git checkout HEAD^ foo
for one revision back, etc.
一次修改回来等。
回答by CDR
And to revert to last committed version, which is most frequently needed, you can use this simpler command.
并且要恢复到最常需要的上次提交版本,您可以使用这个更简单的命令。
git checkout HEAD file/to/restore
回答by bbrown
I had the same issue just now and I found this answereasiest to understand (commit-ref
is the SHA value of the change in the log you want to go back to):
我刚才遇到了同样的问题,我发现这个答案最容易理解(commit-ref
是您要返回的日志中更改的 SHA 值):
git checkout [commit-ref] [filename]
This will put that old version in your working directory and from there you can commit it if you want.
这会将旧版本放在您的工作目录中,如果您愿意,您可以从那里提交它。
回答by Ron DeVera
If you know how many commits you need to go back, you can use:
如果您知道需要返回多少次提交,则可以使用:
git checkout master~5 image.png
This assumes that you're on the master
branch, and the version you want is 5 commits back.
这假设您在master
分支上,并且您想要的版本是 5 次提交。
回答by jdee
I think I've found it....from http://www-cs-students.stanford.edu/~blynn/gitmagic/ch02.html
我想我已经找到了....来自http://www-cs-students.stanford.edu/~blynn/gitmagic/ch02.html
Sometimes you just want to go back and forget about every change past a certain point because they're all wrong.
有时你只想回过头来忘记过去某个点的每一个变化,因为它们都是错误的。
Start with:
从...开始:
$ git log
$ git log
which shows you a list of recent commits, and their SHA1 hashes.
它显示了最近提交的列表及其 SHA1 哈希值。
Next, type:
接下来,键入:
$ git reset --hard SHA1_HASH
$ git reset --hard SHA1_HASH
to restore the state to a given commit and erase all newer commits from the record permanently.
将状态恢复到给定的提交并从记录中永久删除所有较新的提交。
回答by v2k
This worked for me:
这对我有用:
git checkout <commit hash> file
Then commit the change:
然后提交更改:
git commit -a
回答by v2k
You have to be careful when you say "rollback". If you used to have one version of a file in commit $A, and then later made two changes in two separate commits $B and $C (so what you are seeing is the third iteration of the file), and if you say "I want to roll back to the first one", do you really mean it?
说“回滚”时必须小心。如果您曾经在提交 $A 中有一个文件版本,然后在两个单独的提交 $B 和 $C 中进行了两次更改(因此您看到的是文件的第三次迭代),并且如果您说“我要回滚到第一个”,你真的是这个意思吗?
If you want to get rid of the changes both the second and the third iteration, it is very simple:
如果你想摆脱第二次和第三次迭代的变化,很简单:
$ git checkout $A file
and then you commit the result. The command asks "I want to check out the file from the state recorded by the commit $A".
然后你提交结果。该命令询问“我想从提交 $A 记录的状态中检出文件”。
On the other hand, what you meant is to get rid of the change the second iteration (i.e. commit $B) brought in, while keeping what commit $C did to the file, you would want to revert $B
另一方面,您的意思是摆脱第二次迭代(即提交 $B)带来的更改,同时保留提交 $C 对文件所做的更改,您希望恢复 $B
$ git revert $B
Note that whoever created commit $B may not have been very disciplined and may have committed totally unrelated change in the same commit, and this revert may touch files other than fileyou see offending changes, so you may want to check the result carefully after doing so.
请注意,创建 commit $B 的人可能没有遵守纪律,并且可能在同一次提交中提交了完全不相关的更改,并且此还原可能会触及您看到的违规更改以外的文件,因此您可能需要在执行后仔细检查结果所以。