Git 合并 - 更新功能和主控

Git merge - update both feature and master

在我的 Git 存储库中,我有一个功能分支和一个主分支。我想合并两者。然而,为了方便起见,我想保留功能分支并继续开发功能而不创建新分支。也就是说,我想有以下结构:

Master     M0 --- M1 --- M2 --- M3 --- M4
            \                  /  \
Feature      F0 ---- F1 ---- F2    F3 --- F4

功能分支的最新状态 F4 应包括对 master 的所有更改,直至状态 M3

我的理解是 git merge 完全可以做到这一点。然而,在另一次尝试之后,我收到消息说功能分支是主分支后面的一对提交。这是预期的还是某处有错误?如果这是预期的行为,我该如何实现我想要的?

可以先将master合并到feature分支:

$ git switch feature
$ git merge master # Apply all changes in master to feature
$ # (At this step, you might want to do some testing to make sure the merge didn't cause any new bugs)
$ git switch master
$ git merge feature # Apply all changes in the feature branch back to master

事实上,您应该始终 将 master 合并到任何其他分支,然后再将结果合并回 master。通过这种方式,您可以更正功能分支中的任何合并冲突,同时确保 master 始终保持正常运行。

只需获取 M2 提交的哈希并执行命令:

git switch featureBranch
git merge hash-of-master-commit-I-want_to-merge

正在回复评论

要获取提交的哈希值,请执行以下操作:

git switch master # switch to master branch
git log -10 # view last 10 commits

一次提交的示例输出:

commit 667c0a6e4e40f10d3b86178eb51b209aa73ccf67
Author: Michal Turczyn <my@email.com>
Date:   Wed Jan 13 15:03:54 2021 +0100

    MASTER Added 2nd method

第一行包含提交的散列。与合并命令一起使用:

git switch featureBranch
git merge 667c0a6e4e40f10d3b86178eb51b209aa73ccf67

这将合并 maaster 提交到指定的提交。随心所欲