1

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?

rayly
  • 41
  • 3
  • 1
    Does this answer your question? [Git: Easiest way to reset git config file](https://stackoverflow.com/questions/35853986/git-easiest-way-to-reset-git-config-file) – Jeremy Jul 09 '22 at 14:58
  • 1
    "the username and email always come empty" Makes no sense; you need to have _some_ username and email. So your current situation is untenable. Remembering to set the "local" username and email, if you don't like the global / automatic value, is up to you. – matt Jul 09 '22 at 15:18
  • "you need to have some username and email." This is not true. I only need the username and email if I want to create a commit, which is what git asks me for when I try to commit something. And that's what I need. I want it to ask me for the email and username every time I commit in a freshly-cloned repository. – rayly Jul 09 '22 at 21:57
  • @Jeremy, it solves partially. Even if I clean up the global config file, I've noticed that git starts using my workplace email and username in freshly cloned repos after I upgrade git to a new version. – rayly Jul 09 '22 at 22:03
  • Side note: `-x1,5` (as `less` option) seems more clearly expressed to me as `--tabs=4`. I'm of the opinion that hardware tabs should always be at 8 though: to get 4-space tabs, use 4 spaces. Of course this is religious war territory (perhaps we need a [Kelly-Bootle compromise](https://twitter.com/codewisdom/status/806529463395741696) of tabs at 5.5 characters ). – torek Jul 09 '22 at 22:04

1 Answers1

1

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 user.email=thor@example.com <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.

torek
  • 448,244
  • 59
  • 642
  • 775
  • your answer is pretty complete to the point that it'd solve the problem for people who have the problem described in my question. But that doesn't seem to be what I'm looking for. Upon testing, I noticed that my actual problem is that whenever I upgrade git to a newer version, it writes a `user.name` and `user.email` in my global config, even though I left it empty. I has nothing to do with `user.name` and user.email` being set in freshly cloned repositories. Should I edit my question, or should I create a new one? – rayly Jul 13 '22 at 00:42
  • That would be a new question, yes. (Speculation: it's probably due to your installer, not to Git itself.) – torek Jul 13 '22 at 00:58