Is it possible to disable a “dangerous” command line option in git?

By habit, I’ll often work on a change in my repo, and add/commit it in one shot using git commit -am 'my commit message'

There are times when I only want to add a few of the modified files, so I’ll issue preparatory git add commands to meticulously set-up my staging area, and separate the ready-to-commit changes from the half-baked changes.

  • Why .iml files should be added to gitignore? What should be placed in gitignore in common case?
  • Git/GitHub - change default location for cloned repository without changing HOME variable?
  • How can Jenkins poll on a specific file and not the whole repo
  • Make the current git branch a master branch
  • Does git log --branches work?
  • Access Git repo via SSH tunnel
  • Then, I’ll fat-finger the whole thing by issuing the same git commit -am '...' command as I usually do.

    Would there be a way for me to disable the git commit -a option, and/or issue a warning when I use the -a switch? I want to train myself out of this sketchy habit…

  • Port knocking before git push
  • Pull Request Metrics in Stash
  • see git log of commits between master and live branch
  • How do I configure TeamCity build agents to use git and the git:// protocol over a SOCKS proxy?
  • GIT re-merge files from dev to master
  • GitLab email notification not working
  • 2 Solutions collect form web for “Is it possible to disable a “dangerous” command line option in git?”

    Create a wrapper script named git that will catch bad commands and forward good ones on to the real git. Put it earlier in your $PATH:

    #!/bin/bash
    
    for ARG in "${@}"; do
        if [ "${ARG}" = "-am" ]; then
            echo "Hey! Don’t do that!" 1>&2
            exit 1
        fi
    done
    
    exec /usr/bin/git "${@}"
    

    Almost all git commands will work just fine:

    $ git pull
    remote: Counting objects: 1183, done.
    remote: Compressing objects: 100% (728/728), done.
    remote: Total 1183 (delta 771), reused 632 (delta 455)
    Receiving objects: 100% (1183/1183), 1.12 MiB | 1.46 MiB/s, done.
    ...
    

    But the ones you don’t want to work won’t:

    $ git commit -am "foo"
    Hey! Don’t do that!
    

    I don’t know of a git config setting which would prevent/disallow the --all/-a option of a git commit.

    You could consider though a:

    • pre-commit hook (here a hooks--pre-commit.sample):

      git stash save -q --keep-index "possible commit am"
      

    (-q for ‘quiet’)

    That would remove from the working tree any of your modification (except the ones already added to the index)

    • post-commit hook

      if [[ "$(git stash list|| grep "stash@{0}" | grep "possible commit am")" != "" ]]; then
        git stash pop -q
      fi
      

    That should allow you to type a git commit -[a]m "a commit message" without committing everything.
    And if you want to skip those hooks for one commit:

    git commit --no-verify -m "a commit message"
    
    Git Baby is a git and github fan, let's start git clone.