首页 > 解决方案 > 将 git 历史记录折叠到标签(在标签之间合并提交)

问题描述

我们有一个相当大的(1000 多个提交)存储库,其中包含 7 年的更改和 6 个发布版本(每个版本由一个 git 标签表示)。我们想折叠历史,以便在最后一个标签之前,两个标签之间的所有提交都折叠成一个提交,带有版本标签。从而将我们的存储库历史记录减少到大约 15 次提交。我们不会跨版本进行任何还原。作为奖励,理想情况下,我们希望将所有提交我们崩溃的人保留为co-authored-by. 如果它很重要,我们没有任何从标签之前跳过标签进入新版本的合并。

从:

CHEAD
C2
C3
...
TAG6
Cx
Cy
...
TAG5
Ca
..

进入

CHEAD
C2
C3
...
TAG6
TAG5
TAG4
TAG3
TAG2
TAG1

关于如何进行的任何想法?

标签: gitgit-historygit-history-rewrite

解决方案


按标签折叠很容易:git log有两个选项--simplify-by-decoration--decorate-refs=<pattern>,您可以按如下方式使用:

# --graph is useful to have a clear view of parent <-> child relation,
# you may use --oneline to have a compact view, or drop it if you want the complete
# commit messages
git log --graph --oneline --simplify-by-decoration \
      --decorate-refs=refs/tags   # <- this indicates 'keep only tags'

获得“之后的所有内容TAG6,只有之前的标签TAG6”的一种部分方法可能是在两个命令中获取日志:

# in a 'mylog' script :
#!/bin/bash

log_history () {
    # get all from TAG6 up to HEAD :
    git log --decorate --graph TAG6..HEAD
    # get only tags up to TAG6 :
    git log --decorate --graph --simplify-by-decoration \
        --decorate-refs=refs/tags TAG6
}

# combine the two commands, and pipe them in your pager
# if you pipe the output of 'git log', git removes the default '--decorate',
# that's why I added it in the commands above
log_history | less

以上将为您提供您想要查看的提交的完整列表;唯一缺少的部分是,在图中,不会绘制TAG6与其子提交(上面的提交)之间的链接。TAG6

我不知道如何指示您在一个git log命令中描述的组合。


推荐阅读