Override configured user for a single git commit
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 <[email protected]>
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?
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.
Try to prepend this to your git command :
git -c [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
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.
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.
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
[email protected]
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 "$@"
}