如何将远程分支合并到本地分支?

5
我们的项目有一个develop分支。我在develop分支的基础上创建了一个bug-fix分支,并更改了一个文件a.java。当我在修复错误时,我知道另一个人也更改了a.java并将其合并到develop分支。
现在我已经修复了这个bug,我想将我的代码合并到develop分支之前,我想拉取更改(因为其他人也更改了a.java)到我的本地分支以测试是否有效,我是对的吗?如何做到这一点?
首先,我通过“git pull”命令拉取了所有代码,并且我在我的bug-fix分支中。然后运行“git merge develop”命令,输出为空。然后我运行“git merge origin/develop”命令。它输出以下错误。
Updating 46f689b..3011608
error: Your local changes to the following files would be overwritten by merge:
        projectA/a.java
Please commit your changes or stash them before you merge.
Aborting

我该如何将develop分支合并到我的工作目录中?谢谢。


只需按照错误提示的建议,进行提交或隐藏您的工作即可。 - Tim Biegeleisen
4个回答

5

通常在这种情况下,我喜欢做以下事情:

git stash # Stash your local changes
git pull  # Update code
git stash apply # Merge your local changes

现在,为了澄清,如果你需要同步另一个分支,你可以在git pull之前切换分支。从那里,你可以回到你的bug-fixes分支并运行git stash apply,然后与你的develop分支合并。


2

因为您对该文件进行了一些更改。

Please commit your changes or stash them before you merge.

只需按照以下步骤进行操作:
# save current work state
git stash
# merge from develop
git merge origin/develop
# recover current work state (get the last item of stash)
git stash pop

1

由于您还没有将本地提交推送到错误分支,因此需要在更新的(由其他开发人员更新的)origin/bug分支之上对它们进行变基

为此,请确保您已经先设置配置

git --global config pull.rebase true
git --global config rebase.autoStash true

(只需一次,在任何文件夹中完成)

然后,在您当前的分支中,您可以通过简单的方式包含其他人的贡献:

git pull.

现在,请确保所有内容都已提交,并执行以下步骤:
git checkout develop
git pull

最终,合并开发。
git checkout bug
git merge develop

0

在合并其他更改之前,您需要提交或存储项目A / a.java中的更改。

提交示例:

$ git add projectA/a.java
$ git commit -m "my change message"
$ git pull origin/develop

示例代码:

$ git stash
$ git pull origin/develop
$ git stash apply

他在询问如何合并,而不是如何拉取。 - Green

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