【发布时间】:2026-02-04 10:55:01
【问题描述】:
我们使用 git 并有一个 master 分支和 developer 分支。我需要添加一个新功能,然后将提交变基到 master,然后将 master 推送到 CI 服务器。
问题是,如果我在 rebase 期间发生冲突,我无法在 rebase 完成后推送到我的远程开发人员分支(在 Github 上),直到我拉出我的远程分支。这会导致重复提交。当没有冲突时,按预期工作。
问题:在 rebase 和冲突解决之后,如何在不创建重复提交的情况下同步本地和远程开发人员分支
设置:
// master branch is the main branch
git checkout master
git checkout -b myNewFeature
// I will work on this at work and at home
git push origin myNewFeature
// work work work on myNewFeature
// master branch has been updated and will conflict with myNewFeature
git pull --rebase origin master
// we have conflicts
// solve conflict
git rebase --continue
//repeat until rebase is complete
git push origin myNewFeature
//ERROR
error: failed to push some refs to 'git@github.com:ariklevy/dropLocker.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
// do what git says and pull
git pull origin myNewFeature
git push origin myNewFeature
// Now I have duplicate commits on the remote branch myNewFeature
编辑
所以听起来这会破坏工作流程:
开发者 1 正在开发 myNewFeature developer2 正在开发 hisNewFeature 都使用master作为主分支
developer2 将 myNewFeature 合并到 hisNewFeature 中
developer1 变基,解决冲突,然后为 myNewFeature 强制推送到远程分支
几天后,developer2 再次将 myNewFeature 合并到 hisNewFeature 中
这会不会导致其他开发者讨厌 developer1?
【问题讨论】:
-
不是一个解决方案,只是一个想法。谁是
we?你的团队不仅仅是你吗?they说(比我了解更多的人)如果你分享你的代码,那么你不应该使用rebase。你为什么不直接做git pull和git merge? -
对不起,我们 = 开发团队
-
所以是的,当你共享代码时可能不应该变基。这可能会导致您不得不执行下面列出的操作(
force推送) -
哦,对不起,当他们说
rewriting history时,那是rebase -
正如他所说,这是他自己的开发分支,因此除非有人将其合并,否则这应该不是问题。
标签: git merge rebase git-merge git-rebase