开发者

After filtering master, how do I retrieve stashed changes on the old master?

I followed GitHub's instructions for removing sensitive files from a git repository because I wanted to remove some binaries that should not have been checked in.

My first invocation of the git filter-branch command failed with:

Cannot rewrite branch(es) with a dirty working directory.

because I had local changes. So, I stashed away these changes with git stash, and re-ran the filter-branch command.

I then ran these commands per the GitHub instructions:

rm -rf .git/refs/original
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now

The removal of the binaries seems to have worked flawlessly. However, when I now type git stash list, the stash entry that I added on the old master is not displayed.

All that I have is the output from git stash 开发者_如何学编程before I executed the filter-branch command the second time:

Saved working directory and index state WIP on master: a19db18 LOG_MESSAGE
HEAD is now at a19db18 LOG_MESSAGE

Also, a19db18332b19ea41be888eccfc07e6680d8d6dd was one of the commits that was rewritten.

Is there a way to retrieve the stashed changes?


Unless you can remember the SHA1 hash of the stash commit objects (yes, stashes create commit objects), the stash may be lost. One option is to use git fsck --lost-found and look for dangling commit objects. You'll then have to go through one by one until you find the one that is your stash. You can use gitk to view all these commits (see the git fsck help page for an example on how to do this) and hopefully find the stash commits more easily. Once you've found the commits, you can use git cherry-pick on each one, clean up any conflicts and then do git reset HEAD~2 (since there are two commits with each stash). You should be good to go then.

I've never done a stash and then something as dangerous as filter-branch. In the future, I'd recommend making an actual commit with your work-in-progress -- perhaps putting it on a new branch. Then run the dangerous operation. After that, you can rollback the commit with a mixed reset as above.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜