如何确保修复提交不会合并到主分支

3
我经常在代码审查过程中使用git commit --fixup(或--squash)。显然,这些提交应该在git rebase --autosquash后最终消失,但我担心我可能会忘记合并这些提交到主分支。
我该如何确保我不能将这些提交合并到特定的分支中,或者至少确保某些分支不能推送这些提交?
1个回答

2
您至少可以使用下面的pre-push钩子来阻止任何包含fixup!的推送。
#! /usr/bin/perl

use strict;
use warnings;

use constant Z40 => '0' x 40;

my($remote,$url) = @ARGV;

my $abort_push = 0;
while (<STDIN>) {
  # <local ref> SP <local sha1> SP <remote ref> SP <remote sha1> LF
  my($lref,$lsha,$rref,$rsha) = split;

  if ($lsha eq Z40) {} # ignore deletes
  else {
    my $commit_range =
      $rsha eq Z40
        ? $lsha            # new branch: check all commits
        : "$rsha..$lsha";  # existing: check new commits since $rsha
    my @cmd = (qw/ git rev-list --pretty=oneline --grep ^fixup! /, $commit_range);

    open my $fh, "-|", @cmd or die "$0: failed to start git rev-list: $!";
    my @fixup_commits;
    while (<$fh>) { push @fixup_commits, "  - $_" }
    close $fh;

    if (@fixup_commits) {
      my $s = @fixup_commits == 1 ? "" : "s";
      warn "Remove fixup$s from $lref:\n", @fixup_commits;
      $abort_push = 1;
    }
  }
}

die "Push aborted.\n" if $abort_push;

例如,有一个历史的例子:
$ git lola
* 4a732d4 (HEAD -> feature/foo) fixup! fsdkfj
| * 478075c (master) w00t
| * 1d572d3 fixup! sdlkf
| * f9a55ee fixup! yo
|/  
* ea708b0 (origin/master) three
* d4276a2 two
* 6426569 hello

尝试推送会产生:
$ git push origin master feature/foo
Remove fixups from refs/heads/master:
  - 1d572d32f963d6218ed3b92f69d58a8ec790d7ea fixup! sdlkf
  - f9a55ee14f28f9496e2aea1bc400ca65ae150f4b fixup! yo
Remove fixup from refs/heads/feature/foo:
  - 4a732d4601012246986037437ac0c0bab39dd0a9 fixup! fsdkfj
Push aborted.
error: failed to push some refs to [...]

请注意,git lola 是一个非标准但非常有用的别名。将以下内容添加到您的全局 .gitconfig 文件中。
[alias]
        lol = log --graph --decorate --pretty=oneline --abbrev-commit
        lola = log --graph --decorate --pretty=oneline --abbrev-commit --all

请参考此处了解如何配置全局钩子路径(适用于所有存储库)。 - John

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接