Home > Software engineering >  Git not using user from Global Configuration file
Git not using user from Global Configuration file

Time:06-08

I am using Ubuntu 22.04 on AWS EC2, git version 2.34.1

Below is my ~/.gitconfig file:

[credential "https://github.com"]
        helper =
        helper = !/usr/bin/gh auth git-credential
[credential "https://gist.github.com"]
        helper =
        helper = !/usr/bin/gh auth git-credential
[safe]
        directory = /var/www/html/wix
        directory = /var/www/html/wix-native-app
[user]
        name = JXX XXXha
        email = [email protected]
        signingkey = 8XXXXXXXXXXX1
[commit]
        gpgsign = true

However when I try to commit, I get below error:

ubuntu@ip-172-31-46-106:/var/www/html/wix-native-app$ sudo git commit
Author identity unknown

*** Please tell me who you are.

Run

  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: no email was given and auto-detection is disabled

But as seen in my .gitconfig file, username and email are already set. Is there anything else to be done in configuration for commit to work?

CodePudding user response:

You are executing git with sudo i.e. as user root. So git will look for the config in /root/.gitconfig, while you set the config in ~/.gitconfig i.e. in the home path of your non-privileged user.

You have two options:

  1. Change permissions and/or ownership of /var/www/html/wix-native-app so that your unpriviledged user ubuntu has permissions to read and write this path. Make sure to include -R to your chmod/chgrpcommands to apply the changes to all files and subfolders.

    Pros:

    • You do not need a privileged user to change the content of your web server. Users with the right to change the content will not get the rights to tamper with the server config.
    • You can use groups to assign permissions to multiple users.
    • If you have different users responsible for different sub-folders of /var/www/html/, you can assign them write permissions to only the folders they are in charge of.
  2. Copy your ~/.gitconfig to /root/.gitconfig so git will find the config if you run it sudo'ed.

    Con:

    • In most cases, you should not consider changing the web servers content as an administrative task which requires privileged account.
  • Related