简体   繁体   中英

Enforce rebase before each commit (git)

I have to mention I'm new to git before you judge me for asking (maybe) stupid question. I'm working in an environment where we have a lot of branches and one is considered the mainline. Because we have different teams working on the project, we configured jenkins to build and run tests after every commit on any branch. But here comes the problem, people commit on their branch without rebasing with the mainline so the tests fail sometimes because modifications were pushed for the mentioned tests in the mainline and people don't have the modifications on their branch. Is there any way I could configure git to reject any commit if it's not rebased with the mainline branch?

UPDATE:

I'm currently trying something like (pre-commit hook):

#!/bin/sh

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

but the if always returns true. The command git rev-list --left-right --count mainbranch...@ | cut -f1 > 0 git rev-list --left-right --count mainbranch...@ | cut -f1 > 0 should return (and it does) the number of commits that my mainbranch is ahead (if > 0 then I need to rebase). What am I doing wrong?

You appear to have a simple shell-script issue. Your command

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

is interpreted as "run git rev-list , pipe it to cut , and write the output to a file named 0 ", which will always succeed.

What you want is "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 ]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM