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.
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
Every commit records a name and an email. Set them once, before your first commit, or Git
will guess from your machine's username and hostname, and you'll 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 repouser.namea value the command works on"Your Name"text, kept together by the quotes
git config --global user.email "you@example.com"gitversion controlconfigread or set settings--globalfor your user, in every repouser.emaila value the command works on"you@example.com"text, kept together by the quotes
git config --global init.defaultBranch maingitversion controlconfigread or set settings--globalfor your user, in every repoinit.defaultBrancha value the command works onmaina value the command works on
git config --global core.editor "nano"gitversion controlconfigread or set settings--globalfor your user, in every repocore.editora value the command works on"nano"text, kept together by the quotes
git config --global --listgitversion controlconfigread or set settings--globalfor your user, in every repo--listshow every setting
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~/work/client-apia path
git config user.email "you@company.com"gitversion controlconfigread or set settingsuser.emaila value the command works on"you@company.com"text, kept together by the quotes
git config --show-origin user.emailgitversion controlconfigread or set settings--show-originwhich file each value comes fromuser.emaila value the command works on
--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~/projects/helloa path&&run the next command only if this one workedcdchange directory~/projects/helloa path
git initgitversion controlinitcreate a repository here
git statusgitversion controlstatuswhat 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. The files you edit sit next to it in the working directory. The next lesson
saves your first change into that history.
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~/work/paymentsa path&&run the next command only if this one workedcdchange directory~/work/paymentsa path
git initgitversion controlinitcreate a repository here
git config user.email "you@company.com"gitversion controlconfigread or set settingsuser.emaila value the command works on"you@company.com"text, kept together by the quotes
git config --show-origin user.emailgitversion controlconfigread or set settings--show-originwhich file each value comes fromuser.emaila value the command works on
cd ~ && git config --show-origin user.emailcdchange directory~your home directory&&run the next command only if this one workedgitversion controlconfigread or set settings--show-originwhich file each value comes fromuser.emaila value the command works on
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.