Sub topic 2
System, Global and Local Scopes
This chapter assumes Git is installed, you are comfortable in a terminal, and you know what a repository and a commit are. By the end you will know the three places Git reads settings from and how to choose which one to write to.
Why Git has scopes
Git runs on shared servers, on personal laptops, and inside many separate projects. A single list of settings cannot serve all of them. Your name should follow you everywhere. A line-ending rule might apply to one project only. A company proxy might apply to every user on a build machine.
Git solves this with scopes. A scope is a level at which a setting applies. Each scope has its own configuration file, and git config writes to one of them depending on the flag you pass.
The three scopes
| Scope | Flag | Applies to | Typical use |
|---|---|---|---|
| System | `--system` | Every user on the machine | Organization-wide defaults |
| Global | `--global` | Your user account, all repositories | Name, email, editor |
| Local | `--local` | One repository | Project-specific overrides |
Local is the default when you run git config inside a repository and give no scope flag. Outside a repository, a write without a flag fails, so you must name a scope.
git config --global user.name "Ada Lovelace"
git config --global user.email "[email protected]"These two commands write to your global file, so every repository you use will record Ada Lovelace as the author unless a narrower scope says otherwise.
To override the email for one project, change into it and use --local:
cd my-project
git config --local user.email "[email protected]"This writes to .git/config inside that repository. Other repositories still use the global email.
How the scopes combine
Git reads the system file first, then the global file, then the local file. When the same key appears in more than one place, the value read last wins. A local value therefore beats a global one, and a global value beats a system one. Keys that appear in only one scope are all kept, so the effective configuration is a merge of the three.
The next chapters cover where each file lives and how to see which file supplied a value. For now, remember the rule: the narrower the scope, the higher its priority.
Choosing a scope
Ask who should be affected by the setting.
- Should everyone on this machine get it? Use
--system. This usually needs administrator rights, and most individual developers never need it. - Should it follow you into every project? Use
--global. This is the right choice for most first-time setup. - Should it apply to one project only? Use
--local.
A quick check
Run these two commands inside a repository where you set a local email, as above:
git config --global user.email
git config user.emailThe first prints the global value, [email protected]. The second prints the effective value, [email protected], because the local scope overrides the global one.
Exercise
You run git config user.name "Ada" inside a repository, with no scope flag. Which file does this write to, and will a different repository on the same machine see the new name?
Show answer
It writes to the local file, .git/config in that repository. A different repository will not see the name, because local settings apply only to the repository that holds them. To apply the name everywhere, use --global.
Exercise
The system scope sets core.editor to nano, and your global scope sets it to vim. No local value exists. Which editor does Git use, and why?
Show answer
Git uses vim. It reads the system file first and the global file after it, and the later value wins, so the global value overrides the system value.