问题
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 (i.e. my stupidity) and stop myself from committing to the master branch? I'm on Ubuntu.
回答1:
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.
回答2:
You could pretty easily write a pre-commit hook to check your current branch and reject the commit based on the branch name.
回答3:
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.
来源:https://stackoverflow.com/questions/7703345/how-can-i-stop-myself-from-committing-to-the-master-branch-in-git