git checkout HEAD〜2(示例)将删除未跟踪的文件(如果它们是在上次提交时添加的) [英] git checkout HEAD~2 (example) deletes untracked files if they were added with last commit

查看:96
本文介绍了git checkout HEAD〜2(示例)将删除未跟踪的文件(如果它们是在上次提交时添加的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加新文件(之前以未跟踪文件的形式存在)并提交.当我在此提交之前检出时,这些文件将被删除.他们不应该.

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.

.gitignore是否列出这些文件都没有关系(这需要执行git add -f ...).

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

推荐答案

不应[删除].

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

如果文件确实应该已在旧提交中,则可能您想要做的是使用交互式rebase(git rebase -i <start commit> <branch>,git提供的进一步说明)来压缩几个提交.也许您也需要重新排列提交的顺序,将添加这些文件"提交推回到更远的历史记录中.或者,如果在忘记添加文件后立即注意到这一点,只需添加它们并使用commit --amend修改为上一个提交即可,而不用创建单独的提交.

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

这篇关于git checkout HEAD〜2(示例)将删除未跟踪的文件(如果它们是在上次提交时添加的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆