git stash 错误:git stash pop 并最终导致合并冲突

发布于 2024-09-01 12:07:17 字数 1132 浏览 4 评论 0原文

我做了一个 git stash pop 并最终出现了合并冲突。我从文件系统中删除了这些文件,并执行了 git checkout ,如下所示,但它认为这些文件仍未合并。然后我尝试替换文件并再次执行 git checkout 并得到相同的结果。我尝试使用 -f 标志强制它。任何帮助将不胜感激!

chirag-patels-macbook-pro:haloror patelc75$ git status
app/views/layouts/_choose_patient.html.erb: needs merge
app/views/layouts/_links.html.erb: needs merge
# On branch prod-temp
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   db/schema.rb
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       unmerged:   app/views/layouts/_choose_patient.html.erb
#       unmerged:   app/views/layouts/_links.html.erb

chirag-patels-macbook-pro:haloror patelc75$ git checkout app/views/layouts/_choose_patient.html.erb
error: path 'app/views/layouts/_choose_patient.html.erb' is unmerged
chirag-patels-macbook-pro:haloror patelc75$ git checkout -f app/views/layouts/_choose_patient.html.erb
warning: path 'app/views/layouts/_choose_patient.html.erb' is unmerged

I did a git stash pop and ended up with merge conflicts. I removed the files from the file system and did a git checkout as shown below, but it thinks the files are still unmerged. I then tried replacing the files and doing a git checkout again and same result. I event tried forcing it with -f flag. Any help would be appreciated!

chirag-patels-macbook-pro:haloror patelc75$ git status
app/views/layouts/_choose_patient.html.erb: needs merge
app/views/layouts/_links.html.erb: needs merge
# On branch prod-temp
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   db/schema.rb
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       unmerged:   app/views/layouts/_choose_patient.html.erb
#       unmerged:   app/views/layouts/_links.html.erb

chirag-patels-macbook-pro:haloror patelc75$ git checkout app/views/layouts/_choose_patient.html.erb
error: path 'app/views/layouts/_choose_patient.html.erb' is unmerged
chirag-patels-macbook-pro:haloror patelc75$ git checkout -f app/views/layouts/_choose_patient.html.erb
warning: path 'app/views/layouts/_choose_patient.html.erb' is unmerged

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

屋檐 2024-09-08 12:07:17

请参阅 man git merge如何解决冲突):

看到冲突后,您可以做两件事:

  • 决定不合并。您需要的唯一清理是将索引文件重置为 HEAD 提交以反转 2. 并清理 2. 和 3. 所做的工作树更改; git-reset --hard 可以用于此目的。

  • 解决冲突。 Git 会在工作树中标记冲突。将文件编辑为 shape 并将它们添加到索引中。使用 git commit 来达成交易。

TRUE MERGE下(看看2.和3.指的是什么):

当如何协调更改并不明显时,会发生以下情况:

  1. HEAD 指针保持不变。

  2. MERGE_HEAD 引用设置为指向另一个分支头。

  3. 干净合并的路径会在索引文件和工作树中更新。

  4. ...

所以:如果您想从工作树中删除存储更改,请使用 git reset --hard ,或者如果您只想清理索引,请使用 git reset并将冲突留在工作树中以手动合并。

man git stash 下(选项,pop)您还可以阅读:

应用状态可能会因冲突而失败;在这种情况下,它不会从存储列表中删除。您需要手动解决冲突,然后手动调用 git stash drop 。

See man git merge (HOW TO RESOLVE CONFLICTS):

After seeing a conflict, you can do two things:

  • Decide not to merge. The only clean-ups you need are to reset the index file to the HEAD commit to reverse 2. and to clean up working tree changes made by 2. and 3.; git-reset --hard can be used for this.

  • Resolve the conflicts. Git will mark the conflicts in the working tree. Edit the files into shape and git add them to the index. Use git commit to seal the deal.

And under TRUE MERGE (to see what 2. and 3. refers to):

When it is not obvious how to reconcile the changes, the following happens:

  1. The HEAD pointer stays the same.

  2. The MERGE_HEAD ref is set to point to the other branch head.

  3. Paths that merged cleanly are updated both in the index file and in your working tree.

  4. ...

So: use git reset --hard if you want to remove the stash changes from your working tree, or git reset if you want to just clean up the index and leave the conflicts in your working tree to merge by hand.

Under man git stash (OPTIONS, pop) you can read in addition:

Applying the state can fail with conflicts; in this case, it is not removed from the stash list. You need to resolve the conflicts by hand and call git stash drop manually afterwards.

獨角戲 2024-09-08 12:07:17

我也发生过类似的事情。我还不想暂存这些文件,因此我使用 git add 添加了它们,然后执行了 git reset。这基本上只是添加然后取消了我的更改,但清除了未合并的路径。

I had a similar thing happen to me. I didn't want to stage the files just yet so I added them with git add and then just did git reset. This basically just added and then unstaged my changes but cleared the unmerged paths.

我的痛♀有谁懂 2024-09-08 12:07:17

如果像我一样,您通常想要的是用隐藏文件的内容覆盖工作目录的内容,并且您仍然遇到冲突,那么您想要的是使用 git checkout --theirs 解决冲突-- . 从根开始。

之后,您可以 git reset 将所有更改从索引带到工作目录,因为显然在发生冲突的情况下,对非冲突文件的更改保留在索引中。

您可能还想随后运行 git stash drop [] 来删除存储,因为 git stash pop 不会删除它,以防万一的冲突。

If, like me, what you usually want is to overwrite the contents of the working directory with that of the stashed files, and you still get a conflict, then what you want is to resolve the conflict using git checkout --theirs -- . from the root.

After that, you can git reset to bring all the changes from the index to the working directory, since apparently in case of conflict the changes to non-conflicted files stay in the index.

You may also want to run git stash drop [<stash name>] afterwards, to get rid of the stash, because git stash pop doesn't delete it in case of conflicts.

仙女山的月亮 2024-09-08 12:07:17

请注意,Git 2.5(2015 年第 2 季度) 未来的 Git 可能会尝试使这种情况变得不可能。

请参阅 提交 ed178ef,作者:杰夫·金 (peff),2015 年 4 月 22 日。
(由 Junio C Hamano 合并 -- gitster -- 在 提交 05c3967,2015 年 5 月 19 日)

注意:此内容已恢复。见下文

stash:需要一个干净的索引来应用/弹出

问题

如果您在索引中暂存了内容并运行“stash apply/pop”,我们可能会遇到冲突并将新条目放入索引中。
此时恢复到原始状态很困难,因为像“git reset --keep”这样的工具会破坏任何上演的内容

换句话说:

git stash pop/apply”忘记确保不仅工作树是干净的,而且索引也是干净的。
后者很重要,因为存储应用程序可能会发生冲突,并且索引将用于解决冲突。

解决方案

当出现阶段性变更时,我们可以拒绝申请,从而使这一过程更加安全。

这意味着如果之前因为对修改的文件应用存储而进行了合并(添加但未提交),那么现在它们将不会是任何合并,因为存储应用/弹出将立即停止:

Cannot apply stash: Your index contains uncommitted changes.

强制您提交更改意味着,在合并的情况下,您可以使用 git reset --hard< 轻松恢复初始状态(在 git stash apply/pop 之前) /代码>.



请参阅提交 1937610(2015 年 6 月 15 日)和 提交 ed178ef(2015 年 4 月 22 日),作者:杰夫·金 (peff)
(由 Junio C Hamano -- gitster -- 合并于 提交 bfb539b,2015 年 6 月 24 日)

该提交是为了提高应用程序的安全性的尝试
一个藏匿处,因为应用程序进程可能会创建
索引条目冲突,之后很难恢复
原始索引状态。

不幸的是,这会损害“git stash -k”周围的一些常见工作流程,例如:

git add -p       ;# (1) stage set of proposed changes
git stash -k     ;# (2) get rid of everything else
make test        ;# (3) make sure proposal is reasonable
git stash apply  ;# (4) restore original working tree

如果您在步骤 (3) 和 (4) 之间“git commit”,那么这
就可以了。但是,如果这些步骤是预提交的一部分
钩子,你没有这个机会(你必须恢复
原始状态,无论测试是否通过或
失败)。

Note that Git 2.5 (Q2 2015) a future Git might try to make that scenario impossible.

See commit ed178ef by Jeff King (peff), 22 Apr 2015.
(Merged by Junio C Hamano -- gitster -- in commit 05c3967, 19 May 2015)

Note: This has been reverted. See below.

stash: require a clean index to apply/pop

Problem

If you have staged contents in your index and run "stash apply/pop", we may hit a conflict and put new entries into the index.
Recovering to your original state is difficult at that point, because tools like "git reset --keep" will blow away anything staged.

In other words:

"git stash pop/apply" forgot to make sure that not just the working tree is clean but also the index is clean.
The latter is important as a stash application can conflict and the index will be used for conflict resolution.

Solution

We can make this safer by refusing to apply when there are staged changes.

That means if there were merges before because of applying a stash on modified files (added but not committed), now they would not be any merges because the stash apply/pop would stop immediately with:

Cannot apply stash: Your index contains uncommitted changes.

Forcing you to commit the changes means that, in case of merges, you can easily restore the initial state( before git stash apply/pop) with a git reset --hard.


See commit 1937610 (15 Jun 2015), and commit ed178ef (22 Apr 2015) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit bfb539b, 24 Jun 2015)

That commit was an attempt to improve the safety of applying
a stash, because the application process may create
conflicted index entries, after which it is hard to restore
the original index state.

Unfortunately, this hurts some common workflows around "git stash -k", like:

git add -p       ;# (1) stage set of proposed changes
git stash -k     ;# (2) get rid of everything else
make test        ;# (3) make sure proposal is reasonable
git stash apply  ;# (4) restore original working tree

If you "git commit" between steps (3) and (4), then this
just works. However, if these steps are part of a pre-commit
hook, you don't have that opportunity (you have to restore
the original state regardless of whether the tests passed or
failed).

尛丟丟 2024-09-08 12:07:17
git restore --staged  name-of-file-that-has--merge-conflict
git restore --staged  name-of-file-that-has--merge-conflict
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文