在每次提交之前强制执行 rebase (git)

Enforce rebase before each commit (git)

我不得不提一下我是 git 的新手,然后您才能判断我问了(也许)愚蠢的问题。我在一个有很多分支的环境中工作,其中一个被认为是主线。因为我们有不同的团队从事该项目,所以我们将 jenkins 配置为在任何分支上的每次提交后构建和 运行 测试。但是问题来了,人们在他们的分支上提交而不与主线变基,所以测试有时会失败,因为修改被推送到主线中提到的测试,而人们在他们的分支上没有修改。有没有什么方法可以配置 git 拒绝任何提交,如果它没有与主线分支重新建立基础?

更新:

我目前正在尝试(预提交挂钩):

#!/bin/sh

if git rev-list --left-right --count mainbranch...@ | cut -f1 > 0
then
    exi1
fi

但如果总是 return 为真。命令 git rev-list --left-right --count mainbranch...@ | cut -f1 > 0 应该 return (并且确实如此)我的主分支领先的提交次数(如果 > 0 那么我需要变基)。 我做错了什么?

您似乎遇到了一个简单的 shell 脚本问题。你的命令

if git rev-list --left-right --count mainbranch...@ | cut -f1 > 0

被解释为"run git rev-list, pipe it to cut, and write the output to a file named 0",它总是会成功。

你要的是"run git rev-list, pipe it to cut, and compare the result to the number 0":

if [[ "$(git rev-list --left-right --count mainbranch...@ | cut -f1)" > 0 ]]