3

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?

Tom Ritter
  • 99,986
  • 30
  • 138
  • 174

3 Answers3

2

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;
}
Tom Ritter
  • 99,986
  • 30
  • 138
  • 174
1

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

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!

Shay Nehmad
  • 1,103
  • 1
  • 12
  • 25
  • This would work if I had an easy way to detect a 'sloppy' commit, but putting that much logic in seemed wasteful when I could do it another, simpler way. But thanks for the idea, it's a good one! – Tom Ritter Oct 09 '14 at 03:21