如何同步同一个 Git 存储库中的两个分支?

发布于 2024-09-29 01:19:35 字数 682 浏览 11 评论 0原文

这是我经常遇到的一个常见工作流程障碍:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜光 2024-10-06 01:19:35

如果您不需要周围的分支:

如果您已将 foo 合并回 master,请使用“gitbranch -d foo”来终止主题分支,然后在未来当你需要再次破解它时。

如果您确实需要分支:

您可以根据主分支重新调整您的主题分支:

git checkout foo
git rebase master

或者:

git rebase master 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:

git checkout foo
git rebase master

Or:

git rebase master foo
溺渁∝ 2024-10-06 01:19:35

变基是将一系列提交移动或组合到新的基础提交的过程。在功能分支工作流程的上下文中,变基是最有用且最容易可视化的。一般过程可以如下所示:

Git Rebase 视觉解释

下面的示例将 git rebase 与 git merge 结合起来,以维护线性项目历史记录。这是一种确保快速合并的快速简便方法。

# Start a new feature
git checkout -b new-feature master
# Edit files
git commit -a -m "Start developing a feature"

在我们的功能中间,我们意识到我们的项目中存在安全漏洞

# 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

将修补程序合并到主版本后,我们有一个分叉的项目历史记录。我们将把功能分支与 rebase 集成起来,以维持线性历史,而不是简单的 git 合并:

git checkout new-feature
git rebase master

这会将新功能移动到 master 的顶端,这让我们可以从 master 进行标准的快进合并:

git checkout master
git merge new-feature

取自 < 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:

Git Rebase visual explanation

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.

# 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:

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:

git checkout master
git merge new-feature

Taken from Atlassian Git Rebase Tutorial

┾廆蒐ゝ 2024-10-06 01:19:35

我使用以下内容来合并两个分支(我的和你的)的更改,并同步两个分支以继续工作。这似乎有效。有人看到它有问题吗?

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

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
旧人哭 2024-10-06 01:19:35

亚当的回答很好。

此外,对于那些使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文