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

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

git checkout HEAD~2 (example) deletes untracked files if they were added with last commit

gitgit-checkout

提问by Rob

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 ...).

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

回答by Cascabel

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 shouldhave 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 --amendto amend to the previous commit instead of creating a separate one.

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

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 ...

回答by halfdan

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

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

回答by Rob

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.

此时分支已分离并包含以前未跟踪的文件。从现在开始,提交历史中的进一步检出是可能的,并且是一致的。

回答by Mark Peters

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

您可能想git commit --amend在之前忘记的提交中添加一些内容。