Skip to content

Latest commit

 

History

History
359 lines (241 loc) · 8.81 KB

File metadata and controls

359 lines (241 loc) · 8.81 KB

Git Basics

Learning Goals

  • Understand what Git is and why it's used
  • Initialize a new Git repository
  • Track changes with git add and git commit
  • Inspect history with git log
  • See differences with git diff and git status

Introduction

Before we jump into GitHub, we need to understand Git — the version control system that GitHub is built on.

Git tracks changes to your files over time, letting you:

  • Go back to any previous version of your code
  • See exactly what changed, when, and by whom
  • Work on different features in parallel without breaking things

Think of Git as an "undo history" for your entire project — but much more powerful.

Key concepts

  • Repository (repo): A folder that Git is tracking. It contains your files and the entire history of changes.
  • Commit: A snapshot of your files at a specific point in time. Each commit has a message describing what changed.
  • Staging area (index): A "prep area" where you choose which changes to include in your next commit.

The basic workflow looks like this:

Working Directory  →  Staging Area  →  Repository
   (edit files)       (git add)       (git commit)

Terminal or VS Code?

Every Git task in these exercises can be done from the terminal or from VS Code's built-in Source Control panel. We show both approaches throughout.

If you prefer VS Code, open the Source Control view by clicking the branch icon in the left sidebar (or press Ctrl+Shift+G / Cmd+Shift+G).

VS Code Source Control icon in the sidebar

Exercise

Overview

  • Verify your Git installation and configure your identity
  • Create a new folder and initialize it as a Git repository
  • Create a file, stage it, and make your first commit
  • Make more changes and commits to build up a history
  • Inspect the history with git log and see differences with git diff

Step by step instructions

Step by step

Task 1: Verify Git is installed

Open your terminal and run:

git --version

Expected output (version may vary):

git version 2.43.0

💡 If you get "command not found", install Git from git-scm.com/downloads.

Task 2: Configure your identity

Git needs to know who you are so it can label your commits. Set your name and email:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

💡 Use the same email you'll use for your GitHub account — this links your commits to your GitHub profile later.

Verify the configuration:

git config --global --list

Task 3: Create a project folder and initialize a repo

mkdir git-basics-exercise
cd git-basics-exercise
git init

Expected output:

Initialized empty Git repository in /path/to/git-basics-exercise/.git/
💻 VS Code alternative
  1. Create a new folder called git-basics-exercise wherever you like
  2. Open it in VS Code: File → Open Folder
  3. Open the Source Control panel (Ctrl+Shift+G / Cmd+Shift+G)
  4. Click Initialize Repository

VS Code Initialize Repository button

Check the status of your new repo:

git status

Expected output:

On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)

💡 The .git/ folder is where Git stores all its tracking data. Don't delete it!

Task 4: Create a file and make your first commit

Create a new file:

echo "# My Git Exercise" > README.md

Check what Git sees:

git status

Expected output:

On branch main
No commits yet
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        README.md

nothing added to commit but untracked files present (use "git add" to track)

Stage the file (add it to the staging area):

git add README.md

Check the status again:

git status

Expected output:

On branch main
No commits yet
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   README.md

💡 Notice the file moved from "Untracked files" to "Changes to be committed." The staging area lets you choose exactly which changes go into each commit.

Now commit the file with a descriptive message:

git commit -m "Add README with project title"
💻 VS Code alternative
  1. In the Source Control panel, you'll see README.md listed under Changes
  2. Hover over the file and click the + icon to stage it — it moves to Staged Changes
  3. Type your commit message in the text box at the top: Add README with project title
  4. Click the ✓ Commit button (or press Ctrl+Enter / Cmd+Enter)

VS Code stage and commit workflow

💡 In VS Code, the Source Control panel is the visual equivalent of git status. Files under Changes are unstaged; files under Staged Changes are ready to commit.

Expected output:

[main (root-commit) abc1234] Add README with project title
 1 file changed, 1 insertion(+)
 create mode 100644 README.md

Task 5: Make more changes and commits

Add some content to the README:

echo "This is my first Git project." >> README.md
echo "I'm learning the basics of version control." >> README.md

Before committing, see what changed:

git diff

Expected output (approximately):

diff --git a/README.md b/README.md
index 1234567..abcdefg 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,3 @@
 # My Git Exercise
+This is my first Git project.
+I'm learning the basics of version control.

💡 git diff shows unstaged changes — the difference between your working directory and the staging area. Lines prefixed with + are additions, - are deletions.

Stage and commit:

git add README.md
git commit -m "Add description to README"
💻 VS Code alternative

In VS Code, you can see the diff by clicking on a changed file in the Source Control panel — it opens a side-by-side comparison. Stage with +, type your message, and click ✓ Commit.

VS Code side-by-side diff view

Now create another file and commit it:

echo "- Learn git init" > todo.txt
echo "- Learn git add and commit" >> todo.txt
echo "- Learn git log" >> todo.txt
git add todo.txt
git commit -m "Add todo list for learning goals"

Task 6: Inspect the history

View the commit log:

git log
💻 VS Code alternative

Open the Source Control panel and click the GRAPH bar (Timeline) at the bottom of the sidebar to see your commit history.

💡 For a richer Git history view, consider installing the Git Graph extension.

VS Code Timeline view showing commit history

Expected output (your hashes and dates will differ):

commit 9f8e7d6... (HEAD -> main)
Author: Your Name <your.email@example.com>
Date:   Tue Mar 4 10:30:00 2026 +0100

    Add todo list for learning goals

commit 5a4b3c2...
Author: Your Name <your.email@example.com>
Date:   Tue Mar 4 10:25:00 2026 +0100

    Add description to README

commit 1a2b3c4...
Author: Your Name <your.email@example.com>
Date:   Tue Mar 4 10:20:00 2026 +0100

    Add README with project title

For a compact view:

git log --oneline

Expected output:

9f8e7d6 Add todo list for learning goals
5a4b3c2 Add description to README
1a2b3c4 Add README with project title

💡 Each commit has a unique hash (like 9f8e7d6). This is its identifier — you can use it to refer to any specific commit.

Task 7: Explore more Git commands

Try these additional commands to deepen your understanding:

Check which files Git is tracking:

git ls-files

See a summary of changes in each commit:

git log --stat

View the changes introduced by the most recent commit:

git show

Summary

You've learned the core Git workflow: edit → stage → commit. You can now:

  • Initialize a repository with git init
  • Track changes with git add and git commit
  • Inspect history with git log
  • See what changed with git diff and git status

These are the building blocks of everything we'll do next. In the next exercise, you'll create a repository on GitHub and learn how to push and pull changes.

Head over to Working with GitHub to continue.