Override configured user for a single git commit
GitGithubGit Problem Overview
I am trying to commit to a project on github.com from my work laptop, which is already configured for the company git server. Is there a way to commit specifying different author credentials, possible using a different configuration file or orther command line switches?
I've tried using
--author="My Name <[email protected]>"
and I received the message:
Committer: unknown <WorkEmail@workemail.net>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email [email protected]
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
but it still didn't update my username for committing to the github.com project. Is there anything else I can try and is it even possible?
Git Solutions
Solution 1 - Git
First, the author is not necessarily the same as the committer. Git tracks both.
To set what name to use for just this repository, you could do:
git config user.name "Your Name"
git config user.email "Your email"
Notice the absence of the --global
option. This will set the configuration in this repository only.
Alternatively, you can do this for only a single command, using the -c
option:
git -c "user.name=Your Name" -c "user.email=Your email" commit ...
But I think it's better to just use the config options above.
Solution 2 - Git
Try to prepend this to your git command :
git -c user.email[email protected] -c user.name='Your Name'
This is ok for a one-shot, but you could prefer save this in the .git/config file of the local repo you cloned from git. This can be done by running git config
(no --global or --system to make it "local to the repo") or by editing the .git/config
(it's the same syntax as ~/.gitconfig
Solution 3 - Git
You could also set environment variables GIT_COMMITTER_NAME
, GIT_COMMITTER_EMAIL
, GIT_AUTHOR_NAME
, and GIT_AUTHOR_EMAIL
on a per-shell or even per-command basis.
Solution 4 - Git
I believe you could run:
git config user.name "Your Name"
git config user.email [email protected]
for different parts of git. Try opening the folder where the work is being done for the repo to the github server.
Make sure that you see a .git folder in that work folder. Now open that folder in Terminal/Console/CommandPrompt and try changing the username and email for that folder only by running the commands I specified above.
By changing that, you may now be able to access the GitHub server.
Take a look here: https://help.github.com/articles/setting-your-username-in-git
Also, I believe there is a way to push to a remote repo by using your github username and password in the command itself, but I'm not quoting it because it didn't work for me.
Solution 5 - Git
Following up on Jesse Glick's answer here with an expanded implementation because IMO his suggestion is the best way to go for this.
Add the below to your .bash_profile. It will prompt you for a user when first running any git command in the active shell. It'll then remember the user for all subsequent runs.
(obviously cross reference an input user value to exact desired names and email addresses for the various git auther/name values in the case block and update the assigns as needed)
git() {
echo "Running BETTER git..."
if [ -z "$GIT_COMMITTER_NAME" ]; then
while true; do
read -p "Git User: " UNAME
case $UNAME in
user1 ) break;;
user2 ) break;;
* ) echo "Invalid User";;
esac
done
GIT_COMMITTER_NAME=$UNAME
export GIT_COMMITTER_NAME
GIT_COMMITTER_EMAIL=$UNAME@domain.com
export GIT_COMMITTER_EMAIL
GIT_AUTHOR_NAME=$GIT_COMMITTER_NAME
export GIT_AUTHOR_NAME
GIT_AUTHOR_EMAIL=$GIT_COMMITTER_EMAIL
export GIT_AUTHOR_EMAIL
fi
echo " using git user: $GIT_AUTHOR_NAME / $GIT_AUTHOR_EMAIL"
/usr/bin/git "$@"
}
Solution 6 - Git
> You could also set environment variables GIT_COMMITTER_NAME
, GIT_COMMITTER_EMAIL
, GIT_AUTHOR_NAME
, and GIT_AUTHOR_EMAIL
on a per-shell or even per-command basis.
Actually, since Git 2.22 (Q2 2019) you have a new option: Four new configuration variables {author,committer}.{name,email}
have been introduced to override user.{name,email}
in more specific cases.
See commit 39ab4d0 (04 Feb 2019) by William Hubbs (``).
(Merged by Junio C Hamano -- gitster
-- in commit 4e021dc, 07 Mar 2019)
> ## config
: allow giving separate author and committer idents
> The author.email
, author.name
, committer.email
and committer.name
settings are analogous to the GIT_AUTHOR_*
and GIT_COMMITTER_*
environment variables, but for the git config
system.
This allows them to be set separately for each repository.
> Git supports setting different authorship and committer information with environment variables. However, environment variables are set in the shell, so if different authorship and committer information is needed for different repositories an external tool is required.
> This adds support to git config for author.email
, author.name
, committer.email
and committer.name
settings so this information can be set per repository.