我如何简单地线性化我的git历史记录? [英] How do I trivially linearize my git history?

查看:244
本文介绍了我如何简单地线性化我的git历史记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题



考虑到我有如下的提交:

  A  -  B  -  C  -  D  -  G  -  H 
\ /
- E - F -

如何获得完全线性的历史记录,仅在第一个父母之后?

例如,我想得到:

  A  -  B  -  C  -  D  -  G'H'

如上所述,预计G和H的sha1会发生变化。 (由于显而易见的原因)。如果sha1的A,B,C和D也改变了,那么我想知道为什么。

背景



其目的是为了避免在做一个天真的git rebase -i时可能出现的潜在合并提交。 p>真正的观点是希望能够获得额外的洞察力以确定哪些提交在特定分支中,例如,如果我们有以下图表:

  I  -  J  -  K  -  L -  M 
\\ / / /
- N - O - P -

其中N被合并到K中,但是O被合并为--strategy =我们的为L,而P合并成了M。



我希望能够线性化我的历史,以便可以识别和检查这些有问题的犯罪。我想有一棵树可以识别出O没有被放入上游分支,即使我可能通过使用git cherry也可能将N和P识别为可能从上游分支丢失,但是这里的任何建议都将被赞赏。

解决方案

正如您所说,以下命令有效:

  git filter-branch --parent-filter'cut -f 2,3 -d'

为什么?

您提出的问题是通过用简单的提交来转换每个合并提交来解决的:这将简单地删除特征分支被合并,因为它们将成为孤儿。



每个提交都有一个或多个父提交。合并提交是获得多个合并的提交。


$ b

git filter-branch 命令,其中 c $ c> - parent-filter 选项允许重写每个提交的父项,并将其作为 -p SHA1 传递给过滤器,如果存在则重复不止一个家长。我们的过滤器会削减父母,并强制每个承诺拥有一位父母。



对于奖金,以下是如何通过重新创建新提交来手动完成一次精确提交:




  • 获取提交树和第一个父

      tree =`git show  - s --format =%T SHA1` 
    parent =`git show -s --format =%P SHA1 | cut -d-f1`


  • 使用同一棵树进行新的提交,消息,并只保留第一父母作为祖先

      git show -s --format =%B SHA1 | git commit-tree $ tree -p $ parent 



Question

Given that I have a commit like the following:

A - B - C - D - G - H
     \         /
      - E - F -

How do I get a perfectly linear history following only the first parents?

Eg, I want to get:

A - B - C - D - G'- H'

It is expected that the sha1's of G and H will change, as noted above. (For obvious reasons). If the sha1's for A, B, C, and D change as well, then I want to know why.

Background

The intention is to avoid potential merge commits that would arise when doing a naive git rebase -i.

The real point is to hopefully gain additional insight in order to determine which commits are in a particular branch, eg, if we have the following graph:

I - J - K - L - M
 \     /   /   / 
  - N - O - P -

Where N was merged into K, but O was merged with --strategy=ours into L, and P was merged into M.

I want to be able to linearlize my history so that such problematic commits can be identified, and inspected. I want to have a tree where can identify that O was not put into the upstream branch, even if I potentially identify N and P as being potentially missing from the upstream branch as well, by using git cherry, however any suggestions here would be appreciated.

解决方案

As you said, the following command works:

git filter-branch --parent-filter 'cut -f 2,3 -d " "'

Why?

The problem you pose is solved by transforming each merge commit with a simple commit: this will simply remove the feature branches that were merged, since they will become orphan.

Each commit has one or more parent commits. Merge commit are the one which get more than one. Git stores this in each commit object of the history.

The git filter-branch command, with the --parent-filter option, allows to rewrite every commit's parent, passed to the filter as -p SHA1, repeated if there are more than one parent. Our filter cuts the parent and forces every commit to have a single parent.

For bonus, here's how to do it manually on a precise commit, by re-creating a new commit:

  • get the commit tree and the first parent

    tree=`git show -s --format=%T SHA1`
    parent=`git show -s --format=%P SHA1 | cut -d " " -f1`
    

  • make a new commit with the same tree, same message, and keep only the first parent as ancestor

    git show -s --format=%B SHA1 | git commit-tree $tree -p $parent
    

这篇关于我如何简单地线性化我的git历史记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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