DevOps.Academy
CoursesCurriculumInterview prepPricing
◔My learningSign in
DevOps.Academy

Free, hands-on DevOps lessons. Made with care in India.

System status

Learn

All coursesLinux roadmapCurriculumInterview questionsCheat sheetsMy learning

Academy

PricingCertificates

Legal

PrivacyTermsContact

devops.rajeev.pro

Git & GitHubLesson 1 of 13
Your progress · 0%
On this page
  1. Why version control exists
  2. Install Git
  3. Tell Git who you are
  4. System, global and local config
  5. Your first repository
↖ Course roadmap
Lesson 1 · The basics

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.

20 min read · beginner · hands-on

By the end of this lesson you'll be able to

0 of 5 complete

Why 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

SystemCommand
Ubuntu / Debiansudo apt update && sudo apt install git
Fedora / RHELsudo dnf install git
macOSbrew install git, or xcode-select --install
WindowsInstall 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 --version
  • gitversion 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 HEAD
  • gitversion 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 log and git blame show 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 control
  • configread or set settings
  • --globalfor your user, in every repo
git config --global --list
  • --listshow every setting
Keep your email private on GitHub

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.

Three levels of Git config. When the same key is set twice, the closer level wins.© Diagram: DevOps Academy

Three levels of Git config. When the same key is set twice, the closer level wins.© Diagram: DevOps Academy
LevelFlagFileApplies to
System--system/etc/gitconfigEvery user on the machine
Global--global~/.gitconfigAll of your repositories
Local(none)<repo>/.git/configThis 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-api
  • cdchange directory
git config user.email "you@company.com"
  • gitversion control
  • configread 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/hello
  • mkdirmake a directory
  • -pcreate missing parents, no error if it exists
  • &&run the next command only if this one worked
  • cdchange directory
git init
  • gitversion control
  • initcreate a repository here
git status
  • statuswhat changed
ls -a
  • lslist 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 .git
  • lslist 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.
  • HEAD says which branch you're on right now.
  • config is 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.

Never delete .git to start over

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.

At work

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.

Beginner Git commands you need to know (WITH EXAMPLES)GitHub · Watch on YouTube ↗
⌘
Mini mission

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.

Input: Global email: you@example.comOutput: ~/work/payments/.git/config
Reveal 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/payments
  • mkdirmake a directory
  • -pcreate missing parents, no error if it exists
  • &&run the next command only if this one worked
  • cdchange directory
git init
  • gitversion control
  • initcreate 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 done

Run each task in your own account or terminal, then tick it off.

Knowledge check

Quick check

Question 1 of 4

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?

Go deeper

Official reading for this lesson

  • GuidePro Git, chapter 1.1, About version controlgit-scm.com ↗
  • GuidePro Git, Installing Gitgit-scm.com ↗
  • GuidePro Git, First-time Git setupgit-scm.com ↗
  • Docsgit config referenceGit docs ↗
  • Docsgit init referenceGit docs ↗
  • DocsSetting your commit email address on GitHubGitHub Docs ↗

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.

Next lesson →Commits: the working folder, the staging area and history