Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Git与--squash合并,同时保留每次提交的日志_Git_Merge_Git Squash - Fatal编程技术网

Git与--squash合并,同时保留每次提交的日志

Git与--squash合并,同时保留每次提交的日志,git,merge,git-squash,Git,Merge,Git Squash,初始场景: A (master) \ B - C - D (development) 合并后我想要的--挤压: 在分支master,git log将 commit E Squashed commit of the following: commit D commit C commit B commit A commit I Squashed commit of the following: commit H commit G

初始场景:

A (master)
 \
 B - C  - D (development)
合并后我想要的--挤压:

在分支
master
git log

commit E
    Squashed commit of the following:
    commit D
    commit C
    commit B
commit A
commit I
    Squashed commit of the following:
    commit H
    commit G
    commit F
commit E
    Squashed commit of the following:
    commit D
    commit C
    commit B
commit A
Commit I
Commit H
Commit G
Commit F
Commit E
Commit D
Commit C
Commit B
Commit A
继续发展分支机构
发展

A     -     E (master)
 \         / \         
 B - C - D    F - G - H (development)
再次与squash合并:

A     -     E     -     I(master/development)
 \         / \         /
 B - C - D    F - G - H
在分支
master
git log

commit E
    Squashed commit of the following:
    commit D
    commit C
    commit B
commit A
commit I
    Squashed commit of the following:
    commit H
    commit G
    commit F
commit E
    Squashed commit of the following:
    commit D
    commit C
    commit B
commit A
Commit I
Commit H
Commit G
Commit F
Commit E
Commit D
Commit C
Commit B
Commit A
在分支
开发
git日志

commit E
    Squashed commit of the following:
    commit D
    commit C
    commit B
commit A
commit I
    Squashed commit of the following:
    commit H
    commit G
    commit F
commit E
    Squashed commit of the following:
    commit D
    commit C
    commit B
commit A
Commit I
Commit H
Commit G
Commit F
Commit E
Commit D
Commit C
Commit B
Commit A
我想合并
master
上压缩的提交,同时将每个提交保持在
development


我不知道如何实现这一点。我的问题是,我不知道如何使
D
在第一次合并中指向
E
,而
I
将包括
B、C、D、E、F、G、H
,而不仅仅是
F、G、H

这只是一个常规合并

#/垃圾箱/垃圾箱
初始化
触摸a;git加一个;git提交-ma
git签出-b补丁
触摸b;git加b;git提交-mb
触摸c;git加c;git提交-mc
切换到主分支
git合并--无ff修补程序
git日志--图形--单线
结果

*   a9fbaec Merge branch 'patch'
|\
| * fb7eac4 c
| * 94f44c1 b
|/
* 4de57a5 a

您无法使用所显示的结构获得所需的日志结果(
git log--merges master
会这样做)。为您提供所需日志的结构不符合这一点。最后,这是使用Git的一种自我挫败的方式


我们希望能够运行
git log master
,只看到压缩的提交。这行不通。Git不知道有些提交是用于
master
的,有些是用于
开发的。让我们看看提议的结构

A     -     E     -     I [master] [development]
 \         / \         /
 B - C - D    F - G - H
此时,
master
development
指向相同的提交。就Git历史而言,它们是相同的。分支只不过是指向提交的标签。提交人不记得他们被提交到哪个分支
git log master
git log development
将生成相同的日志,显示从A到I的所有提交。E和I压缩的提交日志将是冗余的

使用
git log--merges master
(或
development
)只显示合并提交,但这将显示任何合并提交,甚至是作为
开发
一部分完成的合并提交。所以它实际上不起作用

这整个想法是不必要的复杂,请继续阅读


要获得所需的日志结果,必须像这样断开两个分支之间的关系

A     -     E     -     I [master]
 \                   
 B - C - D - F - G - H [development]
你可以通过某种方式实现,但没有意义
git log master
包含所有相同的日志消息,因此它将与git log development
一样长,但它们将被粉碎在一起。您不能使用
git log master
进行代码考古(即“为什么这一行是这样写的”),因为所有的更改都被分解成一个差异,使得将更改行与特定的提交消息关联起来变得更加困难。由于
master
development
的历史是不相关的,因此无法确保开发中的所有内容都成为master,反之亦然(例如,master的热修复)

git-log-master
提供的信息比
git-log-development
少,而且更难理解
master
开发
没有关联,并且失去了保留合并历史记录的所有好处。维护这种复杂的设置是没有意义的


相反,使用
git merge--no ff
合并特征分支(不是一个连续的“开发”分支),并保留分支历史,以便于考古

              G - J [feature/tacos]
             /
A     -     E     -     K [master]
 \         / \         /
 B - C - D    F - H - I
E和K是由
git merge--no ff
生成的正常合并提交。不存在由功能分支处理的持续
开发
分支。要素分支是一次性的,合并后删除。有关功能分支的信息保留在合并提交中,
git merge--no ff
保证分支结构被保留<代码>功能/tacos是一个功能分支,用于处理尚未合并的tacos

git log--graph--decoration master
将显示master的完整历史记录,合并提交将显示功能结束的时间,以及说明分支历史记录的行。GUI Git历史记录工具(如)是以图形形式读取历史记录的另一种方法


最后,Git历史是一个图形。如果您学会了如何使用该图形,那么使用Git的生活就会轻松得多。如果你试图让Git历史成为线性的,就像一大堆煎饼一样,你就在挫败Git的意义,为你自己和其他人创造额外的工作。

好吧,不可能完全得到你想要的,就像你合并了分支一样,你无论如何都会在提交日志中看到来自两个分支的提交。您可能想要的是这样的:

A      -     E (master)
 \         
 B - C - D (development)
git checkout development && git rebase -i master HEAD
您只需从
development
分支中选择提交,挤压它们并在
master
上应用即可

最简单的方法是:

A      -     E (master)
 \         
 B - C - D (development)
git checkout development && git rebase -i master HEAD
然后用
squash
替换除第一个动作之外的所有动作。因此,您将获得一个指向压缩提交的分离头,应用于
master
之上。您只需将
master
重置为该提交即可

我想,提交消息并不完全是您想要的,因为它将解析所有提交的提交消息,而不是它们的散列,但是您可以使用
--exec
选项连接到提交消息生成中


所有内容都有点过于手动,但您可以编写所有内容的脚本并创建别名。我相信,这是使用git本身所能得到的最接近的结果,而不是用某种更高级的编程语言重写所有内容。

但是你的提交没有在branch
master
git日志中被压缩谢谢你指出,我已经编辑了我的问题