This worked for me on git version 2.23.3
.
git -c [email protected] -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 [email protected] -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.
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?
ts='2024-05-29 02:21:07 +0000'; GIT_AUTHOR_DATE="$ts" GIT_COMMITTER_DATE="$ts" 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.