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

  • How to merge N git repositories into one preserving their histories?
  • Allow Github Organization to read repository
  • Can I make `git merge` always conflict on file changes?
  • Does Google App Engine's git Push-to-Deploy also update backends?
  • Is it possible to have a trailing period in user.name in Git?
  • How can I use git-archive to include submodules from a bare repository
  • 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.

  • Why do git branching diagrams not track branches “correctly”?
  • To hook into Git, would Ruby have an advantage over c#?
  • Different Config Files in GitHub Repo Branch
  • How to always have the latest working copy of a git repo?
  • In git how to diff microsoft word documents?
  • How to get Git diff of the first commit?
  • 3 Solutions collect form web for “How can I stop myself from committing to the master branch in git?”

    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.

    You could pretty easily write a pre-commit hook to check your current branch and reject the commit based on the branch name.

    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.

    Git Baby is a git and github fan, let's start git clone.