如何同步同一个 Git 存储库中的两个分支?
这是我经常遇到的一个常见工作流程障碍:
master 是我们的“稳定”分支
$ git status
# On branch master
nothing to commit (working directory clean)
在分支上创建一个模块
$ git checkout -b foo
$ echo "hello" > world
$ git add .
$ git commit -m "init commit for foo module"
$ git checkout master
$ git merge foo
在 master 或其他分支上工作
在接下来的几周内,更多代码将直接提交给 master以及其他分支机构。 foo
分支在这段时间内将保持不变。
在 foo 分支上恢复工作/进行更新
$ git checkout foo
哦不! foo 已经过时了!我明白为什么,但我确实需要foo恢复同步。
问题
如何从 master 分支获取最新内容?
Here's a common workflow hurdle I encounter often:
master is our "stable" branch
$ 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
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.
Resume work/make updates on the foo branch
$ git checkout foo
Oh no! foo is massively out of date! I understand why, but I do need foo back in sync.
The question
How do I get the latest contents from the master branch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您不需要周围的分支:
如果您已将 foo 合并回 master,请使用“gitbranch -d foo”来终止主题分支,然后在未来当你需要再次破解它时。
如果您确实需要分支:
您可以根据主分支重新调整您的主题分支:
或者:
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.
If you do need the branch around:
You can rebase your topic branch against the master branch:
Or:
变基是将一系列提交移动或组合到新的基础提交的过程。在功能分支工作流程的上下文中,变基是最有用且最容易可视化的。一般过程可以如下所示:
下面的示例将 git rebase 与 git merge 结合起来,以维护线性项目历史记录。这是一种确保快速合并的快速简便方法。
在我们的功能中间,我们意识到我们的项目中存在安全漏洞
将修补程序合并到主版本后,我们有一个分叉的项目历史记录。我们将把功能分支与 rebase 集成起来,以维持线性历史,而不是简单的 git 合并:
这会将新功能移动到 master 的顶端,这让我们可以从 master 进行标准的快进合并:
取自 < a href="https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase" rel="noreferrer">Atlassian Git Rebase 教程
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.
In the middle of our feature, we realize there’s a security hole in our project
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:
This moves new-feature to the tip of master, which lets us do a standard fast-forward merge from master:
Taken from Atlassian Git Rebase Tutorial
我使用以下内容来合并两个分支(我的和你的)的更改,并同步两个分支以继续工作。这似乎有效。有人看到它有问题吗?
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?
亚当的回答很好。
此外,对于那些使用 IntelliJ 并需要有关变基和合并帮助的人,这里有一个 YouTube 视频的链接。这将帮助您直观地了解 rebase 的工作原理。
https://www.youtube.com/watch?v=Nftif2ynvdA
Adam answer is good one.
Additionally, for those who are using IntelliJ and need help regarding rebase and merge, here is a link of youtube video. This will help you to visually understand how rebase works.
https://www.youtube.com/watch?v=Nftif2ynvdA