在主分支上使用Git stash pop

5

我有两个分支,一个是主分支master,另一个是开发分支development。我在主分支上进行了stash操作,例如:

git stash
git checkout development

现在,我原本在开发分支,但是错误地弹出了存储。
git stash pop

现在它显示出冲突。我使用以下命令重置了git:

git reset HEAD

但是仍然存在冲突。我需要将我的存储应用到主分支中。如何解决这个问题?


2
可能是撤销意外的git stash pop的重复问题。 - Jörn Hees
2个回答

2

git reset HEAD 只会影响索引,而不会影响工作树。实际上,它只是取消暂存的文件,因为 HEAD 已经在你当前的分支上了(这就是 HEAD 的定义)。

git reset --hard HEAD 会将工作树恢复到与 HEAD 相同的状态。

如果 git pop 无法干净地应用,那么应该保留存储的内容。

来自 git-stash 手册:

应用状态时可能会出现冲突;在这种情况下,它不会从存储列表中删除。您需要手动解决冲突,并在此之后手动调用 git stash drop。

所以只需执行以下操作:

git checkout master
git stash pop 
#or `git stash apply` if you don't want to drop the stash even if it does apply cleanly

有没有办法找回丢失的代码库? - Rohit
如果您遇到冲突,那么保存的工作区应该被保留下来,也就是说,它不应该丢失。 - Petr Skocik
如果它丢失了,你应该能够在 reflog 中找到它。 - Kristján
你可以使用 'git stash list' 命令查看是否存在存储的更改。 - Ondrej Burkert

1

尝试使用以下内容查看git stash手册

git stash --help

它有一个章节介绍了如果意外清除/删除存储区时应该怎么做。这将有助于理解在这种情况下应该采取什么措施。

Recovering stashes that were cleared/dropped erroneously
   If you mistakenly drop or clear stashes, they cannot be recovered through the normal safety mechanisms. However, you can try the
   following incantation to get a list of stashes that are still in your repository, but not reachable any more:

       git fsck --unreachable |
       grep commit | cut -d\  -f3 |
       xargs git log --merges --no-walk --grep=WIP

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接