threads / discuss / 63228

--reset-author does not reset author date when used in post-commit hook

Subject: --reset-author does not reset author date when used in post-commit hook

## tl;dr

5 messages between Mar 31, 2025 and Apr 2, 2025.

replies: 4people: 2as markdown or json

Devste Devste· Mar 31, 2025, 20:19 UTC · lore
device time zone is not UTC0

post-commit hook: if [[ -n "${SKIP_POST_COMMIT+x}" ]] && [[ "$SKIP_POST_COMMIT" -eq 1 ]] then exit fi

TZ=UTC0 SKIP_POST_COMMIT=1 git commit --amend --reset-author --no-edit --no-verify --no-post-rewrite --allow-empty --quiet

---

Run: git commit -m "foo" git show -s --format="Commit: %h%nAuthor: %an%nAuthor Date: %ad%nCommitter: %cn%nCommitter Date: %cd%nMessage: %s%n" HEAD

Expected that both Author Date: and Committer Date: show +0000 as timezone. Only committer date shows +0000, while author date shows time zone of device.

When manually running the post-commit code (TZ=UTC0 SKIP_POST_COMMIT=1 ...) it correctly resets the author date to +0000 too

When manually setting the date using --date="$(date +%s) +0000" instead of --reset-author it also works correctly in post-commit hook.

brian m. carlson· Apr 1, 2025, 00:43 UTC · re: Devste Devste · lore

Re: --reset-author does not reset author date when used in post-commit hook

On 2025-03-31 at 20:19:30, Devste Devste wrote:
Show 28 quoted lines
> device time zone is not UTC0
> 
> post-commit hook:
> if [[ -n "${SKIP_POST_COMMIT+x}" ]] && [[ "$SKIP_POST_COMMIT" -eq 1 ]]
> then
> exit
> fi
> 
> TZ=UTC0 SKIP_POST_COMMIT=1 git commit --amend --reset-author --no-edit
> --no-verify --no-post-rewrite --allow-empty --quiet
> 
> ---
> 
> Run:
> git commit -m "foo"
> git show -s --format="Commit: %h%nAuthor: %an%nAuthor Date:
> %ad%nCommitter: %cn%nCommitter Date: %cd%nMessage: %s%n" HEAD
> 
> Expected that both Author Date: and Committer Date: show +0000 as
> timezone. Only committer date shows +0000, while author date shows
> time zone of device.
> 
> When manually running the post-commit code (TZ=UTC0 SKIP_POST_COMMIT=1
> ...) it correctly resets the author date to +0000 too
> 
> When manually setting the date using --date="$(date +%s) +0000"
> instead of  --reset-author it also works correctly in post-commit
> hook.

I can reproduce this, except that I had to use `TZ=America/Toronto` since my system is in UTC. A quick look at the code led me to a guess that ended up correct.

The `git commit` code sets `GIT_AUTHOR_NAME`, `GIT_AUTHOR_EMAIL`, and `GIT_AUTHOR_DATE`. The latter contains a timezone identifier. As a consequence, when the hook is invoked, those values are set in the environment, and the subsequent `git commit` in the hook uses them. You can see this by placing something like `env >/tmp/foo` in your hook and then reading the output.

I will say that this is definitely not the intended use of the post-commit hook and I am very much not surprised that this doesn't work as you intended. You could certainly fix it by unsetting those environment variables, but I'd encourage you to adopt a different approach, such as by using an alias that sets `TZ=UTC0` to commit. That will be more likely to work in the future and will definitely be less brittle.

For instance, your call to `git commit` will still invoke the `prepare-commit-msg` (and possibly `commit-msg`) hook, which might result in unexpected changes to your message or even a failure of the second commit.

-- 
brian m. carlson (they/them)
Toronto, Ontario, CA
Devste Devste· Apr 1, 2025, 03:44 UTC · re: brian m. carlson · lore

Re: --reset-author does not reset author date when used in post-commit hook

>your call to `git commit` will still invoke the
`prepare-commit-msg` (and possibly `commit-msg`) hook

Since I used --no-edit --no-verify in the example/hook, that shouldn't be the case though?

>such as by using an alias that sets `TZ=UTC0` to commit.

Unfortunately, (at least on Windows) most tools and IDEs don't even load a bashrc file (e.g. intellij) for their git UI, so that's not an option (and setting the TZ as a global environment variable has unwanted side effects, since it will set the timezone in various applications randomly, e.g. Electron/Slack will use the TZ for some but not for other things,...)

On Tue, 1 Apr 2025 at 02:43, brian m. carlson <sandals@crustytoothpaste.net> wrote:

Show 57 quoted lines
>
> On 2025-03-31 at 20:19:30, Devste Devste wrote:
> > device time zone is not UTC0
> >
> > post-commit hook:
> > if [[ -n "${SKIP_POST_COMMIT+x}" ]] && [[ "$SKIP_POST_COMMIT" -eq 1 ]]
> > then
> > exit
> > fi
> >
> > TZ=UTC0 SKIP_POST_COMMIT=1 git commit --amend --reset-author --no-edit
> > --no-verify --no-post-rewrite --allow-empty --quiet
> >
> > ---
> >
> > Run:
> > git commit -m "foo"
> > git show -s --format="Commit: %h%nAuthor: %an%nAuthor Date:
> > %ad%nCommitter: %cn%nCommitter Date: %cd%nMessage: %s%n" HEAD
> >
> > Expected that both Author Date: and Committer Date: show +0000 as
> > timezone. Only committer date shows +0000, while author date shows
> > time zone of device.
> >
> > When manually running the post-commit code (TZ=UTC0 SKIP_POST_COMMIT=1
> > ...) it correctly resets the author date to +0000 too
> >
> > When manually setting the date using --date="$(date +%s) +0000"
> > instead of  --reset-author it also works correctly in post-commit
> > hook.
>
> I can reproduce this, except that I had to use `TZ=America/Toronto`
> since my system is in UTC.  A quick look at the code led me to a guess
> that ended up correct.
>
> The `git commit` code sets `GIT_AUTHOR_NAME`, `GIT_AUTHOR_EMAIL`, and
> `GIT_AUTHOR_DATE`.  The latter contains a timezone identifier.  As a
> consequence, when the hook is invoked, those values are set in the
> environment, and the subsequent `git commit` in the hook uses them.  You
> can see this by placing something like `env >/tmp/foo` in your hook and
> then reading the output.
>
> I will say that this is definitely not the intended use of the
> post-commit hook and I am very much not surprised that this doesn't work
> as you intended.  You could certainly fix it by unsetting those
> environment variables, but I'd encourage you to adopt a different
> approach, such as by using an alias that sets `TZ=UTC0` to commit.  That
> will be more likely to work in the future and will definitely be less
> brittle.
>
> For instance, your call to `git commit` will still invoke the
> `prepare-commit-msg` (and possibly `commit-msg`) hook, which might
> result in unexpected changes to your message or even a failure of the
> second commit.
> --
> brian m. carlson (they/them)
> Toronto, Ontario, CA
brian m. carlson· Apr 1, 2025, 22:58 UTC · re: Devste Devste · lore

Re: --reset-author does not reset author date when used in post-commit hook

On 2025-04-01 at 03:44:17, Devste Devste wrote:
Show 5 quoted lines
> >your call to `git commit` will still invoke the
> `prepare-commit-msg` (and possibly `commit-msg`) hook
> 
> Since I used --no-edit --no-verify in the example/hook, that shouldn't
> be the case though?

My testing demonstrates that `commit-msg` is skipped with `--no-edit --no-verify`, but not `prepare-commit-msg`.

Show 8 quoted lines
> >such as by using an alias that sets `TZ=UTC0` to commit.
> 
> Unfortunately, (at least on Windows) most tools and IDEs don't even
> load a bashrc file (e.g. intellij) for their git UI, so that's not an
> option (and setting the TZ as a global environment variable has
> unwanted side effects, since it will set the timezone in various
> applications randomly, e.g. Electron/Slack will use the TZ for some
> but not for other things,...)

What I could recommend instead is setting a shell script or PowerShell file or such as the default Git binary (instead of git.exe) and setting `TZ=UTC0` in that script (possibly only if the command is `commit` or `commit-tree`). I use Neovim with the fugitive extension, and it provides functionality to override the Git executable, so I suspect other editors will as well. This is something that will likely be robust and unlikely to break.

The reason I make that recommendation is that I suspect you're going to find that there are other infelicities or weird edge cases in what you're doing and this would be a use case that I could see getting broken accidentally. We've accidentally broken people doing `git add` in `pre-commit` hooks (which we also don't recommend) in the past, for instance.

-- 
brian m. carlson (they/them)
Toronto, Ontario, CA
Devste Devste· Apr 2, 2025, 10:06 UTC · re: brian m. carlson · lore

Re: --reset-author does not reset author date when used in post-commit hook

>My testing demonstrates that `commit-msg` is skipped with `--no-edit
--no-verify`, but not `prepare-commit-msg`.

Looks like a separate bug though? https://git-scm.com/docs/git-commit#Documentation/git-commit.txt-code--no-editcode says:

>Use the selected commit message without launching an editor. For example, git commit --amend --no-edit amends a commit without changing its commit message.
Why would the prepare-commit-msg hook be called then?
>What I could recommend instead is setting a shell script or PowerShell

file or such as the default Git binary (instead of git.exe) and setting `TZ=UTC0` in that script (possibly only if the command is `commit` or `commit-tree`).

The IDEs only work with git.exe. If it's not there, it won't run at all. The only thing possible is setting TZ= as a user environment variable however that will obviously be picked up by other applications and cause the weirdest side-effects (e.g. Slack/Electron suddenly start using that time zone for some things but the in-built/setting timezone for others and lots of other inconsistency issues in tons of applications) There's various tickets for various IDEs for this use case, not just for git, but also for npm and various other tools (e.g. people need to change the JAVA version for a tool or whatever)

Some time ago I opened "Config timezone to prevent chaos when DST/changing timezone" in the git mailing list and I saw you also replied. But if git had a way to actually set the timezone, these workarounds wouldn't be necessary; alas that's another issue

On Wed, 2 Apr 2025 at 00:58, brian m. carlson <sandals@crustytoothpaste.net> wrote:

Show 37 quoted lines
>
> On 2025-04-01 at 03:44:17, Devste Devste wrote:
> > >your call to `git commit` will still invoke the
> > `prepare-commit-msg` (and possibly `commit-msg`) hook
> >
> > Since I used --no-edit --no-verify in the example/hook, that shouldn't
> > be the case though?
>
> My testing demonstrates that `commit-msg` is skipped with `--no-edit
> --no-verify`, but not `prepare-commit-msg`.
>
> > >such as by using an alias that sets `TZ=UTC0` to commit.
> >
> > Unfortunately, (at least on Windows) most tools and IDEs don't even
> > load a bashrc file (e.g. intellij) for their git UI, so that's not an
> > option (and setting the TZ as a global environment variable has
> > unwanted side effects, since it will set the timezone in various
> > applications randomly, e.g. Electron/Slack will use the TZ for some
> > but not for other things,...)
>
> What I could recommend instead is setting a shell script or PowerShell
> file or such as the default Git binary (instead of git.exe) and setting
> `TZ=UTC0` in that script (possibly only if the command is `commit` or
> `commit-tree`).  I use Neovim with the fugitive extension, and
> it provides functionality to override the Git executable, so I suspect
> other editors will as well.  This is something that will likely be
> robust and unlikely to break.
>
> The reason I make that recommendation is that I suspect you're going to
> find that there are other infelicities or weird edge cases in what
> you're doing and this would be a use case that I could see getting
> broken accidentally.  We've accidentally broken people doing `git add`
> in `pre-commit` hooks (which we also don't recommend) in the past, for
> instance.
> --
> brian m. carlson (they/them)
> Toronto, Ontario, CA

← back to recent threads