152

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 <MyNameEmail@email.com>" 

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 you@example.com

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?

Chilledrat
  • 2,593
  • 3
  • 28
  • 38
FilBot3
  • 3,460
  • 6
  • 33
  • 55
  • 2
    Git distinguishes between the *author* and the *committer*. To set the committer temporarily for one command, use `-c user.name=Me user.email=me@example.com`. To use a custom global config, see [this question](http://stackoverflow.com/questions/3592351/how-can-i-specify-custom-global-gitconfig-path), or use a shell alias containing `-c` options. But neither of these are probably the right thing to do in this situation - you want a per-repo config. – Robin Green Nov 07 '13 at 16:23
  • With Git 2.22 (Q2 2019), you will have dedicated settings `{author,committer}.{name,email}`. [See my answer below](https://stackoverflow.com/a/55083513/6309) – VonC Mar 10 '19 at 01:03

6 Answers6

291

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.

Michael
  • 8,362
  • 6
  • 61
  • 88
John Szakmeister
  • 44,691
  • 9
  • 89
  • 79
  • 16
    this is _exactly_ what I needed since I need to commit from shared account (root on devel server) into git repo under myself and I don't want others to be offered my name/email. – andrej Aug 26 '16 at 16:52
77

Try to prepend this to your git command :

git -c user.email=email@domain.fr -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

Asenar
  • 6,732
  • 3
  • 36
  • 49
  • 3
    Thank you! I'm sharing a server with multiple people and don't want to set a default user. This is perfect! – horriblyUnpythonic Nov 12 '16 at 02:26
  • 1
    @horriblyUnpythonic you're welcome :) You can also create an alias in your `.bash_aliases`, and/or you can also set a pre-commit hook to reject commit when user is not defined. – Asenar Dec 18 '16 at 14:25
  • Note, the `-c =` option should be inserted directly after `git` and before the relevant git command (e.g. `log` or `commit`). For example: `git -c log.showSignature=false log` runs `git log` but without showing commit signature info (sometimes the extra lines of signature text are an issue especially when they appear despite you specifying the `--one-line` option expecting it to force `git` to only output a single line). – baltakatei Mar 02 '23 at 17:46
38

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.

Jesse Glick
  • 24,539
  • 10
  • 90
  • 112
  • Great, as this would mostly be used in a shared computer scenario, per session with automatic cleanup is perfect. – kmac Nov 09 '16 at 17:22
  • 1
    Didn't work for me. The author stayed the same after `git commit --amend`. Only the commiter changed. – Ondra Žižka Dec 12 '17 at 16:40
  • 2
    Same here so I did `git commit --amend --author="$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"` – KCD Feb 25 '19 at 03:40
  • 3
    This is because `git commit --amend` by design does not alter the author, as the manpage says. Solution: `git commit --amend --reset-author` – kubanczyk Mar 17 '20 at 12:51
8

I believe you could run:

git config user.name "Your Name"
git config user.email you@example.com

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.

Joe
  • 3,120
  • 3
  • 25
  • 30
8

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 "$@"
}
slumtrimpet
  • 3,159
  • 2
  • 31
  • 44
5

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • It seems that you need to set *all 6* of these variables, to avoid git asking you to configure or user name (using Git 2.25.1) – Supernormal May 04 '22 at 03:41
  • @Supernormal 6? What are the additional two? user.name and user.email? – VonC May 04 '22 at 05:56
  • `GIT_COMMITTER_NAME`, `GIT_COMMITTER_EMAIL`, `GIT_USER_NAME`, `GIT_USER_EMAIL`, `GIT_AUTHOR_NAME`, `GIT_AUTHOR_EMAIL` – Supernormal May 05 '22 at 09:41
  • @Supernormal Oh, OK. I was not aware the user name would be needed when you the the author and committer name. – VonC May 05 '22 at 10:26
  • Note to self: this Necromancer badge is my 10000th badge. – VonC May 29 '23 at 18:11