Time to study: ~35 min You will learn: where git keeps ur name and email, the difference between global and local config, how a config file is actually shaped, and aliases.
← Chapter 1 | back to Git 101 | next: Chapter 3 →
1. what is the config file
it is a plain text file where git keeps its settings, and the most important setting in it is who u are, so that every commit u make can say who made it.
we already used its command in Chapter 1, when git refused to commit until we told it our name:
git config --global user.name "your name"
git config --global user.email "youremail@example.com"the
--globalflag means “for me, on this machine, in every repo”. it writes to a file in ur home directory called~/.gitconfig, and u can just open it and look:
cat ~/.gitconfig[user]
name = mohamedattiadev
email = youremail@example.com
that is all the command did. there is no database and no magic, it wrote 3 lines into a text file. u could have opened it with
nanoand typed them urself.
2. global vs local
--globalis for u.--localis for one project only, and it writes into that project’s own.git/config:
# inside the repo u want it in
git config --local user.name "your work name"
git config --local user.email "you@company.com"cat .git/config[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[user]
name = your work name
email = you@company.com
when git needs a setting, it reads the local one first, and only falls back to the global one if the local file does not have it. so local wins.
git looks for a setting in this order:
.git/config (local) -> this project only WINS
↓ not there?
~/.gitconfig (global) -> u, on this machine
↓ not there?
/etc/gitconfig (system) -> everyone on this machine
and this is not a made up scenario. it is exactly what u do when ur personal projects are signed with ur personal email, and ur work repos have to be signed with the company one. global = personal, local = the work repo. u set it once per work project and forget it.
- note:
--localwith no repo around it fails, because there is no.git/configto write into:
git config --local user.name "ati"fatal: --local can only be used inside a git repository
3. section, key, value
the file has exactly 3 things in it, and once u see them u can read any gitconfig:
[user] <- section
name = mohamedattiadev <- key = value
email = youremail@example.com
- section: the thing in the square brackets.
[user],[core],[alias]. - key: the name on the left of the
=. - value: whatever is on the right of the
=.
and that is also how u write it on the command line. the command is always
section.key, with a dot:
git config --global user.name "ati"
# ↑ ↑ ↑
# section key value4. reading, adding and removing settings
u can make ur own sections too. git does not care, it will store anything u put in there:
git config --local --add examplesection.name Mohamed
git config --local --add examplesection.surname ATIto see everything that is set:
git config --list --localcore.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
examplesection.name=Mohamed
examplesection.surname=ATI
to get one single value:
git config --local --get examplesection.nameMohamed
to remove one key:
git config --local --unset examplesection.nameand to remove the whole section with everything in it:
git config --local --remove-section examplesectiongit config --listwith no flag shows u everything git can see, all 3 files merged together, which is usually what u actually want when something is set and u cannot find where.git config --list --show-originshows the file each line came from. this is the one that ends the argument about which config is winning.
5. the settings worth setting today
u will meet all of these later in the course, but they are settings, so they belong here. set them now and ur life gets easier from Chapter 5 onwards:
# the default branch name for new repos. `master` is the old default,
# `main` is what everyone uses now, and it is what github creates.
git config --global init.defaultBranch main
# ur editor, the one that opens when a command needs a message from u
git config --global core.editor "nano" # or "vim", or "code --wait"
# colored output. usually on already, but make sure
git config --global color.ui auto
# remember how u solved a conflict, and solve it the same way next time
# (this one gets explained properly in Chapter 6)
git config --global rerere.enabled true6. aliases, ur own git commands
an alias is a shortcut u define once and then use forever. it lives in the
[alias]section:
git config --global alias.st "status -s"
git config --global alias.lg "log --oneline --graph --all --decorate"now these work like real git commands:
git st?? notes.md
git lg* 79e91e0 (HEAD -> main) 2nd commit
* 70d5c6e add Readme.md
and in the file it looks like this:
cat ~/.gitconfig[user]
name = mohamedattiadev
email = youremail@example.com
[init]
defaultBranch = main
[alias]
st = status -s
lg = log --oneline --graph --all --decorate
git lgis the one i actually use. that long log command is impossible to remember and u want it 20 times a day.- do not go crazy with aliases early. u are still learning the real commands, and typing them is how they stick. add an alias when u have typed the same long thing 10 times.
NOTES
-
u can edit the file by hand.
git config --global --editopens it in ur editor, and typing the section by hand is often faster than 4--addcommands. -
the config is not committed.
.git/configlives inside.git, and.gitis never part of ur project’s files. so ur local settings never travel to github and never reach anyone who clones ur repo. -
checking what a repo will sign ur commits with, before u commit into a repo u forgot the settings of:
git config user.emailyouremail@example.com
- this is a chapter u read once and then only come back to when something is set wrong. that is normal. u are learning where the setting lives, not memorising a list of them.
Assignment
- Print ur global name and email using a git command, not
cat. - Find the file the global config lives in, and print it with
cat. - Inside
practice-gitfrom Chapter 1, set a local email different from ur global one. - Prove the local one wins, using a git command that prints just the email.
- Show all the settings git can see, with the file each one came from.
- Add a section
coursewith a keynameset togit-101, locally. - Read that key back, then delete the whole section.
- Set the default branch for new repos to
main. - Make an alias
git stthat runs the short status, and use it. - Make an alias
git lastthat shows only the most recent commit in one line, and use it.
stuck, or done and want to check? the solutions are here