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

Re: Issue when changing staged files in a pre-commit hook

From
Duy Nguyen <pclouds@gmail.com>
Date
Jan 19, 2016, 12:20 UTC
Message-ID
<CACsJy8DhiYiie7+Cw3PkPJpSX7CGp-r2Mu98mLp4OMhhGdsXgQ@mail.gmail.com>
In-Reply-To
<CAPYEnzGfnRbajDQAwBTNE5XSaB0WbHKbf1heRV0bUgbq5w_A5g@mail.gmail.com>

On Mon, Dec 28, 2015 at 7:17 PM, Niek van der Kooy <niekvanderkooy@gmail.com> wrote:

> I am having an issue where the default commit message is not changed
> when adding staged files in a pre-commit hook.
> Please see http://stackoverflow.com/questions/34492779/git-update-git-status-part-of-commit-message-after-pre-commit-hook
> for details.

you should probably copy the text here instead of putting a link. Sometimes I don't bother clicking another link. Make it easy for people to help you. Long story short, pre-commit hook adds a file, which ends up in the commit, but is not shown in git-status.

> Is there a workaround / proper way to achieve what I am trying to do,
> or is this a bug?

When you commit something, git may prepare a temporary index that contains what's to be in the commit, unless you do "git commit" without -a/-A, or paths... The pre-commit is set up to see this index. So if you make changes in this index, they will end up in the new commit. But the index will then be discarded. The main index, $GIT_DIR/index, may or may not be untouched the whole time. This is the index that is used by "git status". If you do "git commit" without -a/-A/paths they "git status" should be consistent because your hook sees $GIT_DIR/index instead of a temporary one.

I think it's the intended behavior. I believe $GIT_DIR is exported to your hook, so in theory you could do an extra "GIT_INDEX_FILE=$GIT_DIR/index git add public" (i.e. you update _two_ index files, one for committing, one for future git commands). But be careful. It may have unwanted effects. It's probably easier to make an alias that adds that 'public' file first then commits without hooks.

-- 
Duy
Previous: Niek van der KooyNext: Junio C Hamano
Message 3 of 9 in “Issue when changing staged files in a pre-commit hook”
  1. Niek van der KooyDec 28, 2015
  2. Niek van der KooyJan 19, 2016
  3. Duy NguyenJan 19, 2016
  4. Junio C HamanoJan 19, 2016
  5. Duy NguyenJan 19, 2016
  6. Junio C HamanoJan 19, 2016
  7. Duy NguyenJan 20, 2016
  8. Junio C HamanoJan 20, 2016
  9. Duy NguyenJan 22, 2016

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.