日本語

How to Change Git User Name and Email per Repository

​​Git​command line

To change the name and email recorded in Git commits for only one repository, run git config user.name and git config user.email inside that repository without --global. This is useful when you want separate work and personal email addresses, or when you want a public repository to use a different name. If you have many repositories, you can also use includeIf to switch identities by folder at once. For both methods, I checked the output of git config and git log before and after each change.

Note

The test environment was Ubuntu 24.04 with Git 2.43.0. I used example values for the name and email (Neko <neko@example.com> and Tama <tama@example.org>). The repositories were created directly under the home folder, and I ran the tests with the system-wide configuration file not read (GIT_CONFIG_NOSYSTEM=1). I did not test on Windows or macOS, or push to hosting services such as GitHub.

Check Which Configuration the Current Name and Email Come From

First, I set Neko <neko@example.com> in the global configuration (--global). Running git config user.name and git config user.email inside the repository cat-tools shows the values in use. Adding --show-scope also shows which configuration each value comes from.

console
$ git config user.name
Neko

$ git config user.email
neko@example.com

$ git config --show-scope --get user.email
global	neko@example.com

global means the global configuration, which in this case is written in ~/.gitconfig in the home folder. When I committed at this point, the author was the name and email from the global configuration.

console
$ git log -1 --format="%an <%ae>"
Neko <neko@example.com>

Configure a Repository Without –global

For only cat-tools, I changed the name and email to Tama <tama@example.org>. Inside the repository, I ran git config without --global.

console
$ git config user.name "Tama"

$ git config user.email "tama@example.org"

$ git config user.name
Tama

$ git config --show-scope --get user.email
local	tama@example.org

The source of the value changed to local (this repository's configuration). As stated in the official git config reference, when you write without --global or similar options, the value is written to the repository's .git/config, the same as with --local. When I opened .git/config, a [user] section had been added.

console
$ cat .git/config
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
[user]
	name = Tama
	email = tama@example.org

Listing all settings starting with user. using --get-regexp shows both the global and this repository's values.

console
$ git config --show-scope --get-regexp "^user\."
global	user.name Neko
global	user.email neko@example.com
local	user.name Tama
local	user.email tama@example.org

According to the official reference, Git reads configuration files in the order global, then repository, and the value read later takes priority. So in cat-tools, which has a repository setting, Tama is used.

Diagram showing that Tama from the repository setting overrides Neko from the global setting in cat-tools, that Tama loaded via includeIf overrides it under ~/oss/, and that shop, with no setting, keeps Neko
How per-repository settings and includeIf change the commit author

Other Repositories Keep the Global Setting

In another repository, shop, I had not set anything per repository, so the global setting was used as is. The contents of the global configuration file ~/.gitconfig also still show the Neko I entered first.

console
$ git config --show-scope --get-regexp "^user\."
global	user.name Neko
global	user.email neko@example.com

$ git log -1 --format="%an <%ae>"
Neko <neko@example.com>

$ cat ../.gitconfig
[init]
	defaultBranch = main
[user]
	name = Neko
	email = neko@example.com

Fix the Author of Commits Made Before the Change

Changing the setting does not change the author of commits you already made. Right after changing the setting in cat-tools, the author of the first commit was still Neko.

console
$ git log --format="%h %an <%ae> %s"
07b1861 Neko <neko@example.com> 最初のコミット

For just the most recent commit, you can fix it to the current name and email with git commit --amend --no-edit --reset-author. According to the official git commit reference, --reset-author is an option used together with --amend and similar options, and it replaces the commit's author with the person making the commit now. The author date is also updated.

console
$ git commit -q --amend --no-edit --reset-author

$ git log --format="%h %an <%ae> %s"
d297218 Tama <tama@example.org> 最初のコミット

The author changed to Tama, and the commit hash changed from 07b1861 to d297218. Because this creates a different commit, if you use it on a commit you have already pushed, also check the section on amending a pushed commit in the separate article "How to fix the most recent commit with git commit –amend". New commits made afterward became Tama without any extra steps.

console
$ git log --format="%h %an <%ae> %s"
31e1290 Tama <tama@example.org> 使い方を追加
d297218 Tama <tama@example.org> 最初のコミット

Remove the Repository Setting and Return to the Global Setting

To remove a repository setting, run the command with --unset. When I removed it in cat-tools, the source of the value went back to only the global setting, and the [user] section was removed from .git/config.

console
$ git config --unset user.name && git config --unset user.email

$ git config --show-scope --get-regexp "^user\."
global	user.name Neko
global	user.email neko@example.com

$ cat .git/config
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true

Switch Identities by Folder with includeIf

As the number of repositories grows, setting each one individually becomes tedious. By writing includeIf in the global configuration, you can load a different configuration file only for repositories under a chosen folder. Here, I set the repositories under the oss folder in the home folder (cat-lib and cat-cli) to Tama. All commands from here on were run from the home folder. The cd in each line is there only to run that line's command, and the next line starts again from the home folder.

First, I created the file ~/.gitconfig-oss with the name and email I wanted to switch to.

console
$ printf "[user]\n\tname = Tama\n\temail = tama@example.org\n" > .gitconfig-oss

Next, I added includeIf to the global configuration. gitdir:~/oss/ is the condition "the repository's .git folder is under ~/oss/".

console
$ git config --global includeIf."gitdir:~/oss/".path "~/.gitconfig-oss"

$ cat .gitconfig
[init]
	defaultBranch = main
[user]
	name = Neko
	email = neko@example.com
[includeIf "gitdir:~/oss/"]
	path = ~/.gitconfig-oss

Inside oss/cat-cli, tama@example.org was used, and inside shop, which is outside oss, neko@example.com was used. With --show-scope, the values from the loaded file are also shown as global, with Tama listed after Neko. The later Tama is the one used.

console
$ cd oss/cat-cli && git config --show-scope --get-regexp "^user\."
global	user.name Neko
global	user.email neko@example.com
global	user.name Tama
global	user.email tama@example.org

$ cd oss/cat-cli && git config user.email
tama@example.org

$ cd shop && git config user.email
neko@example.com

When I committed in oss/cat-lib, the author became Tama.

console
$ cd oss/cat-lib && printf "# cat-lib\n" > README.md && git add . && git commit -q -m "最初のコミット" && git log -1 --format="%an <%ae>"
Tama <tama@example.org>

The Trailing / in gitdir and Where to Place includeIf

According to the official git config reference's explanation of conditional includes, the part after gitdir: is a pattern matched against the location of the .git folder, and a pattern ending in / matches everything under it. As a test, I removed the includeIf I had added and added it again with gitdir:~/oss without the trailing /. Then cat-lib under oss went back to neko@example.com.

console
$ git config --global --unset includeIf."gitdir:~/oss/".path

$ git config --global includeIf."gitdir:~/oss".path "~/.gitconfig-oss"

$ cd oss/cat-lib && git config user.email
neko@example.com

The position where includeIf is written also affects the result. I rewrote ~/.gitconfig so that includeIf came before [user], and then cat-lib under oss had the Neko email. According to the official reference, the contents of an included file are treated as if they were at the position where includeIf is written. So the Neko from the global configuration is read after the Tama that was loaded. When I added includeIf with git config --global, in the test environment it was written at the end of the file.

console
$ cat .gitconfig
[includeIf "gitdir:~/oss/"]
	path = ~/.gitconfig-oss
[user]
	name = Neko
	email = neko@example.com

$ cd oss/cat-lib && git config user.email
neko@example.com

After checking, I set includeIf back to gitdir:~/oss/ and returned ~/.gitconfig to its original order.

Stop Commits in Repositories Where You Forgot to Configure (user.useConfigOnly)

If you forget to configure a repository, it will commit using the name and email from the global configuration. To prevent this, remove the name and email from the global configuration and set user.useConfigOnly to true. According to the description of user.useConfigOnly in the official git config reference, when it is true, Git does not guess the name and email and uses only the values in the configuration files.

console
$ git config --global --unset user.name && git config --global --unset user.email

$ git config --global user.useConfigOnly true

In this state, committing in shop, which has no repository setting, stopped with an error.

console
$ cd shop && printf "送料\n" >> README.md && git commit -q -am "送料を追加"
Author identity unknown

*** Please tell me who you are.

Run

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

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: no email was given and auto-detection is disabled

As the Omit --global … line in the message says, if you set it without --global, the name and email are set only for that repository, and you can commit. In a repository under oss, where includeIf loads the name and email, I could commit as is.

console
$ cd shop && git config user.name "Neko" && git config user.email "neko@example.com" && git commit -q -am "送料を追加" && git log -1 --format="%an <%ae> %s"
Neko <neko@example.com> 送料を追加

$ cd oss/cat-cli && printf "# cat-cli\n" > README.md && git add . && git commit -q -m "最初のコミット" && git log -1 --format="%an <%ae>"
Tama <tama@example.org>

​Posted by 86