Git Introduction

What version control is, why Git matters, how to install it and how to set your name and email.

Lesson 1 of 30Beginner17 min read

Have you ever saved files called essay-final.docx, essay-final-2.docx and essay-final-REALLY.docx? That's version control done by hand, and it gets messy fast. Git does the same job properly: it keeps a complete, organised history of every change to every file in your project.

With Git you can see exactly what changed between any two versions, who changed it and why. You can go back to how things were yesterday, try a risky idea without fear, and work alongside other people on the same files. It's one of the first tools every developer learns.

In this lesson you'll learn the key ideas behind Git, install it, and do the one-time setup that every new Git user needs.

How Git thinks

  • A repository (or repo) is a project folder that Git is tracking. Git stores its data in a hidden .git folder inside it.
  • A commit is a saved snapshot of your files, with a message describing what changed, the author and the date.
  • The history is the chain of commits, each pointing to the one before it.
  • A branch is a separate line of commits, so you can work on something new without disturbing the main version.

Git is distributed: every copy of a repository contains the full history, so you can commit, browse history and create branches without any internet connection.

Installing Git

  • Windows: download Git for Windows from git-scm.com and run the installer. The default options are fine. It also installs Git Bash, a terminal for running Git commands.
  • macOS: run git --version in Terminal. If Git isn't installed, macOS offers to install the developer tools, which include it. You can also use Homebrew: brew install git.
  • Linux: use your package manager, for example sudo apt install git.

Check the installation:

Terminal
git --version

First-time setup

Every commit records who made it, so tell Git your name and email once:

Terminal
git config --global user.name "Sam Lee"
git config --global user.email "sam@example.com"
git config --global init.defaultBranch main
  • --global saves the setting for every repository on this computer.
  • Use the same email you'll use on GitHub, so your commits are linked to your account.
  • The third line names the first branch of new repositories main, which is today's standard.

Run git config --list to see your settings.

Getting help

Every Git command has built-in help:

Terminal
git help commit
git commit -h

git help <command> opens the full manual page, and -h shows a short summary in the terminal. You don't need to memorise options; even experienced developers look them up.

Git Syntax

Setup commands

git --version
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --list
git help <command>

Git Summary

  • Git is a version control system that records snapshots of your project called commits.
  • A repository is a folder Git tracks; its history is a chain of commits.
  • Git works offline because every copy has the full history.
  • Install Git from git-scm.com and check it with git --version.
  • Set user.name and user.email once with git config --global.

Git Examples

Each example shows the code, the output it produces and what each part does.

Example 1

Check that Git is installed

Terminal
git --version
Output
git version 2.46.0
Line-by-line explanation
  1. Line 1Any recent version works for this course. If you see "command not found", Git isn't installed yet or the terminal needs restarting.
Example 2

Set your identity

Terminal
git config --global user.name "Sam Lee"
git config --global user.email "sam@example.com"
git config --global init.defaultBranch main
git config --global --list
Output
user.name=Sam Lee
user.email=sam@example.com
init.defaultbranch=main
Line-by-line explanation
  1. Lines 1 to 2Saved once, used in every commit you make on this computer.
  2. Line 3New repositories will start with a branch called main.
  3. Line 4Shows the global settings so you can check them.
Example 3

Quick help for a command

Terminal
git status -h
Output
usage: git status [<options>] [--] [<pathspec>...]

    -v, --[no-]verbose    be verbose
    -s, --[no-]short      show status concisely
    -b, --[no-]branch     show branch information
    ...
Line-by-line explanation
  1. Line 1-h prints a short list of options in the terminal. The output is trimmed here.

Common Git Mistakes

Forgetting the quotes around your name

Don't
git config --global user.name Sam Lee
Do
git config --global user.name "Sam Lee"

Without quotes, the space splits your name, and Git complains or saves only "Sam".

Leaving out --global

Don't
git config user.name "Sam Lee"
Do
git config --global user.name "Sam Lee"

Without --global, the setting only applies to the repository you're in, or fails if you aren't in one.

Git in Real Projects

Git at work

On a typical team, every change to the code arrives as a commit with a clear message. When a bug appears, developers look through the history to find which commit introduced it, and can undo just that change. Tools like GitHub, GitLab and Bitbucket, and the deployment systems that put websites live, are all built on top of Git.

Git FAQ

What is the difference between Git and GitHub?

Git is the version control program that runs on your computer and records the history of your project. GitHub is a website that stores Git repositories online and adds collaboration features such as issues, pull requests and code review. You can use Git without GitHub, and other services such as GitLab and Bitbucket host Git repositories too.

How do I check which version of Git I have installed?

Run git --version in a terminal. It prints something like git version 2.43.0. If you get "command not found" instead, Git either isn't installed or your terminal can't find it, so reinstall it or open a new terminal window after installing.

How do I change my Git name or email after setting them?

Run the same commands again with the new values, for example git config --global user.email "new@example.com". The new setting replaces the old one. It only affects commits you make from now on; existing commits keep the name and email they were made with.

Does my Git email have to match my GitHub account?

Git accepts any email, but GitHub uses it to link commits to your account. If the commit email isn't one of the addresses on your GitHub account, your commits won't show your profile picture or count on your contribution graph. GitHub also gives you a private "noreply" address you can use if you don't want your real email in public history.

Is Git free to use for commercial projects?

Yes. Git is free, open-source software released under the GNU General Public License version 2, and you can use it for any project, including paid and closed-source work. The licence covers Git itself, not the code you store in it, so your own files stay yours.

Git Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Fill in the blankseasy

Complete the command that sets your name for every repository.

git   user.name "Ava Patel"
Multiple choiceeasy

What is a commit?

Multiple choicemedium

Why can you commit without an internet connection?

Predict the outputeasy

What does git --version show if Git is installed?

Multiple choicemedium

Which command shows the version of Git you have installed?

Git Quiz

5 questions to check what you learned.

  1. What kind of tool is Git?

    • A version control system
    • A code editor
    • A programming language
    • A web browser
  2. What is a repository?

    • A Git command
    • A website
    • A project folder that Git is tracking
    • A single file
  3. What's the relationship between Git and GitHub?

    • They're the same thing
    • Git tracks changes locally; GitHub hosts Git repositories online
    • GitHub is required to use Git
    • Git is a feature of GitHub
  4. Why should you set user.email before your first commit?

    • Git emails you a copy of each commit
    • It's needed to install Git
    • It sets your password
    • Every commit records its author's name and email
  5. Does Git need an internet connection to make commits?

    • Yes, always
    • Only for the first commit
    • No, commits are saved on your own computer
    • Only on Windows