BillyBBone
BillyBBone

Reputation: 3324

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.

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...

Upvotes: 6

Views: 1692

Answers (3)

AdrianoFerrari
AdrianoFerrari

Reputation: 2158

This is specific to fish shell, so it won't be useful to everyone... but it might be to some!

function git
  if string match 'commit' $argv[1] > /dev/null
    set has_am 0

    for arg in $argv
      if string match -r -- '^-am.*' $arg > /dev/null
        set has_am 1
      end
    end

    if test $has_am -eq 1
      echo -e "Commit All detected.\nDon't do this!"
    else
      command git $argv
    end
  else
    command git $argv
  end
end

Upvotes: 1

VonC
VonC

Reputation: 1324278

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:

(-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"

Upvotes: 3

andrewdotn
andrewdotn

Reputation: 34823

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!

Upvotes: 7

Related Questions