我经常使用git commit --fixup
(或--squash
),尤其是在代码审查期间。显然,这些提交最终应该在git rebase --autosquash
之后消失,但是令我担心的是,我可能忘记了将这些提交重新设置为基础并合并到master中。
如何确保我不能将它们合并到某些分支中,或者至少不能确保某些分支无法在其中包含这些提交而被推送?
答案 0 :(得分:1)
您至少可以使用下面的fixup!
钩子阻止包含pre-push
的所有推送。
#! /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
是非标准的,但highly useful alias。将以下内容添加到全局.gitconfig
中。
[alias]
lol = log --graph --decorate --pretty=oneline --abbrev-commit
lola = log --graph --decorate --pretty=oneline --abbrev-commit --all