0

I've configured ~/.gitconfig correctly as below.

git config --global user.name "yuzhi"
git config --global user.email "1762266120@qq.com"

Here is the result.

saber@debian:~$ git config -l
user.name=yuzhi
user.email=1762266120@qq.com
core.editor=vim
color.ui=true

saber@debian:~$ git config --global -l
user.name=yuzhi
user.email=1762266120@qq.com
core.editor=vim
color.ui=true

What confuses me is that it does not work when i git commit.

saber@debian:~/ics2020$ sudo git commit

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  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: unable to auto-detect email address (got 'root@debian.(none)')

But it works when i issue git commit after the commands below.

git config --local user.name "yuzhi"
git config --local user.email "1762266120@qq.com"

BTW: i'm running Debian-10.8 on the vmware. and git==2.20.1. here is the similar question as mine, but i don't know how to solve in linux.

Arthur
  • 83
  • 1
  • 4
  • 5
    When you use `sudo`, the local user is not `saber`. The sudo user has its own global configuration. As the log says, the sudo user is `root`. You could use `git config --system` if all the users belong to you only. – ElpieKay Mar 17 '21 at 05:28
  • 4
    Also worth to mention - you should never have to use `git` commands as `sudo`. If so, you've done something wrong. – Croolman Mar 17 '21 at 05:42

1 Answers1

2

You can compare (assuming a recent enough Git, if not: update it) configuration with:

git config --show-origin --show-scope -l
sudo git config --show-origin --show-scope -l

You will clearly see the difference in paths (/home/saber/ vs. /root), explaining why you don't see the same configuration.
That being said, as commented, don't use sudo for regular commands like git.


As noted in the comments, if the repository was initially created with sudo (generating files owned by root), the first commit (done with the regular user) will trigger:

saber@debian:~/ics2020$ git commit 
fatal: Unable to create '/home/saber/ics2020/.git/index.lock': Permission denied.

That means you need to restore the proper ownership:

cd ~/ics2020
sudo chown -R $(id -u saber):$(id -g saber)

Then a simple commit (without sudo) will work.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • You're Right!!! Thanks so much! But it gets me a little bit confused about the option `--show-scope`. ` saber@debian:~/ics2020$ git config --show-origin --show-scope -l error: unknown option show-scope ` Therefore, i remove it and issue ` git config --show-origin -l sudo config --show-origin -l ` the first cammond shows ` file:/home/saber/.gitconfig user.name=yuzhi file:/home/saber/.gitconfig user.email=1762266120@qq.com ` but the output of the second has no user.name and user.email... – Arthur Mar 17 '21 at 09:01
  • 1
    @Arthur `--show-scope` means Git 2.26 minimum (Q1 2020: https://stackoverflow.com/a/60286340/6309). You can update git with ppa: https://stackoverflow.com/a/41357503/6309. – VonC Mar 17 '21 at 09:02
  • anothor question: when issue `git commit`,it shows `saber@debian:~/ics2020$ git commit fatal: Unable to create '/home/saber/ics2020/.git/index.lock': Permission denied`. it needs `sudo`... so i solve it as below. `saber@debian:~/ics2020$ sudo git config --global user.name "yuzhi" saber@debian:~/ics2020$ sudo git config --global user.email "1762266120@qq.com" saber@debian:~/ics2020$ sudo git config --global core.editor vim saber@debian:~/ics2020$ sudo git config --global color.ui true` i don't know why i asked to execute it as `sudo`! – Arthur Mar 17 '21 at 09:17
  • 1
    @Arthur Probably because the repository was initially created with `sudo`, with files owned by `root`. Use `sudo` to restore the proper ownership: `cd /path/to/repo; sudo chown -R $(id -u saber):$(id -g saber)`. Then try to commit again (*without* `sudo`) – VonC Mar 17 '21 at 09:21
  • @Arthur Great! I have edited the answer to reflect that error and its resolution, for more visibility. – VonC Mar 17 '21 at 09:43