简体   繁体   中英

How can I stop myself from committing to the master branch in git?

My ideal workflow would be to commit changes onto dev and only merge changes to master via git merge --no-ff dev , but occassionaly what happens is:

On dev

dev: git add .
dev: git commit -m "Cured cancer."
dev: git checkout master
master: git merge --no-ff dev

Writes some more code

master: git commit -a -m "Did something amazing"

Whops, just committed to master. If I'm lucky I realize it right away, but if I don't I can push the branch upstream with another 10 changes on top. So this ends up in remote:

Master   -------- Merge --- commit - commit --------------- Merge
Dev      \ Commit/                        \ commit - commit /

While it should look like this:

Master   -------- Merge ------------- Merge --------------------- Merge
Dev      \ Commit/     \ commit commit /  \ commit - commit - commit /

How can I remove the human factor (ie my stupidity) and stop myself from committing to the master branch? I'm on Ubuntu.

This isn't a bullet-proof solution, but you could install git-completion.bash and use its PS1 enhancement to display the current branch (and other useful stuff) as part of your command prompt. See here for an explanation.

It won't stop you from committing to master, but it will make it difficult to forget which branch you're on.

您可以很容易地编写一个pre-commit钩子,以检查您当前的分支并根据分支名称拒绝提交。

I am trying to setup something similar where only hudson commits as it will merge the remote branch you commit into master and then push those into our git Soooooo our mainline master is only GOOD code and no developer can be affected by another one breaking the build...oh happy day.

pre-receive script(server side - don't put locally) ....

#!/bin/sh
# <oldrev> <newrev> <refname>
# update a blame tree
while read oldrev newrev ref
do
    echo "STARTING [$oldrev $newrev $ref]"

if [ $ref == "refs/heads/master" ] && [ $USER != "hudson" ]
then
    echo "YOU CANNOT COMMIT STUFF TO MASTER BRANCH"
    echo "TO CORRECT THIS run"
    echo "git branch -c <branch name> then run"
    echo "git push <reponame> <branch name>"
    echo "and hudson will take and push to master IF it passes the tests"
    exit 1;
else
    echo "This is hudson, allowing commit to master"
fi

done

and then of course, I HATE polling so on post-receive I do something like this(NOTE that the hudson user cannot run the curl command or you end up in infinite loop as it keeps pushing changes it makes to master branch)...

post-receive script(server side-don't put locally)

echo "User=$USER"

if [ "hudson" != $USER ]
then
    echo "Notifying hudson to build NOW"
    curl http://10.222.0.168:8080/job/stserver1/build?delay=0sec
    echo "Done notifying"
else
    echo "This is hudson, not triggering build now"
fi

NOTE: I haven't figured out the way for a developer to revert their commit to master yet though :(. still working on that one.

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