在分支历史记录中,有一种语法可用于引用紧随提交之后的提交?

问题描述

如果我有特定的哈希,将其命名为A,我可以通过A^到达父级。但是,有没有一种方法可以到达分支历史中A之后的提交?像A+1这样的东西,例如A(A+1)^吗?我想在有哈希值的提交之后签出提交,然后在之后的提交中签出,依此类推,以检查它们。但是他们真的被埋葬了。除了搜索git log输出以外,是否有任何简单的方法可以在给定参考哈希的情况下将它们检出?

解决方法

我想在有哈希值的提交之后签出提交,然后在那之后的提交中签出,依此类推,以检查提交

git rev-list --first-parent --reverse $thathash..mybranch

将按顺序列出您要检出的所有提交。如果您不仅想要线性历史记录,而且所有从分支提示历史记录中的哈希值继承的提交都将--first-parent替换为--ancestry-path,那么您可以使用排序选项来决定访问它们的顺序。

,

没有内置命令可以找到两次提交之间的线性路径;您将为此编写一个外部脚本。

下面是一个用perl编写的示例(来自this answer):

# in file 'git-path.pl' :
#!/usr/bin/perl

use strict;
use warnings;

# This script implements a breadth first search,starting from $target,# following the 'child -> parent' links,until $source is found or the
# complete history of $target is traversed.

my $source = $ARGV[0];
my $target = $ARGV[1];

my $srcsha = `git rev-parse -q --verify "$source"` || die "'$source' is not a valid source";
chomp($srcsha);
my $tgtsha = `git rev-parse -q --verify "$target"` || die "'$target' is not a valid target";
chomp($tgtsha);

# %seen stores the commits seen so far,linked to the child we are interested in
my %seen = ();
$seen{$tgtsha} = "";

# @stack lists the commits to visit
my @stack = ();
push @stack,$tgtsha;


# print_path : print the path from '$target' down to '$source'
sub print_path {
  my ($source) = @_;

  my @path = ();

  my $sha = $source;
  while ($sha) {
    unshift @path,$sha;
    $sha = $seen{$sha};
  }

  print "$_\n" for @path;
}


# main body :
# as long as there is something to scan,go for it,# if $source is found along the way,print the path and exit with success
# otherwise,end the loop,and exit with failure

while( scalar(@stack) > 0 ) {
  my $sha = shift @stack;

  # extract parent lines from 'git cat-file -p commit'
  my @parents = `git cat-file -p $sha | grep '^parent ' | cut -c8-`;
  
  foreach my $p (@parents) {
    chomp($p);
    # for each parent,if not seen yet :
    #   * store it as a parent of $p
    #   * put it on the list of commits to explore next
    if (!$seen{$p}) {
      $seen{$p} = $sha;
      push @stack,$p;

      if ($p eq $srcsha) {
        # if we reached the 'source' commit : stop here
        print_path $p;
        exit 0;
      }
    }
  }
}

# no path found
exit 1;

示例用法:

$ perl git-path.pl A B
175015a5a00bbb9ad2ee4de23254ace4dbc645eb # commit B
bc03512e6082badab86a00cd320d89339741bb7b
077f584c10852cbadeef6c48886fd600cab61aa6
cec6734db31a1053b1b71674671512e1fe1592b1
b1dd3c71e42ca421f306640d4f0fdc69a00aa2c7
778a504c718f30d0dc2c72a30c885f10847f46a8 # commit A

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...