如何将上游分支的更改合并到我的分支中?

11

我想从上游仓库的分支中拉取更改。我已经使用以下命令设置了上游仓库:

git remote add upstream http://URL-of-master-repo.git 

然后我尝试使用 pull 命令拉取更改。

git checkout my-local-branch
git fetch upstream remote-branch
git merge upstream/remote-branch

但是这些文件仍然没有出现在我的磁盘上,我却遇到了冲突:
Auto-merging minimal-build-config.cmake
CONFLICT (content): Merge conflict in minimal-build-config.cmake
Automatic merge failed; fix conflicts and then commit the result.

我该如何正确解决冲突,以便能够从上游分支获取文件?

2
如果你遇到了合并冲突,那么在尝试评估合并是否成功之前,你应该先解决它并提交。 - Tim Biegeleisen
@TimBiegeleisen 我解决了冲突并提交了更改,但仍然没有得到相关的文件,我重新执行了 git merge upstream/remote-branch 但只得到了 Already up-to-date - stdcerr
在 Git 合并过程中,你可能期望存在的更改/内容有时会被删除。 - Tim Biegeleisen
@TimBiegeleisen,上游分支添加了几个新文件...我不认为在合并过程中它们应该被删除。 - stdcerr
2个回答

14

一个好的做法是使用这个结构,看起来你已经这样做了,但为了澄清需要解释一下:

origin  https://github.com/your-username/forked-repository.git (fetch)
origin  https://github.com/your-username/forked-repository.git (push)
upstream    https://github.com/original-owner-username/original-repository.git (fetch)
upstream    https://github.com/original-owner-username/original-repository.git (push)

所以origin指的是您的分支,upstream则是原始代码库。然后使用

git fetch upstream

你将获得此输出

From https://github.com/original-owner-username/original-repository
 * [new branch]      master     -> upstream/master

现在你有一个名为upstream/master的分支,它跟踪原始仓库。

然后切换到你本地的fork分支并合并。

git checkout master
git merge upstream/master

如果您有冲突(似乎是这样的),请解决它们并提交更改。

来源


我解决了冲突并提交了更改,但仍未获得相关的文件。我重新执行了git merge upstream/remote-branch,但只得到了“已经是最新的”信息。 - stdcerr
执行 git fetchgit status 命令,查看 upstream/master 分支的状态。 - b-fg
我也遇到了类似的问题,但是在输出中并没有看到像上面建议的那样呈现upstream/master分支。 - Krishna Oza

0

//从上游获取 git fetch upstream git checkout main git merge upstream/main git push origin main


2
目前你的回答不够清晰,请编辑并添加更多细节,以帮助其他人理解它如何回答问题。你可以在帮助中心找到有关如何编写好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接