# Unexpected behavior when git-adding files in a pre-commit hook then using "git commit -o"

2 messages from 2015-05-04 to 2015-05-04. Participants: Michaël Fortin, Junio C Hamano.
Thread: https://gitlist.dev/t/39234

## Michaël Fortin, 2015-05-04 12:33

Subject: Unexpected behavior when git-adding files in a pre-commit hook then using "git commit -o"
Message-ID: <463FC822-916F-4160-A1F2-B4AAEFF3A5B2@gmail.com>
URL: https://gitlist.dev/e/463FC822-916F-4160-A1F2-B4AAEFF3A5B2%40gmail.com

```
Hi,

I’ve encountered unexpected behavior performing a git add from a pre-commit hook, followed by performing a commit using the “-o” flag, providing a pathspec. Here is a small bash script that explains the issue and serves as a repro:

###############
echo "----> Setup a new test repo"
dir_name="PreCommitHookAddTest"
rm -rf $dir_name; mkdir $dir_name; cd $dir_name
git init; git commit --allow-empty -m "Initial commit"

echo "----> Add a pre-commit hook that stages a file that doesn't currently exist in the repo"
echo "touch auto-added; git add auto-added" > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

echo "----> Try committing a new file using the '-o' flag"
touch manually-added; git add manually-added
git commit -o -m "Commit that ran the pre-commit hook and should contain file 'auto-added'" -- manually-added

echo "----> Results (expected: working copy clean; actual: auto-added is reported as both DELETED and UNTRACKED. HEAD and working copy are the same, staging area contains ‘incorrect' state)"
git status

echo "----> Stage the file after the fact"
git add auto-added

echo "----> Notice that the working copy is now clean"
git status
###############

I would like to avoid the above-described “invalid” state by performing operations correctly in my pre-commit hook, rather than having to add a post-commit hook only to perform a “git add” after the fact (which the repro script emulates); it seems like an ugly workaround to me.

I have had similar issues before, which Jeff King kindly resolved here:
http://thread.gmane.org/gmane.comp.version-control.git/263319/focus=263323

The provided solution does not seem to help in this new context, or at least I cannot make sense of it in relation to this issue, with my limited knowledge of Git internals.

Any insight would be appreciated!

Thanks in advance,

Michaël Fortin
www.irradiated.net

```

## Junio C Hamano, 2015-05-04 18:03

Subject: Re: Unexpected behavior when git-adding files in a pre-commit hook then using "git commit -o"
Message-ID: <xmqqvbg8rzij.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqvbg8rzij.fsf%40gitster.dls.corp.google.com
In-Reply-To: <463FC822-916F-4160-A1F2-B4AAEFF3A5B2@gmail.com>

```
Michaël Fortin <fortinmike@gmail.com> writes:

> I’ve encountered unexpected behavior performing a git add from a
> pre-commit hook, followed by performing a commit using the “-o”
> flag,...

As pre-commit is a verification (and rejection) mechanism, not a
mechanism to tweak what is to be commited, it is totally expected
that its behaviour is unspecified when you modify anything in it.

I think I saw a similar confusion posted to the list once in the
past few years.  Perhaps we would need a documentation patch to
clarify this.

Thanks.

```
