Introduction to Git
Git is a version control system: it records the history of a project as a series of snapshots, so you can see what changed, when and why, go back to any earlier state, and work on several ideas at once without them getting in each other's way. It is the tool almost every software team uses to share code.
The problem Git solves
Without version control, people keep history by copying files: report-final.docx, report-final-v2.docx, report-final-v2-REALLY.docx. That breaks down quickly. You can't tell what changed between copies, two people editing the same file overwrite each other, and a mistake you only notice a week later is hard to undo.
Git replaces all of that with one folder and a hidden database of its history. Each time you reach a sensible point, you save a commit — a snapshot of every file in the project plus a short message describing the change. Commits link to the one before, forming a timeline you can browse, compare and rewind.
What you can do with Git
- Track history — see every change ever made, who made it and the reason they gave.
- Undo safely — throw away a bad edit, restore a deleted file, or reverse a commit that broke something.
- Branch — start an experiment on its own line of history, then merge it in or throw it away.
- Collaborate — many people work on copies of the same project and combine their changes, with Git pointing out any overlaps.
- Find bugs — search history for the exact commit that introduced a problem.
A taste of Git
Here is a whole first session: create a project, save a file in it, and look at the history. Don't worry about the details yet — every command gets its own lesson.
mkdir recipes cd recipes git init
Initialized empty Git repository in /home/sam/recipes/.git/
echo "Pancakes: flour, milk, eggs" > pancakes.txt git add pancakes.txt git commit -m "Add pancake recipe"
[main (root-commit) c4f5e47] Add pancake recipe 1 file changed, 1 insertion(+) create mode 100644 pancakes.txt
git log --oneline
c4f5e47 (HEAD -> main) Add pancake recipe
The line beginning with a string of letters and digits is the commit's ID, called its hash. Every commit gets a unique one, and you use it to refer to that point in history.
Distributed version control
Older systems such as Subversion kept the history on one central server, and you needed a connection to it for almost everything. Git is distributed: every copy of a project — called a repository, or repo — contains the complete history. You can commit, browse the log, create branches and compare versions offline. You only connect to another machine when you want to exchange commits with it.
Git is not GitHub
People often mix these up:
| What it is | |
|---|---|
| Git | A free, open-source program that runs on your computer and manages a repository's history. |
| GitHub, GitLab, Bitbucket, Codeberg… | Websites that host Git repositories online and add collaboration features: pull requests, code review, issue tracking, automation. |
You can use Git entirely on your own without any account. A hosting service becomes useful when you want a backup off your machine or want to work with other people. This course uses GitHub for its examples because it's the most common, but the Git commands are the same everywhere.
A little history
Linus Torvalds wrote Git in 2005 to manage the source code of the Linux kernel, after the tool the project had been using stopped being available for free. The design goals were speed, support for thousands of contributors working in parallel, and strong protection against history being corrupted. Junio Hamano has maintained it since later that year. Today it is used for everything from tiny personal scripts to the largest codebases in the world — and for things that aren't code at all, like documentation, books and configuration.
Key words
| Term | Meaning |
|---|---|
| Repository | A project folder together with its full Git history. |
| Commit | A saved snapshot of the project, with an author, a date and a message. |
| Hash | The unique ID of a commit, such as c4f5e47 (shortened). |
| Branch | An independent line of development. |
| Remote | Another copy of the repository, usually on a server, that you exchange commits with. |
How to use this course
Git is learned by doing. Every example was run with a real copy of Git and the output shown is exactly what it printed; you'll get the same messages, though the commit hashes and dates will be your own. Install Git in the next lesson and type the commands along with each lesson. The examples follow Sam, who is keeping a collection of recipes under version control.
Try it yourself
Find a project on GitHub you use or have heard of, open its Commits page, and read a few messages. Notice how each one describes a single, focused change — that's the history you'll learn to build.