git checkout HEAD~2 (示例)删除未跟踪的文件(如果它们是在上次提交时添加的)

发布于 2024-09-19 03:03:07 字数 116 浏览 4 评论 0原文

我添加新文件(之前作为未跟踪文件存在)并提交它们。当我在这次提交之前签出时,这些文件将被删除。他们不应该。

.gitignore 是否列出这些文件并不重要(这需要执行 git add -f ...)。

I add new files (which were present before as untracked files) and commit them. When I checkout to before this commit, these files are deleted. They should not.

It does not matter whether .gitignore lists these files or not (which requires to do git add -f ...).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

撩起发的微风 2024-09-26 03:03:07

它们不应该[被删除]。

是的,他们应该这样做。提交是历史状态。您正在检查它们提交之前的状态,因此它们不应该在那里。想象一下一年后你的项目的工作情况。一半的文件已被重命名,有数十个新文件,无论出于何种原因,您决定查看今天的版本。您肯定不希望所有这几十个文件只是闲置并使其变得混乱!他们不属于那里! (当然,“未跟踪的文件...在上次提交时添加”没有任何意义 - 如果您提交了它们,它们现在就会被跟踪。)

如果文件确实应该已经在旧的提交,您可能想要做的是使用交互式变基(git rebase -i,git 提供的进一步说明)将几个提交压缩在一起。也许您还需要对提交重新排序,将“添加这些文件”提交推回其所属的历史记录中更远的位置。或者,如果您在忘记添加文件后立即注意到这一点,只需添加它们并使用 commit --amend 修改上一个提交,而不是创建单独的提交。

最后,如果您确实以这种方式在历史记录中获得了此设置(其他人已拉取,因此您不想重新设置/修改),您可以检查旧的提交,然后检查较新的提交中的文件:

git checkout <old-commit>
git checkout <new-commit> file1 file2 dir1 dir2/file3 ...

They should not [be deleted].

Yes, they should. Commits are historical states. You're checking out a state before they were committed, so they should not be there. Just imagine working on your project a year from now. Half the files have been renamed, there are dozens of new files, and for whatever reason you decide to check out today's version. Surely you wouldn't want all those dozens of files to just sit around and clutter it up! They don't belong there! (And of course "untracked files...added with last commit" doesn't make any sense - if you committed them, they're now tracked.)

If the files really should have been in the old commit, likely what you want to do is use an interactive rebase (git rebase -i <start commit> <branch>, further instructions provided by git)to squash a couple commits together. Perhaps you'll need to reorder the commits too, pushing the "add these files" commit back farther in the history where it belongs. Or, if you notice this right after you forget to add the files, simply add them and use commit --amend to amend to the previous commit instead of creating a separate one.

Finally, if you really do get this set in the history this way (others have pulled so you don't want to rebase/amend), you can check out the old commit, and then check out the files from the newer commit:

git checkout <old-commit>
git checkout <new-commit> file1 file2 dir1 dir2/file3 ...
§普罗旺斯的薰衣草 2024-09-26 03:03:07

只需再次签出 HEAD 即可恢复这些文件。 Git checkout HEAD~2 将存储库的目录恢复到两次提交前的已跟踪状态。这完全是预期的行为。

Just checkout HEAD again and you get those files back. Git checkout HEAD~2 reverts the directory of your repository back to the tracked status you had two commits ago. This is totally expected behaviour.

美人骨 2024-09-26 03:03:07

当最后一次提交仅包含文件添加时,可能的解决方案:

$ git diff HEAD^ >diff
$ git checkout HEAD^
$ git apply diff

此时分支已分离并包含以前未跟踪的文件。从现在开始,可以对提交历史记录进行进一步的检查,并且保持一致。

Possible solution when the last commit only contains the file additions:

$ git diff HEAD^ >diff
$ git checkout HEAD^
$ git apply diff

At this time the branch is detached and contains the formerly untracked files. From now on, further checkouts in commit history are possible, and consistent.

苍暮颜 2024-09-26 03:03:07

您可能希望使用 git commit --amend 来添加您忘记的上一次提交的内容。

You might like git commit --amend to tack on stuff to the previous commit that you forgot.

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