git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Using "git reset" in a pre-commit hook can create an empty commit

From
GSGunnar Strand <gurra.strand@gmail.com>
Date
Feb 19, 2014, 13:05 UTC
Message-ID
<CAEsF1Lt8P8rSmhovbrWSyLE2HQLTjRA3sS-TK_pW6yzp_M+D3g@mail.gmail.com>
Hi,

git version 1.8.4.2 Linux 3.0.74-0.6.8-xen

"git commit" does not properly reevaluate the state after the pre-commit hook has been executed, and lists files which have been reset as part of the commit, and will create an empty commit, despite that no "--allow-empty" was given.

Steps to reproduce:
1. Create a tracked file called "ignoreme"
2. Add the following line to ".git/hooks/pre-commit":
git reset HEAD ignoreme
3. Make a change to the ignoreme file
4. Run "git commit -a"
Result:
The commit template will still list the file:

# Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: ignoreme #

Adding a commit message will create an empty commit:

ign2$ git --no-pager diff HEAD^ HEAD ign2$

Expected behavior:
- Git should refuse to continue if the pre-commit hook resets all changes
- The commit message should not list files removed from the index

Best Regards Gunnar Strand

Message 1 of 1 in “Using "git reset" in a pre-commit hook can create an empty commit”
  1. Gunnar StrandFeb 19, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.