Search code examples
gitgit-config

How to unset default git user on cloned repositories?


I use different emails and users in different git repos. My git global config file has the user section unset. Here it is (notice how nothing comes after [user]):

[core]
    editor = nano
    pager = less -x1,5
[push]
    default = simple
[merge]
    tool = meld
[mergetool "meld"]
    path = /usr/bin/meld
[mergetool]
    prompt = false
[alias]
    adog2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
    adog = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n''          %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
    cc = checkout
    co = checkout
    s = status
    cm = checkout
    ck = checkout
    u = reset HEAD --
    last = log -1 HEAD
    st = status
    ct = checkout
    unstage = reset
    cok = checkout
[user]

Whenever I clone a new git repo, the git local config username and email always comes set with a user that I use in one of the other repos, despite the global user being unset. This leads me to accidentally making commits with the wrong user.

How to unset default git user on cloned repositories so that whenever I clone a new repo, the username and email always come empty? Is there a template file from where git copies the local git config?


Solution

  • We can say the following about user.name and user.email:

    • Git will read them from configuration files. The configuration file rules—in particular their order—are defined in the documentation, but in general user.name and user.email in a global config will be overridden by any setting you have in a local config.

    • git clone does not set the local user.name and user.email unless explicitly told to do so. The git clone command takes -c flags—these must appear after the clone sub-command; those inserted before clone are used during cloning but not set in the new clone's local configuration—and these settings are inserted into the .git/config in the new clone. That is:

      git clone -c user.name=thor -c [email protected] <url>
      

      will create the new repository such that the user.name and user.email settings are as shown. Without these -c options, however, the local configuration won't have any such settings; git config would have to be run to set them, or some program could of course open and write to .git/config directly.

    • Depending on how Git is built, if user.name and user.email are not set anywhere, you may get a runtime complaint, or you may get a default value.

    The last is a bit tricky. Normally, Git used to just complain:

    *** Please tell me who you are. ...
    

    People didn't like this, so Git learned to "guess" the user name and email address. People didn't like that either, so Git 2.8 learned a new configuration option, user.useConfigOnly. You may wish to set this to true, to force Git to go back to complaining as above.