git Bitbucket - 与另一个分支同步分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31471790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 11:12:27  来源:igfitidea点击:

Bitbucket - Syncing Branch with another Branch

gitsyncbitbucketgit-branchgit-merge

提问by Gary

This may be a duplicate of How to keep a git branch in sync with master(I assume I can just replace Master with the other branch), but it's vital that this works and doesn't merge the wrong way around so I need to make sure.

这可能是How to keep a git branch in sync with master 的一个副本(我假设我可以用另一个分支替换 Master),但是这很重要并且不会以错误的方式合并,所以我需要做当然。

Scenario

设想

There was a branch called v1, and I created a branch off of that called v1_adminui. I've made around 10 commits to my branch v1_adminui, but a major improvement to another part of the project has been made in v1, so I want to Sync that change with my current branch.

有一个名为 的分支v1,我创建了一个名为 的分支v1_adminui。我已经对我的分支进行了大约 10 次提交v1_adminui,但是对项目的另一部分进行了重大改进v1,因此我想将该更改与我当前的分支同步。

I believe the following would do it:

我相信以下方法可以做到:

git checkout v1
git pull
git checkout v1_adminui
git merge v1

Please can you confirm if this is correct? If not, any help would be appreciated on how to accomplish this.

请您确认一下是否正确?如果没有,将不胜感激有关如何实现这一点的任何帮助。

回答by nwinkler

Since you are the only one working on this branch, you should use rebaseinstead of merge.

由于您是唯一一个在此分支上工作的人,因此您应该使用rebase代替merge

# Get the base branch
git checkout v1

# Pull in any changes to make sure you have the latest version
git pull

# Check out your branch
git checkout v1_adminui

# Rebase your changes on top of the v1 changes
git rebase v1

# Optionally push your rebased branch
git push origin v1_adminui

You might have to use the --forceoption when pushing in the last step. Since you're rewriting the branch's history with the rebase, the remote branch will have a different history. Only do this if no-one else is using this branch!

--force在推进最后一步时,您可能必须使用该选项。由于您使用 rebase 重写分支的历史记录,因此远程分支将具有不同的历史记录。只有在没有其他人使用这个分支时才这样做!