Version control, and setting Git up once
Why every team keeps code in version control, how to install Git, set your identity at the right level, and create your first repository.
By the end of this lesson you'll be able to
0 of 5 completeWhy version control exists
Picture a team without it. The code lives in a shared folder. Someone copies it to
app-final-v2-fixed.zip before a big change. Two people edit the same file and one of them
silently overwrites the other. Production breaks on Friday and nobody can say what changed
since Tuesday.
A version control system fixes all of that. It records every change as a snapshot with who made it, when, and why. That gives you four things you'll use every single day:
- History. See exactly what changed, line by line, and who changed it.
- Undo. Go back to any earlier version of a file or of the whole project.
- Parallel work. Everyone works on their own branch and merges when ready, instead of editing the same copy.
- A trigger for automation. Every CI/CD pipeline starts with "a commit was pushed". No version control, no pipeline.
Git is the version control system almost everyone uses now. It's distributed: every clone is a full copy of the history, so you can commit, branch and look at history with no network at all. GitHub, GitLab and Bitbucket are places to host and share Git repositories; Git itself runs on your machine.
How it works, in one picture. Every time you save a version (a commit), Git stores a snapshot of the whole project plus a note: who, when, why, and which commit came before it. Those notes link the commits into a chain, so Git can walk back through every version ever saved. A branch is just a name that points at one commit in that chain. Everything else in this course builds on that one idea.
In this lesson you'll install Git, tell it who you are, and create your first repository.
Install Git
| System | Command |
|---|---|
| Ubuntu / Debian | sudo apt update && sudo apt install git |
| Fedora / RHEL | sudo dnf install git |
| macOS | brew install git, or xcode-select --install |
| Windows | Install Git for Windows from git-scm.com, or use it inside WSL |
$ git --version git version 2.x.x # any recent 2.x is fine for this course
git --versiongitversion control--versionprint the version
Tell Git who you are
Why it matters. Every commit permanently stores the author's name and email. Here is a real
commit, opened up with git cat-file:
$ git cat-file -p HEAD tree 89807efe1600c4786b7f695c30a8d13438baf314 author Rajeev Kumar <you@example.com> <timestamp> committer Rajeev Kumar <you@example.com> <timestamp> Add README
git cat-file -p HEADgitversion control
Three things follow from that:
- It's permanent. Fixing the email later means rewriting history, which is painful once others have the commits.
- GitHub links commits to you by email. If the commit email matches one on your GitHub account, the commit shows your avatar and counts on your profile. If it doesn't, the commit looks like it came from a stranger.
- Teammates use it.
git logandgit blameshow who changed a line, so they know whom to ask.
So set your name and email once, before your first commit. If you don't, Git guesses from your
machine's username and hostname, and you end up with commits from rajeev@laptop.localdomain
in the history forever.
$ git config --global user.name "Your Name" $ git config --global user.email "you@example.com" $ git config --global init.defaultBranch main # new repos start on "main" $ git config --global core.editor "nano" # the editor Git opens for commit messages $ git config --global --list user.name=Your Name user.email=you@example.com init.defaultBranch=main core.editor=nano
git config --global user.name "Your Name"gitversion controlconfigread or set settings--globalfor your user, in every repo
git config --global --list--listshow every setting
Commit emails are public in every repository you push to. GitHub gives each account a noreply
address you can use as user.email instead. The resources at the end of this lesson show where to
find yours.
System, global and local config
Git reads settings from three places, in this order. The last one wins, so the setting closest to the repository is the one that counts.
| Level | Flag | File | Applies to |
|---|---|---|---|
| System | --system | /etc/gitconfig | Every user on the machine |
| Global | --global | ~/.gitconfig | All of your repositories |
| Local | (none) | <repo>/.git/config | This one repository only |
The everyday use: your global email is personal, and one client or work repository needs your work address. Set it locally in that repository and nothing else changes.
$ cd ~/work/client-api $ git config user.email "you@company.com" # no flag = local, this repo only $ git config --show-origin user.email # which file did this value come from? file:.git/config you@company.com
cd ~/work/client-apicdchange directory
git config user.email "you@company.com"gitversion controlconfigread or set settings
git config --show-origin user.email--show-originwhich file each value comes from
--show-origin is the fastest way to answer "why is Git using that email?"
Your first repository
$ mkdir -p ~/projects/hello && cd ~/projects/hello $ git init Initialized empty Git repository in /home/rajeev/projects/hello/.git/ $ git status On branch main No commits yet nothing to commit (create/copy files and use "git add" to track) $ ls -a . .. .git
mkdir -p ~/projects/hello && cd ~/projects/hellomkdirmake a directory-pcreate missing parents, no error if it exists&&run the next command only if this one workedcdchange directory
git initgitversion controlinitcreate a repository here
git statusstatuswhat changed
ls -alslist files-ainclude hidden files (names starting with .)
That .git folder is the repository: every commit, every branch and the repo's own config
live inside it. Here's what git init created:
$ ls -F .git HEAD config description hooks/ info/ objects/ refs/
ls -F .gitlslist files
objects/holds the content: every version of every file, and every commit.refs/holds the names: branches and tags, each pointing at a commit.HEADsays which branch you're on right now.configis this repository's own settings (the "local" level from the section above).
You never edit these files by hand; Git commands do it for you. The files you do edit sit next
to .git, in the working directory. Git watches them, but nothing is saved into history
until you commit. The next lesson shows how a change travels from the working directory into a
commit.
Deleting .git deletes the whole history of that project. If something looks broken, there is
almost always a Git command that fixes it, and you'll learn them in Undoing changes.
Check git config user.email in a new work repository before your first commit. Many companies
only accept commits from company addresses, and some block pushes that use anything else.
Use your work email in one repo only
You use a personal email everywhere. A new work project, ~/work/payments, must use you@company.com, and nothing else on your machine should change. Create the repo, set the email at the right level, and prove both values.
~/work/payments/.git/configReveal one safe solution
$ mkdir -p ~/work/payments && cd ~/work/payments $ git init $ git config user.email "you@company.com" $ git config --show-origin user.email file:.git/config you@company.com $ cd ~ && git config --show-origin user.email file:/home/rajeev/.gitconfig you@example.com
mkdir -p ~/work/payments && cd ~/work/paymentsmkdirmake a directory-pcreate missing parents, no error if it exists&&run the next command only if this one workedcdchange directory
git initgitversion controlinitcreate a repository here
git config user.email "you@company.com"configread or set settings
git config --show-origin user.email--show-originwhich file each value comes from
cd ~ && git config --show-origin user.email~your home directory
The local value lives in ~/work/payments/.git/config and only applies there. Outside the
repository, Git falls back to your global ~/.gitconfig.
Try it yourself
0 of 5 steps doneRun each task in your own account or terminal, then tick it off.
Quick check
Your global email is personal, but one work repo has a local user.email set to your work address. Which one do commits in that repo use?
Official reading for this lesson
Or get every quiz answer right and it completes itself.
Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.