Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic git config user.name - depend on hostname

I use git from two computers.

I would like to:

  • Use the same ~/.gitconfig file on the two computers.
  • Have different user.name on commits from one computer or the other.

In short, I'd like to be able to have something like user.name = "Mic - #{Hostname}", with Hostname being dynamically read.

Is there a way to achieve this?

The only other way I see would be to manually change the user.name on each git repo I use on those 2 computers - but I don't find it clean.


Solution for those in this situation: There seems to be no built-in solution as git does not auto-expand config parameters. So, following the advice below, I just issue the following command in my deploying script after importing a new (shared) ~/.gitconfig:

git config --global user.name "Mic [`hostname`]"

That way, git name is always updated.

Depending on your config, you can also put that in your ~/.bashrc or whatever ~/bash_profile you use - although it looks a bit overkill.

like image 230
Qortex Avatar asked May 15 '18 09:05

Qortex


People also ask

Which git config property determines the name which is associated with a users commit?

To set your Git username, run the git config –global user.name command. You should specify both your first and last name but your username can be anything you want to attach to your commits. Your Git username does not need to be the same as your version control username, such as the one you use on GitHub.

Is git config global per user?

One thing to note is that each user gets their own global Git config file. This can cause problems if you run a shell script with sudo command. If sudo is used in a script, the ~root/. gitconfig file will be used instead of the global git config file of the user running the script.

What is user name in git config?

About Git usernamesYou can change the name that is associated with your Git commits using the git config command. The new name you set will be visible in any future commits you push to GitHub from the command line. If you'd like to keep your real name private, you can use any text as your Git username.


1 Answers

Do not store name in ~/.gitconfig but in your ~/.bash_profile set environment variables:

GIT_AUTHOR_NAME="Mic - $HOSTNAME"
GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"
export GIT_AUTHOR_NAME GIT_COMMITTER_NAME
like image 168
phd Avatar answered Sep 19 '22 17:09

phd