好吧,我已经遇到过几次这种情况了,这真的很烦人。场景:
modify a file say a.txt (add say two lines)
stash it
stash apply
git reset
remove the two lines added to a.txt because you want to commit and push these partial changes
git add a.txt
git commit the above, and push the changes (push irrelevant for this discussion)
git reset --hard
now do git stash apply
现在,a.txt 与提交的版本相同,它应该是我隐藏的版本。因此我现在丢失了对 a.txt 的更改。如果我返回一次提交,即。 git co head~1 并应用我的存储,但它仍然不起作用。
修复将不胜感激。
谢谢。
好的,所以我广泛尝试使用脚本来重现此内容,尝试了合并提交等的多种变体,但无法重现它。不幸的是,我很难在脚本中重现发生这种情况时我正在处理的源树的复杂性。我现在想到的另一个问题是,我有多个基于同一分支的不同版本的存储,其中一些文件被推送,一些文件在这些存储中本地提交。我的命令只应用了前面描述的最后一个存储,但在整个过程中,我丢失了 2 个存储,它们在执行 git stash list 时消失了。所以我也许这也会以某种方式影响结果。如果有人遇到类似的情况,请更新此线程。如果有人有兴趣进一步进行我的测试,我可以发布我的脚本(完全独立,创建存储库和所有内容)。与此同时,我将继续使用 git,下次发生这种情况时,也许我会更好地了解发生了什么。
这也是在工作中发生的,因此为什么我无法提供原始代码,但我会再次查看 gitk 结果并发布我的发现,因为这可能会有所帮助。
感谢您的帮助!
好的,所以查看 gitk 显示我的存储是基于索引(我认为这是正常的),然后该索引基于提交,似乎只存在于存储中(这没有意义。即。保存有我在将其提交到我的私人分支时给出的提交消息,但不知何故,我的私人分支没有将该提交作为 gitk 路径的一部分。
Ok, so i have run into this a few times and it's really annoying. Scenario:
modify a file say a.txt (add say two lines)
stash it
stash apply
git reset
remove the two lines added to a.txt because you want to commit and push these partial changes
git add a.txt
git commit the above, and push the changes (push irrelevant for this discussion)
git reset --hard
now do git stash apply
Now, a.txt is the same version as was committed, it should be the version that i stashed. Thus i have now lost my changes to a.txt. If i go back one commit, ie. git co head~1 and apply my stash it still doesn't work.
A fix would be much appreciated.
Thanks.
Okay, so i tried extensively with a script to reproduce this, trying many variations of merge commits etc and could not get reproduce it. Unfortunately it's very difficult in a script for me to reproduce the complexity of the source tree i was working on when this happened. The one other caviat i'm thinking of now is that i had multiple stashes based on different versions of the same branch, with some files pushed, some files committed locally that were in those stashes. My commands only ever applied the last stash as described earlier, but during this whole process, i lost 2 stashes, they just disappeared when executing git stash list. So i maybe this somehow also affects the outcome. If anybody runs into anything similar please update this thread. If anybody is interested in taking my testing further, i can post my script (completely self contained, creates the repository and everything). In the mean time i will continue using git and the next time it happens maybe i get a better idea of what happened.
Also this happened at work, hence why i can't supply original code, but i will take a look at the gitk results again and post my findings as that might help.
Thanks for all the help!
Ok, so looking at gitk shows that my stash is based on an index (which i think is normal), then that index is based off of a commit, that seems to only exist in the stash (this does not make sense. Ie. the save has the commit message i gave it when i committed it to my private branch, but somehow, my private branch does not have that commit as part of the gitk path.
发布评论
评论(1)
好的,我只是按照您的说明顺序操作,我得到的是:
git stash apply
重新应用您对刚刚保存的a.txt
所做的更改藏匿处。git reset 会清除索引,但保留工作目录。
git add
和git commit
将更改保存到a.txt
中,以供以后使用。但是,除了可能误解您在做什么之外,您似乎还可以从 git add --patch 中受益。这允许您向文件添加单独的更改,而无需添加该文件中的所有更改。
Okay, I just followed your sequence of instructions, and what I get is:
The
git stash apply
re-applies the changes you made toa.txt
that you just saved in the stash.The
git reset
clears the index, but leaves the working directory alone.The
git add
andgit commit
save the changes toa.txt
that you meant to stash for later use.But, besides for possibly misunderstanding what you are doing, it looks like you may benefit from
git add --patch
. That allows you to add individual changes to a file, without adding all of the changes in that file.