Git 恢复不恢复之前的状态
所以很久以前,我将一个包含一组文件的文件夹 abc 添加到我的项目中,并使用修订版 xyz 进行了提交。 经过多次提交后,有人使用 git rm -r abc 删除了该文件夹。 此后也进行了大量的提交。
现在我需要取回文件夹 abc。我
git revert xyz
这样做之后,我可以找到文件夹 xyz,但并非提交 xyz 中添加的所有文件都存在。
我做错了什么?我不想重置,因为我想保留提交 xyz 和当前之间的历史记录。
So a long time ago I added a folder abc with a group of files in it to my project and made a commit with revision xyz.
After many commits someone has erased the folder with git rm -r abc.
After this there has been a lot of commits as well.
Now I need to get back the folder abc. I do
git revert xyz
After this I can find folder xyz but not all of the files that were added in the commit xyz are present.
What am I doing wrong? I dont want to do reset because I want to keep the history between commit xyz and the present.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
git revert xyz
不会将您的代码恢复到修订版xyz
时的状态 - 它会创建一个新的提交,撤消任何内容在xyz
中完成。您可以尝试gitcherry-pick --no-commit xyz
,它将重放在xyz
中所做的更改(如果您省略--no-commit
,它将自动成为一个新的提交,但也许您不希望在xyz
中完成的所有操作)。git revert xyz
does not bring your code back to the state in which it was at revisionxyz
- it creates a new commit that undoes whatever was done inxyz
. You could trygit cherry-pick --no-commit xyz
, which will replay the changes that were done inxyz
(if you leave out--no-commit
, it will automatically become a new commit, but maybe you don't want everything that was done inxyz
).你真正想要的是这样的:
这表示“让我获得路径
abc
处的内容版本,就像提交xyz
中一样”。这样,您将使文件夹恢复到提交时的状态,但您不会触及此后可能已修改的任何其他文件。(如果您显式指定提交,则不需要
--
,除非提交的名称恰好也是项目中的文件名。例如,如果您尝试检查从master
指向的提交中取出名为HEAD
的文件版本,您需要执行git checkout master -- HEAD
。 [只是节省时间,不要命名你的分支,标记与您的文件相同的内容。])您不希望
git revert xyz
因为这是为了创建反转其他提交的提交,并且您显然不希望反转首先添加文件。What you really want is this:
That says "get me the version of the stuff at path
abc
, as it was in commitxyz
". That way, you'll get your folder back in the state you committed it, but you won't touch any other files that may have been modified since.(The
--
isn't necessary if you're explicitly specifying the commit, unless the name of the commit happens to also be a filename in your project. For instance, if you're trying to check out the version of a file namedHEAD
from the commit pointed to bymaster
, you'd need to dogit checkout master -- HEAD
. [Just save yourself the time and don't name your branches and tags the same things as your files.])You don't want
git revert xyz
because that's for creating commits that reverse other commits, and you obviously don't want to reverse your adding the files in the first place.