如果一个开发人员忘记 [rebase] 并再次推送,那么我们试图摆脱的testing
所有 [来自废弃提示] 的脏提交都会回来。testing
您无法控制其他人的存储库中发生了什么,但您可以控制他们推送给您的存储库的内容。
一个可接受的解决方案是将测试分支置于一个没有人可以在不进行本地重置的情况下再推送到它的状态。但我想不出一种方法来做到这一点。
这个预接收钩子将拒绝通过合并引入不需要的历史的推送:
#!/bin/sh
# Do not permit merges from unwanted history
#set -x
err=0
while read old new ref; do # for each pushed ref
[[ ${old%[^0]*} = $old ]] && continue # new branches aren't checked.
nomerge=$(git for-each-ref refs/do-not-merge --format='%(objectname)^!')
if [[ $( git rev-list --count --ancestry-path --boundary $old..$new $nomerge
) != $( git rev-list --count --ancestry-path --boundary $old..$new ) ]]; then
echo "$ref doesn't allow merges from outdated history"
err=1
fi
done
exit $err
# why it works:
# if adding nomerge commits' parents as ancestors has any effect, then the
# nomerge commits are reachable without going through $old, i.e. they're
# in some merged history. So check whether adding the abandoned commits as
# explicit ancestors to the push makes them show up, and refuse it if so.
要标记不需要的提交,请参考它们refs/do-not-merge
,例如
git config alias.no-further-merges-from \
'!f() { git update-ref "refs/do-not-merge/$1-@`date +%Y-%m-%dT%H%M%S`" "$1"; }; f'
所以放弃的仪式testing
是
git no-further-merges-from testing
git checkout -B testing master
如果您想标记以前放弃的提示,您可以通过 sha 或任何其他表达方式引用它们,例如
git no-further-merges-from 'testing@{last october 31}'