您至少可以使用下面的
pre-push
钩子来阻止任何包含
fixup!
的推送。
use strict;
use warnings;
use constant Z40 => '0' x 40;
my($remote,$url) = @ARGV;
my $abort_push = 0;
while (<STDIN>) {
my($lref,$lsha,$rref,$rsha) = split;
if ($lsha eq Z40) {}
else {
my $commit_range =
$rsha eq Z40
? $lsha
: "$rsha..$lsha";
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