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
.gitfolder 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 --versionin 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:
git --versionFirst-time setup
Every commit records who made it, so tell Git your name and email once:
git config --global user.name "Sam Lee"
git config --global user.email "sam@example.com"
git config --global init.defaultBranch main--globalsaves 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:
git help commit
git commit -hgit 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.nameanduser.emailonce withgit config --global.
Git Examples
Each example shows the code, the output it produces and what each part does.
Check that Git is installed
git --versiongit version 2.46.0Line-by-line explanation
- Line 1Any recent version works for this course. If you see "command not found", Git isn't installed yet or the terminal needs restarting.
Set your identity
git config --global user.name "Sam Lee"
git config --global user.email "sam@example.com"
git config --global init.defaultBranch main
git config --global --listuser.name=Sam Lee
user.email=sam@example.com
init.defaultbranch=mainLine-by-line explanation
- Lines 1 to 2Saved once, used in every commit you make on this computer.
- Line 3New repositories will start with a branch called main.
- Line 4Shows the global settings so you can check them.
Quick help for a command
git status -husage: 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
- 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
git config --global user.name Sam Leegit config --global user.name "Sam Lee"Without quotes, the space splits your name, and Git complains or saves only "Sam".
Leaving out --global
git config user.name "Sam Lee"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.
Complete the command that sets your name for every repository.
git user.name "Ava Patel"
What is a commit?
Why can you commit without an internet connection?
What does git --version show if Git is installed?
Which command shows the version of Git you have installed?
Git Quiz
5 questions to check what you learned.
What kind of tool is Git?
- A version control system
- A code editor
- A programming language
- A web browser
What is a repository?
- A Git command
- A website
- A project folder that Git is tracking
- A single file
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
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
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