在“git合并"后保留提交历史 [英] Keep commits history after a 'git merge'

查看:43
本文介绍了在“git合并"后保留提交历史的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我处理两个不同的特性时(在从 master 创建的两个不同分支上),当我继续合并时,我没有提交历史是很烦人的.

When I work on two different features (on two different branches created from the master) it is quite annoying that I will not have the commit history when I proceed with merging.

我会更好地解释.当我完成 Branch-A 的工作后,我将其合并到 master 中.这很好,如果我 git log 我会看到我在 Branch-A 上所做的所有提交.

I'll explain better. When I finish work on Branch-A, I merge it into master. And that's fine, if I git log I see all the commits I made on Branch-A.

相反,当我完成Branch-B 的工作并尝试将其合并到master(之后Branch-A 已经合并),我必须为合并指定一个提交消息(而对于第一个分支,我没有被问到任何问题).并且在合并到master之后,如果我输入git log,我在我的历史中看不到Branch-B的提交em>master 分支

Instead, when I finish work on Branch-B and I try to merge it to master (after that Branch-A has been already merged), I have to specify a commit message for the merging (while for the first branch I have not been asked anything). And after the merging to master, if I type git log, I cannot see the commits of the Branch-B in the history of my master branch

假设我有

**Branch A**

commit 09b2unfas9d781n2e
    Add more stuff

commit 8uj8masd89jas898a
    Add stuff

**Branch B**

commit 09b2unfas9d781n2e
    Add feature setting

commit 8uj8masd89jas898a
    Add feature

我吃完了

**Master**

commit 6hf6h8hd871udjkdn
Merge: 09b2un 34osd6
    Merge branch 'Branch-B' into master

commit 09b2unfas9d781n2e
    Add more stuff

commit 8uj8masd89jas898a
    Add stuff

commit 34osd62dhc91123j8
    I'm a previous commit from 'master'.
    The last one before branching...

虽然我想获得类似的东西:

**Master**

commit 09b2unfas9d781n2e
    Add feature setting

commit 8uj8masd89jas898a
    Add feature

commit 09b2unfas9d781n2e
    Add more stuff

commit 8uj8masd89jas898a
    Add stuff

commit 34osd62dhc91123j8
    I'm a previous commit from 'master'.
    The last one before branching...

...这将更准确地反映执行提交的历史.

... that would reflect more exactly the history of the performed commits.

我不明白为什么我可以只保留两个分支之一的历史记录.

I don't get why I can keep the history from just one of the two branches.

如果没有那些隐藏/省略合并提交的真实历史的合并提交,我如何保持一切清晰?

How can I keep everything clear without those merge commits that hide/omit the real history of the merged commits?

推荐答案

看起来第一次合并是快进的,第二次合并是三路合并.

It looks like the first merge was a fast-forward, and the second one was a three-way merge.

说明

Git 有两个版本的合并:快进和三向.(还有其他版本,但这不是这里发生的情况.)默认行为是在可能的情况下进行快进合并,否则进行三路合并.

Git has two versions of merge: fast-forward and three-way. (There are other versions, but that is not what happened here.) The default behavior is to do a fast-forward merge when possible, and otherwise do a three-way merge.

快进合并(您可以使用选项 --ff-only 强制此行为,这将导致在快进不可能时合并失败)可以在提交时发生正在合并的分支在其历史记录中具有当前位置.例如:

A fast-forward merge (you can force this behavior with the option --ff-only, which will cause the merge to fail when fast-forward is impossible) can take place when the commit that is being merged has the current position of the branch in its history. For example:

A - B - C - D <-master
             
              E - F - G <- branch-a

执行git merge(使用默认设置)将导致

Excecuting git merge (with default settings) will result in

A - B - C - D - E - F - G <- branch-a <-master

您也没有机会编辑合并提交,因为没有.但是,一旦发生这种情况,您的另一个分支将与 master 不同(不仅仅是领先):

You will also not get a chance to edit the merge commit because there is none. However, once this happens, your other branch will diverge from master (not just be ahead):

A - B - C - D  - E - F - G <-master
                  
                   E1 - E2 <- branch-b

因此,Git 不能只是将 master 的指针从 G 移动到 E2 因为这将摆脱在 FG.取而代之的是三路合并,这会创建一个具有两个父级的提交,并且还有一个提交消息.现在,master 可以移动到这个提交.(注意,在这种情况下,master 和 branch-b 不会指向同一个提交.

Therefore, Git cannot just move the pointer of master from G to E2 because that will get rid of the changes that were made in F and G. Instead a three-way merge happens, which create a commit that has two parents, and also has a commit message. Now, master can be moved to this commit. (Notice that in this situation, master and branch-b do NOT point to the same commit.

A - B - C - D  - E - F - G - H <-master
                         /
                   E1 - E2 <- branch-b

如果您想拥有线性历史记录,那么您需要使用 rebase,但请注意,如果其他人看到您的分支提交,这可能会导致超出本答案范围的问题.使用变基将涉及两个步骤,变基和快进合并.因此,与其合并,不如先在 branch-b 上执行以下操作,git rebase master.这会创建作为旧提交副本的新提交,即相同的更改集、作者信息和消息,但新的提交者信息和父历史记录.(我在插图中将提交称为 E1' 和 E2' 以表示它们只是副本.)旧提交将一直存在,直到它们被垃圾收集,但除非您查看引用日志,否则将无法访问.

If you want to have a linear history then you need to use rebase, but be forewarned that if anybody else has seen your branch commits this may lead to issues that are beyond the scope of this answer. Using rebase will involve two steps, rebasing and then fast-forward merge. So, instead of merging you first execute the following while on branch-b, git rebase master. This creates new commits that are copies of the old commits, i.e., the same change-set, author information and message, but new committer information and parent history. (I call the commits E1' and E2' in the illustration to indicate that they are just copies.) The old commits will exist until they are garbage collected, but will not be reachable unless you look at the reflog.)

A - B - C - D  - E - F - G <-master
                         
                   E1 - E2  
                            E1' - E2' <- branch-b

执行git checkout master;git merge --ff-only branch-b 现在会将您的更改快进到 master 中,从而为您提供线性历史.

Executing git checkout master; git merge --ff-only branch-b will now fast-forward your changes into master, thereby giving you a linear history.

A - B - C - D  - E - F - G - E1' -E2' <-master <- branch-b

这篇关于在“git合并"后保留提交历史的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆