Skip to content

Instantly share code, notes, and snippets.

@kyle0r
Last active March 18, 2021 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyle0r/f233069eafda594421607e4f665ab75e to your computer and use it in GitHub Desktop.
Save kyle0r/f233069eafda594421607e4f665ab75e to your computer and use it in GitHub Desktop.
specify a transient git author and committer, and optionally specific commit dates

specify a transient git author and committer, and optionally specific commit dates

This worked for me on git version 2.23.3.

git -c user.email=joeb@domain.tld -c user.name="Joe B" commit <path>

Pay attention to the docs on user. as it determines both the author and committer fields.

This approach can be useful when you find yourself having to make a commit to someone else's repo or an anonymous repo, where you don't want to modify the persistent git config.

I had issues using GIT_AUTHOR_EMAIL and GIT_AUTHOR_NAME env vars to work, so I was probably doing something wrong, maybe I was having a mix up with GIT_COMMITTER_EMAIL and GIT_COMMITTER_NAME. Feel free to experiment with them and check the docs.

You could set an transient alias for a bash session as follows:

alias git 1>/dev/null 2>&1 || alias git='git -c user.email=joeb@domain.tld -c user.name="Joe B"'

Which would ensure any git commands e.g. commits in the session would use the specified git cfg.

Note: The provided command should prevent clobbering an existing alias. Use alias git to check for an existing alias.

specify commit dates

If you have a requirement to use a date other than the current system time aka NOW, you can use the following env var overrides.

Who hasn't forgotten a commit?

GIT_AUTHOR_DATE='Wed Oct 14 15:20:00 2020 +0000' GIT_COMMITTER_DATE='Wed Oct 14 15:20:00 2020 +0000' git commit <path>

Combined with the example alias this provides granular control over author and dates.

Note: The invocation style where the env vars are provide as a prefix to git commit and therefore do not enter the shell env, only the env of the spawned process. i.e. they are one-time env vars for the specific command invocation. This is useful because the next commit will be using the standard behaviour of date=now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment