Git全局配置不生效求助:已配置全局信息但新项目无法识别
Hey, let's break down what's going on here—you've set up your global Git credentials with git config --global user.name "My Name" and git config --global user.email "me@email.com", verified they show up with git config -l, but when you spin up a new repo and try to commit, it's acting like those settings don't exist. The [master (root-commit...] part is actually normal for your first commit in a new repo, but if you're getting errors about not having a user name/email set, here's how to fix it:
Possible Causes & Fixes
1. Local Repo Config Overriding Global Settings
It's possible you accidentally set a local config in the parent directory of your new test repo, or you initialized the new repo inside an existing Git repo (nested repos cause all sorts of weirdness). Local config always takes priority over global.
- Check what local config is set for the test repo:
git config --local user.name git config --local user.email - If the output isn't your global credentials, you have two options:
- Remove the local config to let global settings take over:
git config --local --unset user.name git config --local --unset user.email - Or update the local config to match your global values:
git config --local user.name "My Name" git config --local user.email "me@email.com"
- Remove the local config to let global settings take over:
2. Permissions Issue With Global Config File
Your global config lives at ~/.gitconfig (Linux/macOS) or C:\Users\<YourUsername>\.gitconfig (Windows). If Git can't read this file due to incorrect permissions, it'll ignore your global settings.
- Check permissions (Linux/macOS):
Make sure the file is owned by your user and has permissions set to at leastls -l ~/.gitconfigrw-r--r--(644). Fix permissions if needed:chmod 644 ~/.gitconfig
3. Environment Variables Are Overriding Config
System environment variables like GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, GIT_COMMITTER_NAME, and GIT_COMMITTER_EMAIL can bypass your Git config entirely.
- Check if these variables are set:
echo $GIT_AUTHOR_NAME echo $GIT_AUTHOR_EMAIL echo $GIT_COMMITTER_NAME echo $GIT_COMMITTER_EMAIL - If any of these return a value, they're overriding your settings. You can either:
- Remove the
exportlines for these variables in your shell config (like~/.bashrcor~/.zshrc), or - Unset them temporarily for your current session:
unset GIT_AUTHOR_NAME unset GIT_AUTHOR_EMAIL unset GIT_COMMITTER_NAME unset GIT_COMMITTER_EMAIL
- Remove the
4. Verify Config Priority
Git applies settings in this order: Local repo config (.git/config) > Global config (~/.gitconfig) > System config (/etc/gitconfig). To confirm which settings are actually active for your test repo, run:
git config --get user.name git config --get user.email
If the output isn't your global values, you know a higher-priority config is in play—go back to the earlier steps to track it down.
Just a quick note: The [master (root-commit...] message you saw is totally normal for your first commit in a brand-new repo. The real issue here is likely that Git was complaining about missing user credentials despite your global config, which the above fixes should resolve.
内容的提问来源于stack exchange,提问作者MBR

