简体   繁体   English

如何撤消存在冲突的 git 合并

[英]How to undo a git merge with conflicts

I am on branch mybranch1 .我在分支mybranch1 mybranch2 is forked from mybranch1 and changes were made in mybranch2 . mybranch2是从mybranch1派生出来的,并且在mybranch2中进行了mybranch2

Then, while on mybranch1 , I have done git merge --no-commit mybranch2 It shows there were conflicts while merging.然后,在mybranch1 ,我做了git merge --no-commit mybranch2它显示合并时存在冲突。

Now I want do discard everything (the merge command) so that mybranch1 is back to what it was before.现在我想丢弃所有内容( merge命令),以便mybranch1恢复到以前的状态。 I have no idea how do I go about this.我不知道我该怎么做。

Latest Git:最新的 Git:

git merge --abort

This attempts to reset your working copy to whatever state it was in before the merge.这会尝试将您的工作副本重置为合并之前的任何状态。 That means that it should restore any uncommitted changes from before the merge, although it cannot always do so reliably.这意味着它应该从合并之前恢复任何未提交的更改,尽管它不能总是可靠地这样做。 Generally you shouldn't merge with uncommitted changes anyway.通常,您无论如何都不应该合并未提交的更改。

Prior to version 1.7.4: 1.7.4 之前的版本:

git reset --merge

This is older syntax but does the same as the above.这是较旧的语法,但与上述语法相同。

Prior to version 1.6.2: 1.6.2 之前的版本:

git reset --hard

which removes all uncommitted changes, including the uncommitted merge.这将删除所有未提交的更改,包括未提交的合并。 Sometimes this behaviour is useful even in newer versions of Git that support the above commands.有时,即使在支持上述命令的较新版本的 Git 中,这种行为也很有用。

Actually, it is worth noticing that git merge --abort is only equivalent to git reset --merge given that MERGE_HEAD is present.实际上,值得注意的是git merge --abort仅等效于git reset --merge因为存在MERGE_HEAD This can be read in the git help for merge command.这可以在合并命令的 git 帮助中阅读。

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 , so they are not only old and new syntax for the same thing .合并失败后,当没有MERGE_HEAD ,可以使用git reset --merge撤消失败的合并,但不一定使用git merge --abort因此它们不仅是同一事物的新旧语法

Personally I find git reset --merge much more useful in everyday work.我个人认为git reset --merge在日常工作中更有用。

假设您使用的是最新的 git,

git merge --abort

If using latest Git,如果使用最新的 Git,

git merge --abort

else this will do the job in older git versions否则这将在较旧的 git 版本中完成这项工作

git reset --merge

or或者

git reset --hard

There are two things you can do first undo merge by command有两件事你可以先通过命令撤消合并

git merge --abort

or或者

you can go to your previous commit state temporarily by command你可以通过命令临时进入你之前的提交状态

git checkout 0d1d7fc32 

Sourcetree源码树

If you not commit your merge, then just double click on another branch (=checkout) and when sourcetree ask you about discarding all changes then agree如果您不提交合并,则只需双击另一个分支(= checkout),当 sourcetree 询问您是否放弃所有更改时,请同意

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM