开发者

Intercept "git commit -a(m)"

I like the fine-grained commits that the git index allow, i.e. the staging of individual files or even hunks through git add before the final commit. Unfortunately, sometimes after spendi开发者_高级运维ng some time staging a particular commit, muscle-memory kicks in so that I git commit -a -m "msg". Then I either have to live with it, or jump through some reset or --amend hoops.

Is there a way for me to (ideally, globally) configure Git so that if I issue a git commit -a, it gets intercepted? Maybe a script asking me to confirm if I really want to commit all? I've thought about delegating the commit operation to a wrapper script (e.g., "gitcommit"), but do not think that will work very well as it does not stop me from doing a git commit -a -m "msg", which is the problem in the first place.


Try pre-commit hooks:

something like this (not tested)

.git/hooks/pre-commit

#!/bin/sh
git diff-files --quiet    # check if the working directory have non-staged files
DIRTY_WORKING=$?

if [ ! $DIRTY_WORKING ] ; then        #  check if we are committing all files
   # list all files to be committed
   git diff-index --name-status --cached HEAD

   echo "Are you sure? (type YES to continue)"
   read VALUE
   [ "x$VALUE" != "xYES" ]
   exit $?
fi
exit 0

You can find out the number of files to be committed using git diff-index --cached HEAD | wc -l. But I think I would leave this to yourself.


One way would be to make git an alias that runs a script you create that looks at the command line arguments and lets you know if you typed commit followed by -a on the command line.

Personally, I never use commit -a and instead always use git add -u plus git commit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜