git 无法将上游更改合并回我的分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5772908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:45:16  来源:igfitidea点击:

Trouble merging upstream changes back into my branch

gitmergecommitconflictpartial

提问by anonymous

I'm running into conflicts while trying to merge upstream changes back into my branch and I'm not sure how to resolve them.

我在尝试将上游更改合并回我的分支时遇到冲突,我不确定如何解决它们。

I created my own fork. I cloned it. I made changes to the branch on my fork, committed, and pushed. But then the main fork updated, and I tried to update my own fork by merging upstream in like so:

我创建了自己的叉子。我克隆了它。我对分支上的分支进行了更改、提交和推送。但是随后主分支更新了,我尝试通过合并上游来更新我自己的分支,如下所示:

$ cd repo-name
$ git remote add upstream git://github.com/username/repo-name.git
$ git fetch upstream
$ git merge upstream/master

The merge says that there's some problem with a file and auto-merging doesn't work. It tells me to fix it myself and re-merge. So I actually went to the (upstream) repository on GitHub of the main fork and copied all the code of the new file into the file on my fork, and tried to merge again. Then, git gives me this error:

合并说文件有问题,自动合并不起作用。它告诉我自己修复它并重新合并。所以我实际上去了主分支的 GitHub 上的(上游)存储库,并将新文件的所有代码复制到我的分支上的文件中,并尝试再次合并。然后,git 给了我这个错误:

fatal: 'merge' is not possible because you have unmerged files. Please, fix them up in the work tree, and then use 'git add/rm ' as appropriate to mark resolution and make a commit, or use 'git commit -a'.

致命:“合并”是不可能的,因为您有未合并的文件。请在工作树中修复它们,然后根据需要使用“git add/rm”来标记分辨率并进行提交,或者使用“git commit -a”。

Is there some argument I'm leaving out? Am I doing something stupid? What does it mean by "unmerged files?" Isn't the whole point of merging to merge files? Do I have to commit my changes before I merge?

有什么我要离开的论点吗?我在做蠢事吗?“未合并的文件”是什么意思?不是合并以合并文件的全部意义吗?我是否必须在合并之前提交更改?

回答by ?imon Tóth

What you are seeing means that automatic merge could not resolve the conflicts in the files. You need to resolve these conflicts manually. Run git mergetoolor git gui.

您所看到的意味着自动合并无法解决文件中的冲突。您需要手动解决这些冲突。运行git mergetoolgit gui

回答by cbare

The "git merge" command tries to incorporate changes from another branch onto the present branch. If the merge is clean, meaning no conflicts, it will commit. Since your merge did have conflicts, it didn't commit. You need to resolve the conflict.

“git merge”命令尝试将来自另一个分支的更改合并到当前分支。如果合并是干净的,意味着没有冲突,它将提交。由于您的合并确实存在冲突,因此它没有提交。您需要解决冲突。

Pulling the copy from the upstream repo is one way to do that - by accepting the upstream repo's version. You can do that within git using "git checkout --theirs conflicting_file.txt"

从上游存储库中提取副本是一种方法 - 通过接受上游存储库的版本。您可以使用“git checkout --theirs conflicting_file.txt”在git中执行此操作

Editing the file to get it into the shape you want is another way.

编辑文件以使其成为您想要的形状是另一种方法。

Once it's fixed, you need to add using "git add conflicting_file.txt" then commit. Then your working copy is clean and ready for more hacking. Good luck.

一旦修复,您需要使用“git add conflicting_file.txt”添加然后提交。然后你的工作副本是干净的,准备好进行更多的黑客攻击。祝你好运。

回答by Jakub Nar?bski

In Git there are cases merge refusesto even start in order to protect your local changes. This may happen in two cases:

在 Git 中,有些情况下合并甚至拒绝启动以保护您的本地更改。这可能在两种情况下发生:

  • You have uncommitted changesin you repository that conflict with merge. The git will refuse to do a merge with the following message:

    error: Your local changes to the following files would be overwritten by merge:
            foo
    Please, commit your changes or stash them before you can merge.
    Aborting
    

    Then you have to either commit the changes first (git commit -aor git add+ git commit), or stash them away with git stash save.

  • You are in the middle of some unfinishedmerge-y operation. There was some conflict, for example

    Auto-merging foo
    CONFLICT (content): Merge conflict in foo
    Automatic merge failed; fix conflicts and then commit the result.
    

    and you have not finished resolving conflicts (by editing files and marking them as resolved with git add, or using some graphical merge tool via git mergetool) and didn't create a final merge commit with git commit -a, or aborted the merge with git reset --hard(NOTE:this will discard all you changes, and you will loose work done on resolving conflicts!!!).

    Or you have just run second git mergetoo fast, or used git mergeinstead of git committo create a merge commit.

    error: 'merge' is not possible because you have unmerged files.
    hint: Fix them up in the work tree,
    hint: and then use 'git add/rm ' as
    hint: appropriate to mark resolution and make a commit,
    hint: or use 'git commit -a'.
    fatal: Exiting because of an unresolved conflict.
    

    Resolve conflicts as described e.g. in old Fun with completing a mergearticle by Junio C Hamano and finalize a merge with git commit, or discard a merge, or stash it away. Then if you meant to create this second merge, you can do it.

  • 您的存储库中有与 merge 冲突的未提交更改。git 将拒绝使用以下消息进行合并:

    error: Your local changes to the following files would be overwritten by merge:
            foo
    Please, commit your changes or stash them before you can merge.
    Aborting
    

    然后,您必须首先提交更改(git commit -agit add+ git commit),或者使用git stash save.

  • 您正处于一些未完成的合并操作中。有一些冲突,例如

    Auto-merging foo
    CONFLICT (content): Merge conflict in foo
    Automatic merge failed; fix conflicts and then commit the result.
    

    并且您还没有完成解决冲突(通过编辑文件并将它们标记为已解决git add,或使用一些图形合并工具通过git mergetool)并且没有创建最终合并提交git commit -a,或中止合并git reset --hard注意:这将丢弃您的所有内容更改,您将失去解决冲突的工作!!!)。

    或者你只是跑得太快git merge了,或者用来git merge代替git commit创建合并提交。

    error: 'merge' is not possible because you have unmerged files.
    hint: Fix them up in the work tree,
    hint: and then use 'git add/rm ' as
    hint: appropriate to mark resolution and make a commit,
    hint: or use 'git commit -a'.
    fatal: Exiting because of an unresolved conflict.
    

    解决冲突,例如在 old Fun 中完成Junio C Hamano 的一篇合并文章,并完成与 的合并git commit,或放弃合并,或将其隐藏起来。然后,如果您打算创建第二个合并,则可以执行此操作。

Sidenote:by default git-aware shell prompt shows if you are in the middle of merge, rebase or applying patches (git amoperation). You can also configure it to show if the working directory is dirty (different from latest version, i.e. HEAD).

旁注:默认情况下,git-aware shell 提示会显示您是否处于合并、rebase 或应用补丁(git am操作)的中间。您还可以配置它以显示工作目录是否脏(不同于最新版本,即 HEAD)。

回答by alternative

Run git commit(after adding the files) the second time, not git merge.

git commit第二次运行(添加文件后),而不是git merge.

Also the conflict resolution will create files to help you merge. See also git mergetool.

此外,冲突解决将创建文件来帮助您合并。另见git mergetool

回答by Robin Green

After you've resolved a merge, you need to use git addto add the files you've changed to the index, and then commit (like the message says). This says to git "Yes, I really do want to make these changes".

解决合并后,您需要使用git add将已更改的文件添加到索引,然后提交(如消息所述)。这对 git 说“是的,我真的很想进行这些更改”。

Remember, always use git addbefore committing (either normally or committing a merge), if you're using the command line interface. Frontends like magit can streamline this for you so you don't have to worry about typing "git add" every time.

请记住,git add如果您使用命令行界面,请始终在提交之前使用(通常或提交合并)。像 magit 这样的前端可以为您简化这一过程,因此您不必担心每次都输入“git add”。