1. 27
  1. 18

    I’ve left my git global config unset. When I make a new repo and put it under git I do git config user.name and user.email

    I don’t create so many repos per day that I can’t spend a few minutes setting this up at the same time I set up remotes.

    1. 7

      Same. Nice thing about leaving the global config unset: as soon as you try to commit or amend Git prompts you for your name and email, at which point I fill in the repo config.

      I also have 2 nice aliases that set the repo config’s name/e-mail fields: hg work and hg personal.

    2. 5

      This is a really neat approach, I had no idea it worked! So far I’ve used direnv with a config that exports “GIT_AUTHOR_EMAIL”. It’s neat that the git config supports this out of the box.

      1. 2

        I use the same directory structure to separate my repositories and wrote a little wrapper for git-clone to automate it: https://gitlab.com/3point2/git-cu.

        The includeIf is new to me and very useful, thanks!

        1. 2

          I’ve done this for a while and it really helps. If you ever find you goofed the wrong email, you can amend any commits with git commit --amend --author="username <user@email.addr>" --no-edit

          1. 1

            Can you do the same include matching based on repo URL? I may have to try that out.

            1. 21

              With git 2.37 you can use a remote - this example is taken directly from https://git-scm.com/docs/git-config:

              ; include only if a remote with the given URL exists (note
              ; that such a URL may be provided later in a file or in a
              ; file read after this file is read, as seen in this example)
              [includeIf "hasconfig:remote.*.url:https://example.com/**"]
              	path = foo.inc
              [remote "origin"]
              	url = https://example.com/git

              I use this to manage work vs. nonwork email addresses by repo remote. This is how I do it (edited to show a git@ remote URL rather than just https). In my ~/.gitconfig:

              [includeIf "hasconfig:remote.*.url:git@gitlab.com:myname/**"]
                path = ~/dotfiles/shared/gitconfigs/gitlab
              [includeIf "hasconfig:remote.*.url:git@github.com:myname/**"]
                path = ~/dotfiles/shared/gitconfigs/github
              [includeIf "hasconfig:remote.*.url:git@github.com:companyname/**"]
                path = ~/dotfiles/shared/gitconfigs/companyname

              Then, for example, ~/dotfiles/shared/gitconfigs/gitlab:

                sshCommand = "ssh -i ~/.ssh/my-gitlab-specific-key -F /dev/null"
                name       = My Name
                email      = gitlab@myname.domain
                signingkey = ...
                gpgsign = true
                program = gpg
              1. 1

                Thank you so much for sharing! This is going to save me a lot of time.

                1. 1

                  Right on, I’m glad to hear that!

                2. 1

                  oh this would’ve saved me so much hassle

              2. 1

                I personally use https://github.com/DrVanScott/git-clone-init to automatically setup a different user.name/user.email according to the cloned domain.