Git Intro
Git is a version control system: a tool that records snapshots of a project over time, so you can see what changed, who changed it, why, and go back to any earlier state. It was written by Linus Torvalds in 2005 to manage the Linux kernel, and today almost every software team uses it.
Without version control, people end up with folders like report-final.docx, report-final-2.docx and report-really-final.docx. Git replaces that mess with a single folder plus a hidden history database.
What Git gives you
| Feature | What it means in practice |
|---|---|
| History | Every saved snapshot (a commit) has an author, a date, a message and a unique ID |
| Undo | Restore one file or the whole project to any earlier commit |
| Branches | Work on a feature in isolation, then merge it back when it is ready |
| Collaboration | Many people work on copies of the same repository and exchange commits |
| Distributed | Every copy contains the full history, so you can work offline |
| Integrity | Content is identified by a cryptographic hash, so silent corruption is detected |
Git vs GitHub
These names are often confused:
- Git is the program that runs on your computer and stores history in a
.gitfolder. - GitHub (and GitLab, Bitbucket, Gitea, …) is a website that hosts Git repositories and adds features such as pull requests, issues and code review.
You can use Git without GitHub. In this course, the "GitHub" lessons simulate the server with a repository in a local folder, so every push, pull and clone really runs.
A first look
Here is a whole Git session: create a project, save a snapshot, and look at the history. Don't worry about the details yet; each command gets its own lesson.
mkdir recipes && cd recipes
git init
printf 'Pancakes\n- 2 eggs\n- 1 cup flour\n' > pancakes.txt
git add pancakes.txt
git commit -m "Add pancake recipe"
git log --onelineInitialized empty Git repository in /home/learner/recipes/.git/
[main (root-commit) 6a32645] Add pancake recipe
1 file changed, 3 insertions(+)
create mode 100644 pancakes.txt
6a32645 Add pancake recipe
git init created an empty repository, git add chose the file to include, and git commit saved the snapshot. The short code at the start of the log line is the beginning of the commit's ID.
How the examples in this course run
Every example is a real Bash script, run by Git 2.54 on Linux in a fresh, empty home directory. Git is already configured with the name Ada Lovelace and every commit gets the same fixed date, so the commit IDs you see are reproducible. On the site, pressing Run shows the saved output; in your own terminal you will see different IDs and dates, which is expected.
git --version
git config user.name
git config user.emailgit version 2.54.0
Ada Lovelace
ada@example.com
Snapshots, not differences
Some older systems store a list of changes per file. Git instead thinks of each commit as a snapshot of every tracked file. Files that did not change are not copied again; Git just points to the version it already stored. That makes switching between versions and branches very fast.
mkdir notes && cd notes
git init -q
echo "first idea" > ideas.txt
git add ideas.txt
git commit -q -m "First idea"
echo "second idea" >> ideas.txt
git commit -q -am "Second idea"
git log --oneline
echo "--- the file one commit ago:"
git show HEAD~1:ideas.txt6a9b8b0 Second idea
cb8e0a5 First idea
--- the file one commit ago:
first idea
git show HEAD~1:ideas.txt printed the file exactly as it was in the previous snapshot, even though the working copy has two lines now.
Note: The
-q(quiet) flag only hides informational messages. Many examples use it so the output focuses on the command being taught.
Test yourself
What is the difference between Git and GitHub?
- They are two names for the same program
- Git is the version control tool; GitHub is a service that hosts Git repositories
- GitHub is the command-line version of Git
- Git only works when connected to GitHub
What does Git store in each commit?
- Only the lines that changed
- A snapshot of all tracked files (reusing unchanged ones)
- A zip file of the whole disk
- Just the commit message
Why can you work with Git while offline?
- Git caches the website
- Every clone contains the full history locally
- Git queues commands until you reconnect
- You can't