Git commit author identity across repos
When to use
You're about to create a commit in a repo whose default identity may
not match the user's intent — a freshly cloned sibling repo, a
throwaway scratch clone, or a CI-like environment where user.name /
user.email are unset. Common trigger: the user says "use the same
identity as ".
Problem
Two recurring failures:
Committing with the wrong identity (e.g. a work email into a personal OSS repo, or
root@hostnamefrom an unconfigured clone).Trying to override per-command with
git -c user.name='First Last'and having the shell split the quoted value, so git seesuser.name=Firstand treatsLastas the next config key:error: key does not contain a section: useAdd fatal: unable to parse command-line configOr, if it parses, the committed author ends up as
First_Last/Firstbecause quoting was stripped somewhere along the pipeline.
Solution
Read the target identity from an existing repo the user trusts:
git -C /path/to/reference-repo config user.name git -C /path/to/reference-repo config user.emailCommit using environment variables, which survive any shell quoting shenanigans:
GIT_AUTHOR_NAME="First Last" \ GIT_AUTHOR_EMAIL="me@example.com" \ GIT_COMMITTER_NAME="First Last" \ GIT_COMMITTER_EMAIL="me@example.com" \ git -C /path/to/repo commit -F .commit_msg.tmpVerify immediately before pushing:
git -C /path/to/repo log -n1 --format='author=%an <%ae>%n committer=%cn <%ce>'If you got it wrong, fix in place with
--amend --reset-author(only safe if you haven't pushed yet):GIT_AUTHOR_NAME="First Last" GIT_AUTHOR_EMAIL="me@example.com" \ GIT_COMMITTER_NAME="First Last" GIT_COMMITTER_EMAIL="me@example.com" \ git -C /path/to/repo commit --amend --no-edit --reset-author
Example
Wrong (will sometimes give First_Last as the author, sometimes
error out with key does not contain a section):
git -c user.name='First Last' -c user.email=me@example.com commit -m "msg"
Right:
GIT_AUTHOR_NAME="First Last" GIT_AUTHOR_EMAIL="me@example.com" \
GIT_COMMITTER_NAME="First Last" GIT_COMMITTER_EMAIL="me@example.com" \
git commit -F .commit_msg.tmp
Pitfalls
git -cdoes work on the shell in a normal terminal, but through agent tools the quoting is lossy. Env vars are the safe default.--reset-authoronly rewrites the author by default; set bothGIT_AUTHOR_*andGIT_COMMITTER_*or the committer will keep the old identity.- Once pushed, rewriting author requires a force-push; prefer to get it right on the first commit.