mattmilten
mattmilten

Reputation: 6726

Prevent accidentally committing all local changes after adding some changes to the index

I often run git commit -a. Sometimes, though, I want to split up the current changes into several commits using git add.

Is there a way to trigger a warning when I do git commit -a with some changes already added to the index? Otherwise all the work of splitting up the changes is lost.

Upvotes: 0

Views: 37

Answers (1)

Marcus Müller
Marcus Müller

Reputation: 36433

so, first of all: the obvious solution is not using git commit -a; I know you are aware of that, but there will be people less used to git that come along here by searching and it's worth explicitly noting that -a should only be used when wanting to commit all the changes.

You could write a pre-commit hook; look into any git repo's .git/hooks/ directory to find a couple of example scripts.

The tricky part will be figuring out that you're in a commit -a situation; I think the easiest part will just be abusing the powers of ps and grep:

TEST_VARIABLE="$(ps -AF|grep -E '.*git commit.*-a')"
if ["$TEST_VARIABLE" != "" ]; then; ..<WARN HERE>.. ; 

or similar (note: this is practically pseudo-syntax. Adapt to your favourite shell/scripting language at will).

Upvotes: 1

Related Questions