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

  • Why am I committing a bunch of desktop.ini files despite .gitignore?
  • Git pull hangs on SMB shared repository
  • Git GUI like HG Workbench in MS Windows
  • Unpacker error Git-pushing from bare repo to staging server
  • How to automatically do full backup of repo, wiki, issue tracker etc. in bitbucket?
  • Git - Create a new remote branch out of an old commit
  • 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.

  • Git clone from bash script
  • Why does Git store the file in the repository when I just add it to the staging area?
  • NuGet issues with packages.config, project references and the solutionwide packages folder
  • Copy 1 commit from one branch (dev) to another (stable) with clean working dir
  • Unable to correct sudden “server certificate verification failed”
  • How to overwrite local tags with git fetch?
  • 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) ….

    # <oldrev> <newrev> <refname>
    # update a blame tree
    while read oldrev newrev ref
        echo "STARTING [$oldrev $newrev $ref]"
    if [ $ref == "refs/heads/master" ] && [ $USER != "hudson" ]
        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;
        echo "This is hudson, allowing commit to master"

    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 ]
        echo "Notifying hudson to build NOW"
        echo "Done notifying"
        echo "This is hudson, not triggering build now"

    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.