Installing Git and Telling It Who You Are
Two jobs on this page. Get the git program onto your computer, and tell it your name and email so that everything you record is signed with something meaningful.
The second job takes about thirty seconds and is skipped by roughly everyone, which is why so many first repositories have a history attributed to nobody in particular. Do it now and it is done for every project you ever start on this machine.
Installing
On macOS, the simplest route is Homebrew (brew install git); Apple's command line developer tools also include a version, and macOS will offer to install them the first time you type git. On Windows, download the official Git for Windows installer and accept the defaults; it brings Git Bash, the terminal from the last topic. On Linux, use your distribution's package manager — sudo apt install git on Debian and Ubuntu, sudo dnf install git on Fedora.
Whichever route you took, one command confirms it:
git --version
A version number comes back. Everything this book teaches works on any reasonably recent Git — the two commands this book leans on, restore in Chapter 4 and switch in Chapter 6, arrived in version 2.23, and anything you install today is far newer than that. If instead you get "command not found", the program is either not installed or not on the list of places your terminal looks; reopening the terminal after installing fixes it more often than you would think.
Telling Git who you are
Git stamps a name and an email onto every commit, permanently. There is no login step, because there is no server to log in to — you are simply telling the program on your machine what to write.
git config --global user.name "Your Name" git config --global user.email "you@example.com"
Use the email you intend to use on GitHub later, so that your commits and your account line up when you get there in Chapter 7. Treat it as public information: it is written into every commit and travels with the project. If that bothers you, GitHub offers a no-reply address for exactly this reason, and Chapter 7 shows where to find it.
What --global means
Git settings live at more than one level. --global writes to your user account's settings and applies to every project on the machine. Leave it off and the setting applies only to the project you are currently standing in — useful when one project needs a different email, and unnecessary for everything else.
Those two levels are as much of the scope story as you need. Git has more levels and more precedence rules, and they belong in the deep dive rather than in your first week.
The default branch name
One more setting, and it prevents a genuinely confusing problem later:
git config --global init.defaultBranch main
Branches are Chapter 6. For now, all you need is that every project starts with one line of work, that line has a name, and the name to use is main. Git's own default is still master — it changes to main in Git 3.0 — while GitHub has created new repositories with main since 2020. Without this setting your machine and GitHub can disagree about what the same thing is called, which is a small annoyance in Chapter 7 and an irritating one afterwards. Some installations set it for you already — Apple's Git on macOS is one — and git config init.defaultBranch tells you which case you are in.
Checking what you have
git config --list
You will see your name, your email, the default branch, and a handful of settings that came with the installation. If something is wrong, run the same git config --global command again with the corrected value — the new value replaces the old one.
git --version answers — installed and reachable from this terminaluser.name and user.email — stamped onto every commit, permanentlyinit.defaultBranch main — your machine and GitHub use the same nameWhat you deliberately have not done
You have not created an account, chosen a graphical client, or connected to anything. Git is a local program and everything in the next five chapters happens on your own machine, with no permission required from anyone. Sandpiper is still just a folder — that changes on the very first page of Chapter 2.
- "
git configlogged me in." There is nothing to log into. It writes two lines to a settings file on your computer. Proving who you are to GitHub is an entirely different mechanism, and it waits until Chapter 7. - "My email should be kept private, so I will use a fake one." A fake address means your commits link to nothing and nobody can reach you about a change. Use a real address you are happy to publish, or GitHub's no-reply address once you have an account.
- "I can fix the name later and old commits will update." Changing the setting affects future commits only. Past commits keep the identity they were made with, and correcting them means rewriting history — which this book does not teach and rarely recommends.
- "Setting
init.defaultBranchrenames branches in projects I already have." It only affects projects created after it is set. Existing repositories keep whatever their branch is already called.
- An unset
user.emailproduces commits that link to nobody, which on any shared project means nobody can ask you what a change was for — the one question commits exist to answer. - Setting the default branch now removes a whole category of confusion in Chapter 7, where a local
mastermeeting a GitHubmainis a classic and baffling first-push failure. - These settings are per-machine, not per-project: five minutes now covers every repository you will ever create here.
Knowledge Check
What does --global change about a git config command?
- It applies the setting to every project on this machine rather than only the current one
- It sends the setting to GitHub so that all of your devices share the same identity
- It makes the setting permanent, where settings without the flag expire after a session
- It applies the setting to every user account on the computer, including other people
You set user.email today, after making three commits yesterday. What happens to those three?
- They keep the identity they were recorded with, because a commit's contents never change
- They are updated automatically the next time you run any Git command
- They are deleted, because Git refuses to keep commits with an unknown author
- They keep the old identity until you push them, at which point it is corrected
Why does this book set init.defaultBranch to main before creating anything?
- So that your machine and GitHub use the same name for the same line of work
- Because Git refuses to create a repository until a default branch name is chosen
- Because a branch named
masteris no longer supported by recent versions of Git - Because the default branch decides where commits are stored on your disk
Your terminal answers command not found when you type git --version. What does that tell you?
- That the program is either not installed or not visible to this terminal session
- That Git is installed but the version is too old to report itself properly
- That Git is installed but has not yet been given a name and email to use
- That the repository in the current folder is damaged and needs to be recreated
You got correct