我遇到了合并冲突。 如何中止合并?

发布于 2024-07-05 20:39:24 字数 155 浏览 12 评论 0 原文

我使用了 git pull 并遇到了合并冲突:

unmerged:   some_file.txt

You are in the middle of a conflicted merge.

如何放弃对文件的更改并仅保留拉取的更改?

I used git pull and had a merge conflict:

unmerged:   some_file.txt

You are in the middle of a conflicted merge.

How do I abandon my changes to the file and keep only the pulled changes?

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

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

发布评论

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

评论(14

谎言月老 2024-07-12 20:39:26

对于 git >= 1.6.1:

git merge --abort

对于旧版本的 git,这将完成这项工作:

git reset --merge

git reset --hard

For git >= 1.6.1:

git merge --abort

For older versions of git, this will do the job:

git reset --merge

or

git reset --hard
澉约 2024-07-12 20:39:26

您可以中止合并步骤:

git merge --abort

否则您可以保留您的更改(您所在的分支),

git checkout --ours file1 file2 ...

否则您可以保留其他分支更改

git checkout --theirs file1 file2 ...

You can either abort the merge step:

git merge --abort

else you can keep your changes (on which branch you are)

git checkout --ours file1 file2 ...

otherwise you can keep other branch changes

git checkout --theirs file1 file2 ...
水溶 2024-07-12 20:39:26

在这个特定的用例中,您并不是真的想中止合并,只是以特定的方式解决冲突。

也没有特别需要重置并使用不同的策略执行合并。 git 已正确突出显示冲突,并且接受另一方更改的要求仅适用于该文件。

对于冲突中未合并的文件,git 在索引中提供该文件的公共基础版本、本地版本和远程版本。 (这是 git mergetool 读取它们以便在 3 向 diff 工具中使用的位置。)您可以使用 git show 来查看它们。

# common base:
git show :1:_widget.html.erb

# 'ours'
git show :2:_widget.html.erb

# 'theirs'
git show :3:_widget.html.erb

解决逐字使用远程版本冲突的最简单方法是:

git show :3:_widget.html.erb >_widget.html.erb
git add _widget.html.erb

或者,使用 git >= 1.6.1:

git checkout --theirs _widget.html.erb

In this particular use case, you don't really want to abort the merge, just resolve the conflict in a particular way.

There is no particular need to reset and perform a merge with a different strategy, either. The conflicts have been correctly highlighted by git and the requirement to accept the other sides changes is only for this one file.

For an unmerged file in a conflict git makes available the common base, local and remote versions of the file in the index. (This is where they are read from for use in a 3-way diff tool by git mergetool.) You can use git show to view them.

# common base:
git show :1:_widget.html.erb

# 'ours'
git show :2:_widget.html.erb

# 'theirs'
git show :3:_widget.html.erb

The simplest way to resolve the conflict to use the remote version verbatim is:

git show :3:_widget.html.erb >_widget.html.erb
git add _widget.html.erb

Or, with git >= 1.6.1:

git checkout --theirs _widget.html.erb
窝囊感情。 2024-07-12 20:39:26

评论表明 git reset --merge 是 git merge --abort 的别名。 值得注意的是,在存在 MERGE_HEAD 的情况下,git merge --abort 仅相当于 git reset --merge。 这可以在 git 合并命令帮助中阅读。

当 MERGE_HEAD 存在时,git merge --abort 相当于 git reset --merge。

合并失败后,当没有 MERGE_HEAD 时,可以使用 git reset --merge 撤消失败的合并,但不一定使用 git merge --abort 它们不仅仅是同一事物的新旧语法

就我个人而言,我发现 git reset --merge 对于与所描述的类似的场景以及一般失败的合并来说更强大。

Comments suggest that git reset --merge is an alias for git merge --abort. It is worth noticing that git merge --abort is only equivalent to git reset --merge given that a MERGE_HEAD is present. This can be read in the git help for merge command.

git merge --abort is equivalent to git reset --merge when MERGE_HEAD is present.

After a failed merge, when there is no MERGE_HEAD, the failed merge can be undone with git reset --merge, but not necessarily with git merge --abort. They are not only old and new syntax for the same thing.

Personally, I find git reset --merge much more powerful for scenarios similar to the described one, and failed merges in general.

各空 2024-07-12 20:39:26

如果您最终遇到合并冲突并且没有任何可提交的内容,但仍然显示合并错误。 应用以下所有命令后,

git reset --hard HEAD
git pull --strategy=theirs remote_branch
git fetch origin
git reset --hard origin

请删除

.git\index.lock

文件[将粘贴剪切到其他位置以备恢复],然后根据您想要的版本输入以下任意命令。

git reset --hard HEAD
git reset --hard origin

希望有帮助!

If you end up with merge conflict and doesn't have anything to commit, but still a merge error is being displayed. After applying all the below mentioned commands,

git reset --hard HEAD
git pull --strategy=theirs remote_branch
git fetch origin
git reset --hard origin

Please remove

.git\index.lock

File [cut paste to some other location in case of recovery] and then enter any of below command depending on which version you want.

git reset --hard HEAD
git reset --hard origin

Hope that helps!!!

掌心的温暖 2024-07-12 20:39:26

保留工作副本状态的另一种选择是:

git stash
git merge --abort
git stash pop

我通常建议不要这样做,因为它实际上就像在 Subversion 中合并一样,因为它会在接下来的提交中丢弃分支关系。

An alternative, which preserves the state of the working copy is:

git stash
git merge --abort
git stash pop

I generally advise against this, because it is effectively like merging in Subversion as it throws away the branch relationships in the following commit.

海拔太高太耀眼 2024-07-12 20:39:26

可能不是OP想要的,但对我来说,我尝试将稳定分支合并到功能分支,但存在太多冲突。
我没有设法重置更改,因为 HEAD 已被多次提交更改,因此简单的解决方案是强制签出到稳定分支。
然后您可以签出到另一个分支,它将与合并之前一样。

git checkout -f master

git checkout side-branch

Might not be what the OP wanted, but for me I tried to merge a stable branch to a feature branch and there were too many conflicts.
I didn't manage to reset the changes since the HEAD was changed by many commits, So the easy solution was to force checkout to a stable branch.
you can then checkout to the other branch and it will be as it was before the merge.

git checkout -f master

git checkout side-branch

葮薆情 2024-07-12 20:39:26

从 Git 1.6.1.3 开始 git checkout 已经可以签出从合并的任一侧:

git checkout --theirs _widget.html.erb

Since Git 1.6.1.3 git checkout has been able to checkout from either side of a merge:

git checkout --theirs _widget.html.erb
拥抱我好吗 2024-07-12 20:39:26

为了避免陷入此类麻烦,可以扩展git merge --abort方法并在之前创建一个单独的测试分支合并。

案例:您有一个主题分支,它没有被合并,因为您分心/出现了一些事情/您知道但它已经(或已经)准备好了。

现在是否可以将其合并到master中?

测试分支中工作以估计/找到解决方案,然后放弃测试分支并在主题分支中应用该解决方案。

# Checkout the topic branch
git checkout topic-branch-1

# Create a _test_ branch on top of this
git checkout -b test

# Attempt to merge master
git merge master

# If it fails you can abandon the merge
git merge --abort
git checkout -
git branch -D test  # we don't care about this branch really...

致力于解决冲突。

# Checkout the topic branch
git checkout topic-branch-1

# Create a _test_ branch on top of this
git checkout -b test

# Attempt to merge master
git merge master

# resolve conflicts, run it through tests, etc
# then
git commit <conflict-resolving>

# You *could* now even create a separate test branch on top of master
# and see if you are able to merge
git checkout master
git checkout -b master-test
git merge test

最后再次检查主题分支,应用测试分支中的修复并继续 PR。
最后删除测试和主测试。

涉及? 是的,但在我准备好之前,它不会扰乱我的主题或主分支。

To avoid getting into this sort of trouble one can expand on the git merge --abort approach and create a separate test branch before merging.

Case: You have a topic branch, it wasn't merged because you got distracted/something came up/you know but it is (or was) ready.

Now is it possible to merge this into master?

Work in a test branch to estimate / find a solution, then abandon the test branch and apply the solution in the topic branch.

# Checkout the topic branch
git checkout topic-branch-1

# Create a _test_ branch on top of this
git checkout -b test

# Attempt to merge master
git merge master

# If it fails you can abandon the merge
git merge --abort
git checkout -
git branch -D test  # we don't care about this branch really...

Work on resolving the conflict.

# Checkout the topic branch
git checkout topic-branch-1

# Create a _test_ branch on top of this
git checkout -b test

# Attempt to merge master
git merge master

# resolve conflicts, run it through tests, etc
# then
git commit <conflict-resolving>

# You *could* now even create a separate test branch on top of master
# and see if you are able to merge
git checkout master
git checkout -b master-test
git merge test

Finally checkout the topic branch again, apply the fix from the test branch and continue with the PR.
Lastly delete the test and master-test.

Involved? Yes, but it won't mess with my topic or master branch until I'm good and ready.

下雨或天晴 2024-07-12 20:39:26

我发现以下内容对我有用(将单个文件恢复到合并前状态):

git reset *currentBranchIntoWhichYouMerged* -- *fileToBeReset*

I found the following worked for me (revert a single file to pre-merge state):

git reset *currentBranchIntoWhichYouMerged* -- *fileToBeReset*
厌味 2024-07-12 20:39:25

由于您的 pull 不成功,因此 HEAD (不是 HEAD^)是您分支上的最后一个“有效”提交:

git reset --hard HEAD

您想要的另一部分是让他们的更改覆盖您的更改。

旧版本的 git 允许您使用“他们的”合并策略:

git pull --strategy=theirs remote_branch

但这已被删除,如 此消息由 Junio Hamano(Git 维护者)发出。 正如链接中所述,您可以这样做:

git fetch origin
git reset --hard origin

Since your pull was unsuccessful then HEAD (not HEAD^) is the last "valid" commit on your branch:

git reset --hard HEAD

The other piece you want is to let their changes over-ride your changes.

Older versions of git allowed you to use the "theirs" merge strategy:

git pull --strategy=theirs remote_branch

But this has since been removed, as explained in this message by Junio Hamano (the Git maintainer). As noted in the link, instead you would do this:

git fetch origin
git reset --hard origin
遗忘曾经 2024-07-12 20:39:25

如果您的 git 版本 >= 1.6.1,您可以使用 git reset --merge 。

另外,正如 @Michael Johnson 提到的,如果您的 git 版本 >= 1.7.4,您还可以使用 git merge --abort 。

与往常一样,请确保在开始合并之前没有未提交的更改。

来自 git merge 手册页

MERGE_HEAD 存在时, git merge --abort 相当于 git reset --merge

合并正在进行时,会出现 MERGE_HEAD

另外,关于开始合并时未提交的更改:

如果您有不想在开始合并之前提交的更改,只需在合并之前 git stash 它们,然后 git stash pop 完成合并或中止合并后。

If your git version is >= 1.6.1, you can use git reset --merge.

Also, as @Michael Johnson mentions, if your git version is >= 1.7.4, you can also use git merge --abort.

As always, make sure you have no uncommitted changes before you start a merge.

From the git merge man page

git merge --abort is equivalent to git reset --merge when MERGE_HEAD is present.

MERGE_HEAD is present when a merge is in progress.

Also, regarding uncommitted changes when starting a merge:

If you have changes you don't want to commit before starting a merge, just git stash them before the merge and git stash pop after finishing the merge or aborting it.

栩栩如生 2024-07-12 20:39:25
git merge --abort

中止当前的冲突解决进程,并尝试重建
合并前的状态。

如果合并时存在未提交的工作树更改
启动后,git merge --abort 在某些情况下将无法
重建这些变化。 因此建议始终
在运行 git merge 之前提交或存储您的更改。

git merge --abort 相当于 git reset --merge
MERGE_HEAD 存在。

http://www.git-scm.com/docs/git-merge

git merge --abort

Abort the current conflict resolution process, and try to reconstruct
the pre-merge state.

If there were uncommitted worktree changes present when the merge
started, git merge --abort will in some cases be unable to
reconstruct these changes. It is therefore recommended to always
commit or stash your changes before running git merge.

git merge --abort is equivalent to git reset --merge when
MERGE_HEAD is present.

http://www.git-scm.com/docs/git-merge

︶葆Ⅱㄣ 2024-07-12 20:39:25

我认为这是您需要的 git reset 。

请注意,git revert 的含义与 svn revert 等含义非常不同 - 在 Subversion 中,还原将放弃您的(未提交的)更改,将文件从当前版本返回到当前版本。存储库,而 git revert 则“撤消”提交。

git reset 应该执行与 svn revert 相同的操作,即放弃不需要的更改。

I think it's git reset you need.

Beware that git revert means something very different to, say, svn revert - in Subversion the revert will discard your (uncommitted) changes, returning the file to the current version from the repository, whereas git revert "undoes" a commit.

git reset should do the equivalent of svn revert, that is, discard your unwanted changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文