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.

  • Using GCC Through Git Bash
  • Vim: Can not diff more than 4 buffers when trying to do three way merging in git
  • Cygwin: color coding and branch info for git?
  • How do I setup a staging repository in git?
  • Move specific git commit from master to branch
  • Cannot SSH using private key while building Docker image
  • 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…

  • How to accept 'your' changes when doing git merge?
  • How to see if local git repo and remote repo are same commit?
  • Preventing a file overwrite with Git
  • How do I push files specified in .gitignore?
  • How might one “git describe” every commit?
  • How can I tell, from a script, if “git cherry-pick” fails?
  • 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:

    for ARG in "${@}"; do
        if [ "${ARG}" = "-am" ]; then
            echo "Hey! Don’t do that!" 1>&2
            exit 1
    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

    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.