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...
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!
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With