Introduction to Version Control with Git

SA
StudyAI Editorial
Reviewed by StudyAI tutors
· Published Updated

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

Close-up of software development tools displaying code and version control systems on a computer monitor.
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

Scrabble tiles spelling 'A New Chapter' on wooden grid, symbolizing new beginnings.
Photo by Ann H on Pexels

Git has a unique workflow for saving changes. It involves two main steps:

  1. 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
  2. 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

Side view of ethnic teenage writing words on whiteboard and studying biology in classroom
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

Eyeglasses reflecting computer code on a monitor, ideal for technology and programming themes.
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.

  1. Start a new project folder and initialize Git:
    bash mkdir recipe-project cd recipe-project git init
    You'll see Initialized empty Git repository in /path/to/recipe-project/.git/.

  2. Create your first file:
    Create a file named cookie_recipe.txt and add some content:
    ```
    Chocolate Chip Cookies

    Ingredients:
    - Flour
    - Sugar
    ```

  3. 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....

  4. Make some changes:
    Edit cookie_recipe.txt to add more ingredients and instructions:
    ```
    Chocolate Chip Cookies

    Ingredients:
    - Flour
    - Sugar
    - Butter
    - Chocolate Chips

    Instructions:
    1. Preheat oven.
    2. Mix ingredients.
    ```

  5. Check status and commit the updated version:
    bash git status
    You'll see cookie_recipe.txt listed under "Changes not staged for commit."

    bash git add cookie_recipe.txt git commit -m "Add more ingredients and basic instructions"

  6. 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 init starts a new Git repository in your project folder.
  • git add stages changes, preparing them for your next commit.
  • git commit -m "message" saves staged changes as a permanent snapshot with a descriptive message.
  • git log shows you the history of all your commits.
  • You can undo changes at various stages using git restore or git 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

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. Read the full notes above for the details.

Introduction to Version Control with Git is a core topic in quiz. Most exam papers test it via a mix of definitions, worked examples, and applied problems. The notes above cover the high-yield sub-topics, common pitfalls, and the kind of questions examiners typically set.

Yes. Every note in the StudyAI Campus Hub is free to read. Create a free account if you want to clone the full plan, generate your own notes from your textbook, or get AI-powered practice quizzes and flashcards.

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