从Git的隐藏存储中恢复忽略的目录 [英] Recover ignored directory from dropped stash in Git

查看:112
本文介绍了从Git的隐藏存储中恢复忽略的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过隐藏未跟踪的文件,然后破坏了流行音乐,丢失了一个被忽略的目录(在.gitignore中列出).

I lost an ignored (listed in .gitignore) directory by stashing untracked files and then botching the pop.

我在一次提交中进行了很多更改(大部分代码移入新文件,重新排序代码,单行更改等).我想将它们分解成几个单独的提交.首先,为了安全起见,我创建了一个新分支并将其签出.我重置为上一个提交,并使用git add --patch进行了所有更改,并有选择地索引了要包含在第一个新提交中的内容.我还想包括我创建的几个新文件,所以我也必须添加它们.一旦索引反映了我想要进行的第一次新提交的更改,就可以使用git stash save --keep-index --include-untracked来存储未索引的更改,并仅将索引的更改保留在工作目录中,以便在提交之前对其进行测试.对提交感到满意之后,我将其提交,从存储中弹出更改,然后有选择地为下一次新提交添加更改.

I had a bunch of changes all lumped in one commit (big sections of code moved into new files, reordered code, one-line changes, etc.). I wanted to tease them apart into a few separate commits. First, I created a new branch and checked it out, to be safe. I reset to the previous commit and used git add --patch to go through all the changes and selectively index those I wanted to include in the first new commit. I also wanted to include a couple of the new files I had created, so I had to add them, too. Once my index reflected the changes I wanted for the first new commit, I used git stash save --keep-index --include-untracked to stash the unindexed changes and leave only the indexed changes in the working directory, so I could test them before committing. Once I was happy with the commit, I would commit it, pop the changes off the stash, and go about selectively adding the changes for the next new commit.

现在,我把这个搞砸了几次.首先,我在存储区中忘记了--include-untracked,因此我的工作目录中仍然包含所有我不需要的新文件.我立即意识到并尝试了git stash pop,所以我可以再试一次,但是出现合并冲突.我不确定如何正确地合并以及所有有选择地添加的更改,所以我回到了母版,创建了一个新分支,然后将其检出,希望我在第一个新分支中搞砸的任何内容会留在那儿(我当时是怎么错...).

Now, I screwed this up a few times. First, I forgot to --include-untracked in the stash, so my working directory still had all the new files I didn't want. I realized immediately and tried to git stash pop so I could try again, but I got merge conflicts. I wasn't sure how to merge properly, what with all the selectively added changes, so I went back to master, created a new branch, and checked it out, hoping that anything I had screwed up in the first new branch would stay there (how wrong I was...).

下一次(在有选择地再次添加后,phew ..),我忘记添加要包含在提交中的新文件,因此被藏起来了.我再次切换回master,建立了新分支,然后再次尝试.

The next time (after selectively adding again, phew..), I forgot to add the new files I wanted to include in the commit, so they got stashed. I again switched backed to master, made a new branch, and tried again.

在正确理解"之前,我必须至少完成3到4次.但是,当我去测试我的代码时,找不到我正在使用的输入文件之一.该文件位于我的忽略"目录中,该目录在.gitignore中列出.当我查看时,整个目录都丢失了.我在其中扔了很多东西-数据库文件等-我想保留它们,但不跟踪它们的更改.

I must have done that at least 3 or 4 times before I "got it right". But when I went to test my code, it couldn't find one of the input files I was using. The file was in my "ignore" directory, which was listed in .gitignore. When I looked, the entire directory was missing. I had thrown a variety of things in there - database files, etc. - I wanted to keep them, but not track their changes.

所以我看着我的git stash list.它列出了大约5个存储区,其中有4个是我如上所述创建的.我意识到他们在各自的分支机构中表现不佳.然后,我尝试在隐藏区中找到我的忽略"目录.我尝试弹出每个冲突,但是不断出现合并冲突,而且我从来都不十分确定如何解决它们-当我认为自己有冲突时,隐藏项仍然存在于列表中,但似乎已被部分应用.我最终尝试弹出,然后将每个存储区放到列表中,总是看着丢失的目录显示出来.没有.

So I looked at my git stash list. It listed about 5 stashes, 4 of which I had just created as described above. I realized that they hadn't stayed nicely in their respective branches. I then went through trying to find my "ignore" directory in the stashes. I tried to pop each one, but kept getting merge conflicts, and I was never really sure how to resolve them - when I thought I had, the stash still existed in the list but seemed to have been partially applied. I ended up attempting to pop and then dropping each stash in the list, always watching for my lost directory to show up. It didn't.

我遵循了如何在Git中恢复已丢弃的存储区中的指示?在gitk中查看我的历史记录,但似乎找不到我丢失的目录;列出的所有提交都不在树"或修补程序"视图中具有它,这可能是有道理的,因为应该 将其忽略.我特别看了那些标有WIP但没有骰子的东西.

I followed the instructions at How to recover a dropped stash in Git? to look at my history in gitk, but couldn't seem to find my missing directory; none of the commits listed has it in the Tree or Patch view, which might make sense, since it is supposed to ignore it. I looked in particular at those labeled WIP, but no dice.

我也尝试签出我创建的每个分支,但无济于事.

I also tried checking out each branch I'd made, to no avail.

如何进行跟踪?它不在任何位置(非隐藏)提交中,因为它从未被跟踪过.我假设它已被藏匿物移除,但也许我错了.如何找到放下的藏藏物并选择合适的藏藏物-我可以按内容搜索吗?当我找到它时,如何强制它在没有冲突的情况下适用?

How do I go about tracking it down? It's not in a (non-stash) commit anywhere, because it was never tracked. I'm assuming it got removed by a stash, but maybe I'm wrong.. How do I find my dropped stashes and choose the right one - can I search by contents? When I do find it, how do I force it to apply without conflicts?

$ git reflog --all
25b96f2 refs/heads/recover0@{0}: commit: trash
fbf987b refs/heads/recover0@{1}: commit: trash
de24fd2 refs/heads/tmp2@{0}: branch: Created from de24fd
78f64c9 refs/heads/fix0dot1@{0}: commit: temp commit d
6aaa987 refs/heads/tmp0@{0}: branch: Created from 6aaa987
6cab748 refs/heads/exp6@{0}: commit: trash
7cab1c1 refs/heads/tmp4@{0}: branch: Created from 7cab1c
bc60313 refs/heads/tmpA0@{0}: branch: Created from bc6031
38389f3 refs/heads/a1a@{0}: branch: Created from master
f01ca25 refs/heads/tmp5@{0}: branch: Created from f01ca2
0d59a2d refs/heads/tmp6@{0}: branch: Created from 0d59a2
43a9dc8 refs/heads/tmp7@{0}: branch: Created from 43a9dc
e8dd137 refs/heads/exp5@{0}: branch: Created from master
207bfea refs/heads/exp3@{0}: commit (amend): temp commit c
996a20b refs/heads/exp3@{1}: commit (amend): temp commit c
a1713f8 refs/heads/exp@{0}: rebase -i (finish): refs/heads/exp onto 7d5e666
09d03de refs/heads/exp@{1}: branch: Created from master
7d5e666 refs/heads/exp4@{0}: reset: moving to HEAD^
149c949 refs/heads/exp4@{1}: commit (amend): temp commit a
b4d9cbc refs/heads/exp4@{2}: rebase -i (finish): refs/heads/exp4 onto 75f8808
e8dd137 refs/heads/exp4@{3}: branch: Created from master
ec8afef refs/heads/exp6@{1}: reset: moving to HEAD^
7d5e666 refs/heads/exp6@{2}: reset: moving to HEAD^
38389f3 refs/heads/exp6@{3}: branch: Created from master
7e66c30 refs/stash@{0}: WIP on master: 662f5b9 fix #6: Print time used
f2e045e refs/heads/tmp8@{0}: branch: Created from f2e045
b780d56 refs/heads/tmp3@{0}: branch: Created from b780d5

推荐答案

git reflog --all应包含隐藏的隐藏项.您可以看到它的名称(它们类似于stash@{N}),然后使用git show或reset或其他任何东西来查看.

git reflog --all should include dropped stashes. You can see the name of it (they're something like stash@{N}) and then use git show or reset or whatever to take a look.

如果您无法从reflog中找到它,我认为您无法使用git恢复它.

If you can't find it from the reflog, I don't think you can recover it with git.

之所以将未跟踪的文件称为该文件,是有原因的.我建议您确保提交您关心的所有文件/目录.这样,您通常可以正确地恢复它们.

There's a reason untracked files are called that. I would recommend making sure you commit any files/dirs that you care about. That way you can usually recover them properly.

这篇关于从Git的隐藏存储中恢复忽略的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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