Introduction to Version Control with Git
From the quiz curriculum
Introduction to Version Control with Git
TL;DR
Version control helps you track changes to your files over time, making collaboration and error recovery much easier. Git is a popular system that lets you save "snapshots" of your project, called commits, and easily switch between different versions. Using Git prevents lost work, simplifies teamwork, and provides a full history of your project's evolution.
1. The Mental Model
Think of version control like having an unlimited undo button and a time machine for your entire project. Every time you save a significant change, you're taking a snapshot. You can always go back to any previous snapshot, or even branch off to try new ideas without messing up your main work.
2. The Core Material
When you're working on a project, especially with others, things can get messy. You might accidentally delete something important, want to revert to an earlier version, or need to combine work from multiple people. That's where Git comes in.
Git helps you manage these scenarios by tracking every change you make to your files. It doesn't just save the whole file again; it cleverly saves the differences between versions, which is very efficient.
Initializing a Repository

Photo by Daniil Komov on Pexels
To start using Git for a project, you first need to "initialize" a Git repository in your project's folder. This creates a hidden .git directory where Git stores all its tracking information.
You do this in your terminal:
git init
Staging and Committing Changes

Photo by Ann H on Pexels
Git has a unique workflow for saving changes. It involves two main steps:
- Staging: You select which changes you want to include in your next save. This is like preparing a set of files for a specific "snapshot."
bash git add <filename> # To stage a specific file git add . # To stage all changes in the current directory - Committing: You permanently record the staged changes into the project's history as a "commit." Each commit gets a unique ID and a message describing what changes were made.
bash git commit -m "Your descriptive commit message here"
Viewing History

Photo by Katerina Holmes on Pexels
You can always look back at the history of your commits.
git log
This command shows you each commit, its unique ID (hash), author, date, and commit message.
Understanding the Git Workflow

Photo by Kevin Ku on Pexels
Here's a simple flow of how you'll typically use Git:
graph LR
A["Start Project / Existing Repo"] --> B["Make Changes to Files"];
B --> C{"Are Changes Ready to Save?"};
C -- Yes --> D["Stage Changes (git add)"];
D --> E["Commit Changes (git commit)"];
E --> F["New Version Saved!"];
C -- No --> B;
Undoing Changes
One of Git's superpowers is the ability to undo.
* Discarding local changes (before staging): If you've made changes to a file but haven't staged it yet and want to throw them away, you can:
bash
git restore <filename>
* Unstaging changes (before committing): If you've staged a file but decide you don't want it in the next commit:
bash
git restore --staged <filename>
* Reverting a commit (after committing): If you've committed something but later realize it was a mistake and want to undo that specific commit, creating a new commit that undoes the old one:
bash
git revert <commit_hash>
3. Worked Example
Let's say you're writing a simple recipe.
-
Start a new project folder and initialize Git:
bash mkdir recipe-project cd recipe-project git init
You'll seeInitialized empty Git repository in /path/to/recipe-project/.git/. -
Create your first file:
Create a file namedcookie_recipe.txtand add some content:
```
Chocolate Chip CookiesIngredients:
- Flour
- Sugar
``` -
Stage and commit the first version:
bash git add cookie_recipe.txt git commit -m "Add basic ingredients for chocolate chip cookies"
Git will output something like[main (root-commit) 7a1b2c3] Add basic ingredients.... -
Make some changes:
Editcookie_recipe.txtto add more ingredients and instructions:
```
Chocolate Chip CookiesIngredients:
- Flour
- Sugar
- Butter
- Chocolate ChipsInstructions:
1. Preheat oven.
2. Mix ingredients.
``` -
Check status and commit the updated version:
bash git status
You'll seecookie_recipe.txtlisted under "Changes not staged for commit."bash git add cookie_recipe.txt git commit -m "Add more ingredients and basic instructions" -
View the history:
bash git log
You'll see two commits, with the most recent one at the top. Each commit has a unique identifier (a long string of letters and numbers).
4. Key Takeaways
- Git tracks changes in your project files, providing a full history of your work.
git initstarts a new Git repository in your project folder.git addstages changes, preparing them for your next commit.git commit -m "message"saves staged changes as a permanent snapshot with a descriptive message.git logshows you the history of all your commits.- You can undo changes at various stages using
git restoreorgit revert.
Common Mistakes to Avoid:
- Not committing frequently: Commit small, logical changes often. Don't wait until you've done a huge chunk of work.
- Vague commit messages: "Changes" or "Fixes" aren't helpful. Be specific about what you did and why.
- Forgetting git add: You need to add files to the staging area before you can commit them.
- Committing sensitive information: Never commit passwords, API keys, or other private data directly into your repository.
5. Now Try It
Create a new folder called my-story. Inside it, initialize a Git repository. Create a file called story.txt and write the first sentence of a story. Stage and commit it with a clear message. Then, add a second sentence to story.txt, stage and commit that change. Finally, use git log to see both of your commits. Success looks like seeing two distinct commits in your log, each with your custom message.
Frequently asked about Introduction to Version Control with Git
More from quiz
Get the full quiz curriculum
Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.
Create Free Account