DevOps.Academy
CoursesCurriculumPricing

    Loading…

    ↑ ↓ move · ↵ open · Ctrl K anywhere

    ◔My learningSign in
    Join the waitlist
    DevOps.Academy

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

    Learn

    All coursesLinux roadmapCurriculumMy 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.

    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

    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 control
    • configread or set settings
    • --globalfor your user, in every repo
    • user.namea value the command works on
    • "Your Name"text, kept together by the quotes
    git config --global user.email "you@example.com"
    • gitversion control
    • configread or set settings
    • --globalfor your user, in every repo
    • user.emaila value the command works on
    • "you@example.com"text, kept together by the quotes
    git config --global init.defaultBranch main
    • gitversion control
    • configread or set settings
    • --globalfor your user, in every repo
    • init.defaultBrancha value the command works on
    • maina value the command works on
    git config --global core.editor "nano"
    • gitversion control
    • configread or set settings
    • --globalfor your user, in every repo
    • core.editora value the command works on
    • "nano"text, kept together by the quotes
    git config --global --list
    • gitversion control
    • configread or set settings
    • --globalfor your user, in every repo
    • --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
    • ~/work/client-apia path
    git config user.email "you@company.com"
    • gitversion control
    • configread or set settings
    • user.emaila value the command works on
    • "you@company.com"text, kept together by the quotes
    git config --show-origin user.email
    • gitversion control
    • configread or set settings
    • --show-originwhich file each value comes from
    • user.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/hello
    • mkdirmake a directory
    • -pcreate missing parents, no error if it exists
    • ~/projects/helloa path
    • &&run the next command only if this one worked
    • cdchange directory
    • ~/projects/helloa path
    git init
    • gitversion control
    • initcreate a repository here
    git status
    • gitversion control
    • 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. The files you edit sit next to it in the working directory. The next lesson saves your first change into that history.

    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.

    ⌘
    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
    • ~/work/paymentsa path
    • &&run the next command only if this one worked
    • cdchange directory
    • ~/work/paymentsa path
    git init
    • gitversion control
    • initcreate a repository here
    git config user.email "you@company.com"
    • gitversion control
    • configread or set settings
    • user.emaila value the command works on
    • "you@company.com"text, kept together by the quotes
    git config --show-origin user.email
    • gitversion control
    • configread or set settings
    • --show-originwhich file each value comes from
    • user.emaila value the command works on
    cd ~ && git config --show-origin user.email
    • cdchange directory
    • ~your home directory
    • &&run the next command only if this one worked
    • gitversion control
    • configread or set settings
    • --show-originwhich file each value comes from
    • user.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 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.

    Next lesson →Repositories