Home > Software engineering >  Is it Possible to set Git Credentials on a Per-Host Basis?
Is it Possible to set Git Credentials on a Per-Host Basis?

Time:10-30

My school has its own GitLab instance, and I'd like Git to use my real name and student email for commits on that instance. Everywhere else, I want to use my standard username and email. Setting this on a per-repo basis won't be feasable since I need to create multiple repos a week.

CodePudding user response:

First, the user.name and user.email are not credentials; they have no effect on authentication whatever. The former is typically not a username. It is intended to be a personal name (that is, something like "Pat Doe").

It isn't possible to set them on a per-host basis because the values are applied when you make a commit, not when you push. It isn't necessarily the case that the remotes that are set at commit time will continue to be the same when you push, or that you have any remotes specified at all. There is a proposal to allow this on the Git list, but it has some problems and it's unknown if it will be adopted.

However, you can include files into your configuration depending on the location of the .git directory. For example, you could include separate config files like this:

[user]
    name = "Pat Doe"
    email = [email protected]
[includeIf "gitdir:~/checkouts/school/**"]
    path = ~/.config/git/config-school

and then include the values for your school in ~/.config/git/config-school. As long as you put your school's clones in ~/checkouts/school, things will just work.

  •  Tags:  
  • git
  • Related