我开了个藏宝箱,合并发生了冲突。与被列为重复的问题不同,我已经在我想保留的目录中有一些未提交的更改。我不只是想让合并冲突消失,还想让我的目录恢复到弹出窗口之前的状态。

我尝试了git合并-abort,但git声称没有合并正在进行中。是否有一种简单的方法来中止弹出,而不破坏我原来在目录中的更改?


当前回答

好吧,我想我已经解决了"git stash unapply"。它比git apply -reverse更复杂,因为你需要反向合并操作,以防git stash apply做了任何合并。

反向合并要求所有当前更改都被推入索引:

Git add -u

然后反转由git stash apply完成的合并递归:

——$(Git write-tree) stash@{0}^1

现在你将只剩下非隐藏的变化。它们会在索引中。如果你愿意,你可以使用git重置来取消你的更改。

鉴于你最初的git stash应用失败了,我假设反向也可能失败,因为它想要撤消的一些事情没有完成。

下面是一个例子,展示了工作副本(通过git状态)如何再次干净:

 $ git status
# On branch trunk
nothing to commit (working directory clean)
 $ git stash apply
Auto-merging foo.c
# On branch trunk
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   foo.c
#
no changes added to commit (use "git add" and/or "git commit -a")
 $ git add -u
 $ git merge-recursive stash@{0}: -- $(git write-tree) stash@{0}^1
Auto-merging foo.c
 $ git status
# On branch trunk
nothing to commit (working directory clean)

其他回答

好吧,我想我已经解决了"git stash unapply"。它比git apply -reverse更复杂,因为你需要反向合并操作,以防git stash apply做了任何合并。

反向合并要求所有当前更改都被推入索引:

Git add -u

然后反转由git stash apply完成的合并递归:

——$(Git write-tree) stash@{0}^1

现在你将只剩下非隐藏的变化。它们会在索引中。如果你愿意,你可以使用git重置来取消你的更改。

鉴于你最初的git stash应用失败了,我假设反向也可能失败,因为它想要撤消的一些事情没有完成。

下面是一个例子,展示了工作副本(通过git状态)如何再次干净:

 $ git status
# On branch trunk
nothing to commit (working directory clean)
 $ git stash apply
Auto-merging foo.c
# On branch trunk
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   foo.c
#
no changes added to commit (use "git add" and/or "git commit -a")
 $ git add -u
 $ git merge-recursive stash@{0}: -- $(git write-tree) stash@{0}^1
Auto-merging foo.c
 $ git status
# On branch trunk
nothing to commit (working directory clean)

一些想法:

使用git mergetool将合并文件拆分为原始部分和新部分。希望其中一个是包含你非隐藏更改的文件。 反向应用隐藏的diff来撤销这些更改。您可能必须手动分离合并冲突的文件(希望上面的技巧可以奏效)。

这两个我都没有测试过,所以我不确定它们是否能工作。

如果你不需要担心你所做的任何其他更改,你只想回到上次提交,那么你可以这样做:

git reset .
git checkout .
git clean -f

我用不同的方法解决了这个问题。事情是这样的。

首先,我碰到了错误的分支,产生了冲突。存储仍然完好无损,但是索引在冲突解决中,阻塞了许多命令。

简单的git重置HEAD会中止冲突解决,并留下未提交(和不需要的)更改。

几个git co <filename>将索引恢复到初始状态。最后,我用git co <branch-name>切换了分支,并运行了一个新的git stash pop,解决了没有冲突的问题。

简单的一行

我总是用

Git重置—合并

我不记得它曾经失败过。


注意:git重置——合并将丢弃任何阶段性更改。此外,正如@Saroopashree Kumaraguru在评论中指出的那样,隐藏的内容不会丢失,以后可以重新应用。