27 27. Installing and Configuring Git
Before you can use Git, it has to be installed, and you have to tell it who you are. This takes about ten minutes, and you do it once per computer.
27.1 Is Git already installed?
Open your terminal (Terminal on a Mac, Git Bash on Windows) and type:
git --versionOutput (your version number will be different):
git version 2.56.0
If you see a version number, Git is installed. Any reasonably recent version works for this book. On a Mac or on Windows the line may have a little extra text after the number, such as (Apple Git-...) or .windows.2.
27.2 Installing Git
Option 1 (easiest): Apple’s Command Line Tools. Type git --version in Terminal. If Git isn’t installed yet, macOS offers to install Apple’s Command Line Tools, a free package of developer tools that includes Git. Accept, wait for the download to finish, and run git --version again. (You can also start this install yourself with xcode-select --install.)
Option 2: Homebrew. If you use Homebrew (a popular “package manager” for installing developer tools on a Mac), this installs the newest version of Git:
brew install gitApple’s Git is usually a few versions behind the newest one. That doesn’t matter for this book.
You already have Git. Git for Windows, which you installed in Getting Bash on Your Computer to get Git Bash, is Git. Open Git Bash and run git --version to check.
The installer asked several questions about Git. These four matter most:
| Installer screen | Recommended choice |
|---|---|
| Choosing the default editor used by Git | Visual Studio Code (if it’s installed), otherwise Nano. Not Vim, the default. |
| Adjusting the name of the initial branch in new repositories | “Override the default branch name for new repositories”, with main |
Choose the default behavior of git pull |
Merge (the default) |
| Choose a credential helper | Git Credential Manager (the default) |
If you skipped these screens (for example, if you installed with winget, which accepts every default), just run the configuration commands below. They fix everything.
27.3 Tell Git who you are
Every commit records an author: a name and an email address. Set yours once:
git config --global user.name "Maria Lopez"
git config --global user.email "maria@example.com"These commands print nothing when they succeed. Use your real name (or the name you want others to see), with quotes around it because it contains a space.
--globalmeans “for my user account on this computer”, so the setting applies to every repository you create or copy here.- Use the same email as your GitHub account, so GitHub can link your commits to your profile. Anyone who can see your repository can see this email. To keep your address private, GitHub gives you a noreply address: on github.com go to Settings > Emails, turn on Keep my email addresses private, and use the address shown there. It looks like
ID+USERNAME@users.noreply.github.com.
27.4 Three recommended settings
git config --global init.defaultBranch main
git config --global core.editor "nano"
git config --global pull.rebase falseinit.defaultBranch main: the name of the first branch in every new repository. Plain Git still calls itmasterand prints a long hint about it; GitHub usesmain. (Some installations, such as Apple’s Git, already usemain. Setting it yourself is harmless and makes every computer behave the same.)core.editor: the text editor Git opens when it needs you to type something, for example a commit message when you forget-m. Nano is a simple editor that runs inside the terminal: type your text, save withCtrl+OthenEnter, and exit withCtrl+X. If you prefer VS Code, usegit config --global core.editor "code --wait"instead (on a Mac, first open VS Code’s Command Palette and run “Shell Command: Install ‘code’ command in PATH”).pull.rebase false: tellsgit pullwhat to do when both your computer and GitHub have new commits: merge them. Without any setting, Git stops with an error in that situation (you’ll meet it in Cloning and Pulling). The Windows installer usually sets this already; on a Mac you need this line.
If Git ever opens a screen full of ~ symbols that doesn’t respond normally, you’re in Vim, a powerful but tricky editor. To get out: press Esc, type :wq, and press Enter (this saves and quits). Then set core.editor as shown above so it doesn’t happen again.
27.5 Check your settings
git config --global --listOutput:
user.name=Maria Lopez
user.email=maria@example.com
init.defaultbranch=main
core.editor=nano
pull.rebase=false
(Git shows setting names in lowercase, such as init.defaultbranch; they’re not case-sensitive.) To check a single setting, give its name:
git config user.nameOutput:
Maria Lopez
If git config --global --list prints an error such as fatal: unable to read config file '/Users/maria/.gitconfig': No such file or directory, you haven’t set anything yet.
On Windows, the choices you made in the installer are stored in a different (system-wide) file, so they don’t appear in this list. To see every setting and the file it comes from, run git config --list --show-origin.
27.6 Commit messages and other Git files are plain text
Commit messages, .gitignore files, and code are all plain text. Always write them in a text editor (nano, VS Code, Notepad, TextEdit in plain-text mode), never in a word processor such as Word or Pages. See Writing to Files for the difference.
--global settings live in a plain text file in your home folder, ~/.gitconfig:
cat ~/.gitconfigOutput:
[user]
name = Maria Lopez
email = maria@example.com
[init]
defaultBranch = main
[core]
editor = nano
[pull]
rebase = false
Git actually reads settings at three levels:
| Level | Applies to | Stored in | Set with |
|---|---|---|---|
| system | every user on the computer | a file in Git’s install folder | (the installer) |
| global | you, in all your repositories | ~/.gitconfig |
git config --global ... |
| local | one repository only | .git/config inside the repository |
git config ... (no --global), run inside the repository |
When the same setting appears at several levels, the most specific one wins: local beats global, and global beats system. For example, running git config user.email "maria@company.com" inside one work repository changes the email for that repository only.
Windows ends each line of a text file with two invisible characters (CRLF); Mac and Linux use one (LF). Git for Windows is normally set up (core.autocrlf=true) to convert between them automatically. That’s why Windows users sometimes see warnings like:
warning: in the working copy of 'README.md', LF will be replaced by CRLF the next time Git touches it
These warnings are harmless. You can ignore them.
Practice Questions
Easy
Question 1 · Write the command
Write the command that shows whether Git is installed, and which version.
Question 2 · Multiple choice
Sam uses Windows and installed Git for Windows in Getting Bash on Your Computer. What does he need to do to install Git?
- Download Git separately from Git for Windows
- Nothing: Git for Windows is Git (plus Git Bash)
- Install Homebrew and run
brew install git - Install the Apple Command Line Tools
Question 3 · Short answer
Why does Git need your name and email address?
Question 4 · True or false
You must run git config --global user.name "..." again in every new repository.
Question 5 · Matching
Match each setting with what it controls.
| Setting | Controls | |
|---|---|---|
1. user.name |
A. the editor Git opens for messages | |
2. user.email |
B. what git pull does when both sides have new commits |
|
3. init.defaultBranch |
C. the author name recorded in commits | |
4. core.editor |
D. the name of the first branch in new repositories | |
5. pull.rebase |
E. the author email recorded in commits |
Question 6 · Short answer
In nano, how do you save the file and how do you exit?
Medium
Question 7 · Write the command
Write the commands Sam should run to set his name to Sam Chen, his email to his GitHub noreply address 12345678+samchen@users.noreply.github.com, and the default branch name to main.
Question 8 · Predict the output
On a brand-new Mac account, Maria runs only these two commands:
git config --global user.name "Maria Lopez"
git config --global init.defaultBranch mainWhat does git config --global --list print?
Question 9 · Scenario
Maria wants to make commits in public GitHub repositories without showing her personal email address. What should she do?
Question 10 · Short answer
What does --global mean in git config --global ...? What happens if you run git config user.email "..." without --global inside a repository?
Question 11 · Read the output
Maria runs git config --global --list and sees:
fatal: unable to read config file '/Users/maria/.gitconfig': No such file or directory
What does it mean, and what should she do?
Question 12 · Scenario
Sam typed git commit without -m, and his terminal filled with ~ symbols. Typing doesn’t seem to work normally. What happened, how does he get out, and how can he prevent it next time?
Question 13 · Short answer
Why is pull.rebase false useful even for someone who works alone?
Hard
Question 14 · Find the bug
Maria typed:
git config --global user.name Maria LopezThere was no error, but her commits show the author as just Maria. Why? How does she fix it?
Question 15 · Scenario
Sam’s commits on GitHub show the author name, but they aren’t linked to his GitHub profile (no profile picture). git config user.email prints sam@laptop.local. What’s wrong, and how does he fix it?
Question 16 · Compare
Explain the difference between system, global, and local Git settings, and which one wins if the same setting appears in more than one.
Question 17 · Short answer
If you use VS Code as Git’s editor, the setting is code --wait, not just code. What could go wrong without --wait?
Question 18 · Scenario
Sam installed Git for Windows with winget, which accepts every installer default. When he runs git init in a new project, Git prints a long hint about the branch name master. Explain why, and what he should run so that this computer behaves like the rest of the class.