Vahid
Vahid

Reputation: 1417

How can I restrict a git command with specific options?

The reason might be silly but I have a very bad habit of using git commit -am and I hate it. I know I can revert that but I was wondering if there is a way that I can prevent the command from executing at all?

For example, is there a way I add a script or alias to my .bashrc so that every time I run git commit -am "message" it prevents it from running and shows me some message?

Edit: As @chepner mentioned in the comments, I should be more clear, the part I don't like is the -a.

Upvotes: 2

Views: 452

Answers (1)

torek
torek

Reputation: 488491

Actually, you can write a pre-commit hook that checks for this. It's not perfect, by any means, because it checks for too much: any git commit --include somefile will trigger it as well.

Here is a sample (and not useful) pre-commit hook that uses this technique. It should be obvious how to adapt it for your desires.

#! /bin/sh
path=$(basename $GIT_INDEX_FILE)
case $path in
index) echo "normal commit";;
index.lock) echo "commit -i or -a";;
next-index-*.lock) echo "commit -o";;
*) echo "something else: $GIT_INDEX_FILE";;
esac
exit 1

(Make this executable and place it in .git/hooks/pre-commit.)

Upvotes: 4

Related Questions