2016-07-06 99 views
4

我手边没有特定的问题,但过去遇到过一些我不小心炸掉我的索引的情况,希望我可以返回给定文件的前一个状态,该文件在某个时刻被编入索引。索引是否有reflog?

一些示例情况是:

$ git add <file> 
# find out that I already had an indexed version of <file>, 
# and that for some reason I shouldn't have added the extra modifications 

$ git stash pop 
# find out afterwards that I have a mix of "the index I had" 
# and "the index in the stash" 

$ git stash 
# with an active index, which is now mixed with the state of the working tree 

$ git reset <typo> 
# accidentally resetting the wrong file, or the whole directory 

人们可以诉诸通过git fsck --full --unreachable --no-reflog挖掘(如建议here),我想知道是否有这样做更方便的方式。

问:

是否有某种引用日志为指数?

回答

1

不,这个索引没有reflog。你将不得不按照我们的方式工作,因为你想出git fsck带或不带--lost-found标志的命令。

您可以使用文件(SHA-1)的时间戳根据创建日期对文件进行“排序”,并根据文件创建时间设置一些基本的引用日志。

+0

创建日期的窍门是一个不错的主意。谢谢。 – LeGEC

+0

为了测试的目的,我试图查找由'fsck --unreachable'返回的所有'.git/objects/ab/cdef ...'blob。一些晃来晃去的斑点不在那里,这可能意味着它们被存储在包文件中。你知道一个快速的方法来找到blob所属的包文件吗? – LeGEC

+0

我正在寻找文件的修改日期。您是否还有其他技巧可以找到blob的修改日期? – LeGEC

4

reflog包含refs ...不是索引的条目。

但是,也许工作流的调整是这里的答案...(这是我的)。

如果东西,这将需要超过5-10分钟,工作承诺,因为你去(和清理推前)。否则,阶段,你要去

index太好了......我整天都用它!但是,如果我知道我将在短短一两分钟内提交(基本上是原子级工作流操作),我才真正使用它。这是因为我害怕我会做一些愚蠢的事情,并吹走我的索引。

在我工作时,每当我达到一个小小的里程碑时,我都会进行一个私人提交,通常不会推迟,直到我有机会首先进行一些清理。在我处理那个特定问题时,我通常会进行提交,通常会进行修改。

然后,一旦我实际上达到了一个稳定点,我想创建一个公共提交,我将我所有的小wip一起提交,提供一个很好的提交消息并推送。

这给了我的reflog在需要时创建小面包屑的巨大优势。

这是我的工作流程:

# start work 
git checkout -b featurea 
# work 
vim file.txt 
# reach a little milestone 
git commit -a -m "working on feature..." 
# work some more 
vim file.txt 
# reach another little milestone 
git commit -a --reuse-message=HEAD --amend 
# work some more 
vim file.txt 
# another little milestone... 
git commit -a --reuse-message=HEAD --amend 
# finishing touches... 
vim file.txt 
# ok, done now, put everything back in working dir so I can review 
git reset HEAD~ 
# decide what goes in this commit 
# perhaps use `git add -p` 
git add file.txt 
# give a nice commit message (use editor) 
git commit 
# now merge to master and push with confidence! 

这可能看起来像很多打字的,但如果你在飞行壳上取得好(服用set -o emacsset -o vi优势是一个很好的方法),那么这种方法变成几乎瞬间。

如果我真的在做什么是一个非常快速的解决方案,我通常只会使用阶段随用的方法,但任何比我需要安全地填充我的reflog的时间长的任何事情都会随着我走。

+0

工作流程上的+1。此外,对于较长时间的任务(任何可能需要超过一天的时间,或者甚至超过几小时的任何时间),请创建本地分支。 Git的分支几乎是免费的:他们占用一定的磁盘空间,但我发现他们最昂贵的资源是我自己的头部空间(“嗯,我有'experiment1'通过'experiment45',很好,现在*我究竟在做什么与*?)... – torek

+0

@torek脱离主题 - 但是当你的git书出来?:-) –

+0

@ Alok--我得到了一个全职工作,并在它停止工作,唉! – torek