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 can you git pull only the current branch?
  • Why not check in AssemblyInfo.cs
  • After svn2git, can you fetch changes from svn?
  • New git diff compaction heuristic isn't working
  • How to create new project from GIT repo using Eclipse EGit
  • When was a file added to a branch in Git?
  • 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.

  • How to do a pre-checkout with Git
  • How to git clone dotfiles into my home directory with one command?
  • Any nice way to generate a timeline view of commits from subversion?
  • Make a local copy of a remote git repository
  • Make Xcode 4 stop auto staging with git
  • Structuring related components in git
  • 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.