简体   繁体   English

如何在git上同步提交

[英]How to sync commits on git

I have a branch A on which I make a commit C1. 我有一个分支A,在该分支上进行提交C1。 I fork another branch from A; 我从A分支了另一个分支; call it B; 称它为B; and make an commit C2 (changes independent of C1). 并进行提交C2(更改独立于C1)。 Now later I go back to A and make some changes to C1. 现在稍后,我回到A并对C1进行一些更改。 I need to update B now with the latest changes so I do a "git rebase A" on B. This gives me conflicts. 我现在需要用最新的更改来更新B,因此我在B上执行了“ git rebase A”。这给我带来了冲突。 Is there an easy way to sync with A. I just want to overwrite C1 on branch B (actually B.C1 should be a mirror of A.C1). 有没有一种简单的方法可以与A同步。我只想覆盖分支B上的C1(实际上B.C1应该是A.C1的镜像)。 Or when rebasing, can I specify something like: "auto resolve conflicts by taking all changes from branch A" 或在重新定基时,是否可以指定类似内容:“通过从分支A进行所有更改来自动解决冲突”

I can always do the following. 我总是可以做到以下几点。 Create a new branch D from updated A and cherry-pick C2 on top. 从更新后的A和顶部的C2樱桃创建一个新的分支D。 When I do this and try to push my changes to gerrit (after updating D.C2) I get the error: 当我这样做并尝试将更改推送到gerrit(更新D.C2之后)时,出现错误:

To ssh://xxxx@gerrit.xxxx:29418/xxxxx
 ! [remote rejected] HEAD -> refs/for/master (no changes made)
error: failed to push some refs to 'ssh://xxxx@gerrit.xxxx:29418/xxxxx'

I am trying to understand why. 我试图了解原因。

Thanks in advance. 提前致谢。

When you are rebasing commits, you rewrite them completely. 在重新提交时,可以完全重写它们。 So you end up with completely new commit objects that are incompatible with the ones you had before. 因此,您最终得到了与以前不兼容的全新提交对象。 This is not a problem if you do it locally, but it becomes one when you have already pushed the old commits before (as in your case). 如果您在本地进行操作,这不是问题,但是如果您之前已经推送了旧的提交(例如您的情况),它就会变成一个问题。 Because now, you have the old commits on your remote repository, and rewritten commits in your local repository. 因为现在,您在远程存储库中有旧提交,而在本地存储库中有重写的提交。 That's why you should never rebase commits once you have published them anyway. 因此,一旦发布了提交,就永远不应该对提交进行基准调整。 The same thing happens with cherry-picking btw.: the commit you are cherry-picking gets rewritten completely and applied on top of your branch. 采摘btw也会发生同样的事情:采摘的提交将被完全重写并应用于分支的顶部。

Instead, just merge the branches using git merge A . 相反,只需使用git merge A 合并分支。 That way, you end up with commits that are compatible to whatever you might have published before. 这样,您最终将获得与以前发布的内容兼容的提交。

Now in your case, to repair the situation, do git push --force . 现在,根据您的情况,要修复这种情况,请执行git push --force This will overwrite whatever is on the remote repository's branch. 这将覆盖远程存储库分支上的所有内容。 So be careful with this and definitly don't use it when others are accessing the repository too (since their commits will then break too). 因此,请谨慎使用它,并且当其他人也访问存储库时,请不要使用它(因为它们的提交也将因此中断)。 But I guess for gerrit it's okay, as it will update the change request. 但是我猜对于gerrit来说还可以,因为它将更新更改请求。

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

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