Git 恢复不恢复之前的状态

发布于 2024-12-03 21:26:22 字数 270 浏览 1 评论 0原文

所以很久以前,我将一个包含一组文件的文件夹 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神妖 2024-12-10 21:26:23

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 revision xyz - it creates a new commit that undoes whatever was done in xyz. You could try git cherry-pick --no-commit xyz, which will replay the changes that were done in xyz (if you leave out --no-commit, it will automatically become a new commit, but maybe you don't want everything that was done in xyz).

孤独患者 2024-12-10 21:26:22

你真正想要的是这样的:

git checkout xyz -- abc

这表示“让我获得路径 abc 处的内容版本,就像提交 xyz 中一样”。这样,您将使文件夹恢复到提交时的状态,但您不会触及此后可能已修改的任何其他文件。

(如果您显式指定提交,则不需要 -- ,除非提交的名称恰好也是项目中的文件名。例如,如果您尝试检查从 master 指向的提交中取出名为 HEAD 的文件版本,您需要执行 git checkout master -- HEAD[只是节省时间,不要命名你的分支,标记与您的文件相同的内容。]

您不希望 git revert xyz 因为这是为了创建反转其他提交的提交,并且您显然不希望反转首先添加文件。

What you really want is this:

git checkout xyz -- abc

That says "get me the version of the stuff at path abc, as it was in commit xyz". 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 named HEAD from the commit pointed to by master, you'd need to do git 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文