如何同步同一个 Git 存储库中的两个分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4010962/
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
How do I synchronize two branches in the same Git repository?
提问by ma?ek
Here's a common workflow hurdle I encounter often:
这是我经常遇到的一个常见的工作流程障碍:
masteris our "stable" branch
master是我们的“稳定”分支
$ git status
# On branch master
nothing to commit (working directory clean)
Create a module on a branch
在分支上创建模块
$ git checkout -b foo
$ echo "hello" > world
$ git add .
$ git commit -m "init commit for foo module"
$ git checkout master
$ git merge foo
Do work on master or other branches
在 master 或其他分支上工作
Over the next couple weeks, more code will be committed to master directly and by other branches. The foo
branch will go untouched for this time period.
在接下来的几周内,更多的代码将直接和其他分支提交给 master。在foo
这段时间内,该分支将保持不变。
Resume work/make updates on the foobranch
在foo分支上恢复工作/进行更新
$ git checkout foo
Oh no! foois massively out of date! I understand why, but I doneed fooback in sync.
不好了!foo已经过时了!我明白为什么,但我确实需要foo同步。
The question
问题
How do I get the latest contents from the masterbranch?
如何从master分支获取最新的内容?
回答by Adam Vandenberg
If you don't need the branch around:
如果您不需要周围的分支:
If you've merged foo back to master, "git branch -d foo" to kill the topic branch, and then "checkout -b foo" in the future when you need to hack on it again.
如果您已将 foo 合并回 master,请使用“git branch -d foo”来终止主题分支,然后在以后需要再次对其进行 hack 时使用“checkout -b foo”。
If you do need the branch around:
如果您确实需要周围的分支:
You can rebase your topic branch against the master branch:
您可以根据主分支重新设置主题分支:
git checkout foo
git rebase master
Or:
或者:
git rebase master foo
回答by Mauricio Gracia Gutierrez
Rebasing is the process of moving or combining a sequence of commits to a new base commit. Rebasing is most useful and easily visualized in the context of a feature branching workflow. The general process can be visualized as the following:
重新定位是将一系列提交移动或组合到新的基础提交的过程。在功能分支工作流的上下文中,重新定位是最有用且易于可视化的。可以将一般过程可视化如下:
The example below combines git rebase with git merge to maintain a linear project history. This is a quick and easy way to ensure that your merges will be fast-forwarded.
下面的例子结合了 git rebase 和 git merge 来维护一个线性的项目历史。这是确保您的合并快进的快速简便的方法。
# Start a new feature
git checkout -b new-feature master
# Edit files
git commit -a -m "Start developing a feature"
In the middle of our feature, we realize there's a security hole in our project
在我们的功能中间,我们意识到我们的项目中有一个安全漏洞
# Create a hotfix branch based off of master
git checkout -b hotfix master
# Edit files
git commit -a -m "Fix security hole"
# Merge back into master
git checkout master
git merge hotfix
git branch -d hotfix
After merging the hotfix into master, we have a forked project history. Instead of a plain git merge, we'll integrate the feature branch with a rebase to maintain a linear history:
将修补程序合并到 master 后,我们有一个分叉的项目历史。我们将使用 rebase 集成功能分支,而不是简单的 git merge,以维护线性历史记录:
git checkout new-feature
git rebase master
This moves new-feature to the tip of master, which lets us do a standard fast-forward merge from master:
这将新功能移到 master 的尖端,这使我们可以从 master 进行标准的快进合并:
git checkout master
git merge new-feature
Taken from Atlassian Git Rebase Tutorial
回答by user74279
I use the following to combine changes from two branches (mine and yours) and to synchronize both branches for continued work. This seems to be working. Does anyone see a problem with it?
我使用以下内容组合来自两个分支(我的和你的)的更改,并同步两个分支以继续工作。这似乎有效。有没有人看到它有问题?
git checkout mine # make sure I'm on my branch
git commit -a # commit changes
git push origin mine
git checkout yours # switch to your branch
git pull origin yours # get changes you've committed & pushed
git checkout mine
git merge yours # merge your changes into mine
git push origin mine
git checkout yours
git rebase mine # set your branch to the merged result
git push origin yours # push the merged result up to your branch on origin
git checkout mine # get back to my branch