Tom Ritter
Tom Ritter

Reputation: 101390

How can I stop myself from using 'git commit -a'?

I confess that I sometimes use git commit -a when I should not. It's gotten to be reflex about half the time, often when I think I'm working in separate repositories - but I'm actually working in a much larger one that will affect directories far and wide.

Is there a .git/config setting I can specify that will cause the -a flag to throw an error?

Upvotes: 3

Views: 109

Answers (3)

Tom Ritter
Tom Ritter

Reputation: 101390

Thanks to VonC, I hacked up a function I stuck in my rc file:

git() {
    if [[ ($1 == "add") || ($1 == "stage") || ($1 == "commit") ]]; then
        if [[ $@ == *-a* ]]; then
            echo "Don't use 'git $1 -a'.";
        else
            command git "$@";
        fi
    else
        command git "$@";
    fi;
}

Upvotes: 2

VonC
VonC

Reputation: 1326892

Is there a .git/config setting I can specify that will cause the -a flag to throw an error?

Not that I know of.

You would need a wrapper for git which would check the arguments ("commit", "-a", ...), and on the specific command "commit -a" would throw an error.

Jubobs' script (mentioned in the comment above) is a good example of such a wrapper.

Upvotes: 1

Shay Nehmad
Shay Nehmad

Reputation: 1163

No, there is nothing in the git configuration which will do this. However, there is a solution, and it's git hooks.

Git hooks are scripts which execute before or after a git command has been executed - for example, there's a hook which fails your commit if there's no commit message. So you can write a costum hook for your needs which falls when the commit is sloppy.

However, personally, I wouldn't fail it, but rather show an extra prompt (eg "Are you sure? Y/N"). Think about it, do you really want to block the functionality forever?

More info : http://git-scm.com/book/en/Customizing-Git-Git-Hooks

Best of luck!

Upvotes: 1

Related Questions