Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-config

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

like image 444
BillyBBone Avatar asked Jun 19 '14 22:06

BillyBBone


2 Answers

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!
like image 110
andrewdotn Avatar answered Oct 03 '22 13:10

andrewdotn


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"
like image 26
VonC Avatar answered Oct 03 '22 14:10

VonC