简体   繁体   English

git合并后同步2个分支

[英]Syncing 2 branches after git merge

How do I keep 2 Git branches in sync?如何保持 2 个 Git 分支同步?

I currently have a master branch and a gh-pages branch as well as the respective remotes.我目前有一个master分支和一个gh-pages分支以及各自的遥控器。 Both, master and gh-pages branch shared the same commit id whenever I committed as follows:每当我按如下方式提交时, mastergh-pages分支都共享相同的提交 ID:

git checkout master
git commit -am "message"
git checkout gh-pages
git merge master

My log looked like so (achieved with git log --graph --oneline --all --decorate ):我的日志看起来像这样(使用git log --graph --oneline --all --decorate ):

单跟踪图

After a merge conflict, which I resolved, each merge receives its own commit as such:在我解决了合并冲突之后,每个合并都会收到自己的提交:

双轨图

My question is, how can I align the 2 branches so that merges will happen as previously on a single 'graph-track'?我的问题是,如何对齐 2 个分支,以便像以前一样在单个“图形轨道”上进行合并?

I have tried to pull according to this answer , but to no avail.我试图根据这个答案pull ,但无济于事。

It looks like commit 50b6dec was added to the gh-pages branch, and commit e43c01f was made on master at the same time, and this is what caused the merge conflict.它看起来像犯50b6dec加入到gh-pages分支,并承诺e43c01f被上进行master在同一时间,这是什么原因造成合并冲突。

Because of that, gh-pages had 1 commit that master did not have.因此, gh-pages有 1 个master没有的提交。 So any time you merged after that, Git was not able to do a fast-forward merge.因此,此后任何时候合并时,Git 都无法进行快进合并。 (The fast-forward merges is what gave you the linear history you had before.) Thus, the solution is: (快进合并为您提供了以前的线性历史。)因此,解决方案是:

git checkout master
git merge gh-pages

To prevent this from happening again in the future, change your process to this:为了防止将来再次发生这种情况,请将您的流程更改为:

git checkout master
git commit -am "message"
git checkout gh-pages
git merge --ff-only master

The --ff-only flag will abort the merge with an error if gh-pages has any commits that master doesn't have.如果gh-pagesmaster没有的任何提交,则--ff-only标志将中止合并并出现错误。


If you want to rewrite history so that it is as if this never happened, you can do that too.如果你想改写历史,让它仿佛从未发生过,你也可以这样做。

First, ensure you are on master :首先,确保您在master

git checkout master

Now rebase:现在变基:

git rebase 50b6dec

You'll have to resolve that conflict again.您将不得不再次解决该冲突。 When finished, do:完成后,执行:

git rebase --continue

When the rebase is complete, you will have to force-update origin :当 rebase 完成后,你必须强制更新origin

git push --force-with-lease origin master

You'll also have to move the gh-pages branch.您还必须移动gh-pages分支。 Do:做:

git branch -f gh-pages master
git push --force-with-lease origin gh-pages

This will give you a history that looks something like this:这会给你一个看起来像这样的历史:

* f218f70' (HEAD -> master) Added Scatterplot functionality to check ratings
* cb79b79' Squashed bug
* fcf0d8a' Small change
* e43c01f' Small change
* 50b6dec Small change
* a217267 Small change

Obligatory warning : Since a rebase rewrites history, this can be dangerous / disruptive for anyone else working on this branch.强制性警告:由于 rebase 会重写历史记录,因此对于在此分支上工作的任何其他人来说,这可能是危险的/具有破坏性的。 Be sure you clearly communicate what you have done with anyone you are collaborating with.确保您与正在合作的任何人清楚地传达您所做的事情。

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

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