Session 2 · Installing and configuring Git#
The big idea#
Before your first commit you tell Git who you are, because every commit is stamped with a name and email. Git keeps settings at three levels (system, global, local); the most specific level wins. You set your identity once globally, and override it in any project that needs a different one.
Everyday example: a visiting card and an office ID. Your personal card (global) has your own name and email. When you work on a client’s project you might hand over the client-issued ID (local) instead. Git uses the more specific one for that project.
Install Git#
System |
Command |
|---|---|
Ubuntu / Debian |
|
macOS |
|
Windows |
Download Git for Windows from git-scm.com |
Check it worked with git --version. Full instructions for every system are in the official
Pro Git book.
Set your identity, and override it for one project#
02-config.sh sets a global identity, then makes a project that uses a different one. It runs in a throwaway
home folder, so your real settings are never touched.
git --version
echo '--- 1. global identity, used by every repository on this computer'
git config --global user.name "DevUser"
git config --global user.email "devuser@example.com"
git config --global init.defaultBranch main
git config --global --list
echo '--- 2. a project that must use a different identity'
git init -q MyProject
cd MyProject
git config user.name "ProjectUser"
git config user.email "projectuser@example.com"
echo '--- who wins inside this project? (and where each value comes from)'
git config --show-origin --get-regexp '^user\.'
echo '--- the value Git will really use'
git config user.name
echo '--- the identity lands on every commit'
git commit -q --allow-empty -m "First commit"
git log -1 --format='%an <%ae>'
git version 2.34.1
--- 1. global identity, used by every repository on this computer
user.name=DevUser
user.email=devuser@example.com
init.defaultbranch=main
--- 2. a project that must use a different identity
--- who wins inside this project? (and where each value comes from)
file:~/.gitconfig user.name DevUser
file:~/.gitconfig user.email devuser@example.com
file:.git/config user.name ProjectUser
file:.git/config user.email projectuser@example.com
--- the value Git will really use
ProjectUser
--- the identity lands on every commit
ProjectUser <projectuser@example.com>
Reading the output:
--globalwrote to~/.gitconfig.git config --global --listshows the three keys we set (Git prints key names in lower case).No flag inside a repository writes to that repository’s
.git/config: the local level.--show-originreveals where each value comes from. The project has bothDevUser(global) andProjectUser(local); the later, more specific one wins.git config user.nameprints the winning value,ProjectUser.The commit is stamped
ProjectUser <projectuser@example.com>, proving which identity was used.
Other settings worth knowing#
Setting |
What it does |
|---|---|
|
Name the first branch |
|
Choose the editor for commit messages |
|
Make |
|
Type |
Common mistakes#
Skipping the identity. Git refuses to commit (“Please tell me who you are”) or guesses a wrong name from your computer.
Using a work email in personal projects, or the reverse. Set a local override per project.
Typos in
--globalversus local. Forgetting--globalinside a repository sets it for that project only, and outside any repository it fails.Changing the identity and expecting old commits to change. Settings apply to new commits only.
Putting secrets in config.
.git/configand~/.gitconfigare plain text files.
Hands-on exercises#
Exercise 1. Set your name and email globally and print them back.
Solution
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global user.name
git config --global user.email
The last two commands print what you just set.
Exercise 2. Make a repository whose commits use Team Bot <bot@example.com> without changing your global identity.
Solution
git init bot-project && cd bot-project
git config user.name "Team Bot"
git config user.email "bot@example.com"
git commit --allow-empty -m "test"
git log -1 --format='%an <%ae>'
It prints Team Bot <bot@example.com>, while other repositories still use your global identity.
Exercise 3. Use git config --show-origin to find which file supplies your user.email in the current repository.
Solution
git config --show-origin user.email
It prints the file path and the value: .git/config if you set a local override, otherwise ~/.gitconfig.
Practice#
Take the Session 2 quiz. The original Git Installation and Initial Configs page is in the reference section.