Git Basics & GitHub Workflow: Hands-On Practice Session Learn Git from zero โ€” then grill yourself with 10 quizzes before you touch real code

๐Ÿ“š Tutorial โฑ ~45 min read + practice ๐Ÿงฉ 5 Parts ยท 15 Sections ยท 10 Quizzes Beginner

๐ŸŽฏ What You'll Learn

๐Ÿ“‹ Before You Begin

๐Ÿ“– Table of Contents

  1. What Is Git and Why Do You Need It?
  2. Installation & First-Time Setup
  3. Your First Repository: git init
  4. The Three Trees: Working Directory, Staging, Repository
  5. Tracking Changes: status, add, commit, log, diff
  6. Branching: Work Without Fear
  7. Merging & Conflict Resolution
  8. GitHub: Connect Your Local Repo to the Cloud
  9. Push, Pull, and Fetch โ€” The Sync Triad
  10. Pull Requests: The Heart of Collaboration
  11. Forking: Contributing to Others' Repos
  12. Undoing Changes: reset, revert, restore
  13. .gitignore โ€” What NOT to Commit
  14. Practice Session: Build a Mini Project End-to-End
  15. Cheat Sheet & Key Takeaways

1. What Is Git and Why Do You Need It? Beginner

Git is a distributed version control system created by Linus Torvalds in 2005 to manage the Linux kernel โ€” a project with thousands of contributors worldwide. It tracks every change to every file in your project, forever.

Here's what Git gives you:

โœ… The core idea Git doesn't store copies of files โ€” it stores snapshots of your entire project at each commit. This makes it incredibly efficient: comparing two commits is just comparing two trees of snapshots, not scanning every file.

Git vs GitHub โ€” What's the Difference?

This confuses every beginner. Here's the simplest explanation:

GitGitHub
A tool that runs on your computerA website (cloud service)
Manages version history locallyHosts copies of your repos online
Works completely offlineRequires internet to sync
Free and open-sourceFree for public repos; paid plans for private teams
Created by Linus Torvalds (2005)Founded by Tom Preston-Werner (2008)

Analogy: Git is your engine โ€” it does the real work of tracking changes. GitHub is a parking lot where you share your car (repo) with others. You need the engine to drive; the parking lot just makes it easy to park together.

2. Installation & First-Time Setup Beginner

Installing Git

OSHow to InstallVerify
Windows Download from gitforwindows.org. Run the installer (defaults are fine). git --version
macOS Open terminal and type git --version. If not installed, it prompts you to install Xcode Command Line Tools. git --version
Linux (Ubuntu/Debian) sudo apt update && sudo apt install git -y git --version
โš ๏ธ Windows users: Git Bash When you install Git for Windows, it includes "Git Bash" โ€” a Unix-style terminal that behaves like Linux. Use Git Bash for all commands in this tutorial, not the standard Command Prompt. It makes your life easier.

Configure Your Identity

Git stamps every commit with your name and email. Set them once and forget them:

git config --global user.name "Your Full Name"
git config --global user.email "[email protected]"

Set Your Default Editor

Git sometimes opens a text editor (for commit messages, merge descriptions, etc.). Set it to VS Code (or your preferred editor):

git config --global core.editor "code --wait"

Verify Configuration

git config --list
๐Ÿ’ก "I used the wrong name/email โ€” how do I fix it?" Run the same git config --global commands again with the correct values. Git overwrites the old values. Past commits still show the old identity โ€” but all future commits will use the new one.

Quiz 1 ๐Ÿง  Git Basics

What is the difference between Git and GitHub?

Correct! Git is the engine (local version control); GitHub is the parking lot (cloud hosting). You can use Git perfectly fine without ever touching GitHub.

3. Your First Repository: git init Beginner

A repository (or "repo") is simply a folder that Git is watching. To turn any folder into a Git repository:

mkdir my-first-repo
cd my-first-repo
git init

That's it. Git creates a hidden .git/ folder inside your project. This folder is the engine room โ€” it contains the entire history, all branches, all configuration. Never touch it directly.

๐Ÿ” See for yourself Run ls -la (or dir /a on Windows). You'll see the .git/ directory. Delete it, and your repo is no longer a repo โ€” just a regular folder again.

Checking the State: git status

The most-used command in Git. Run it constantly to stay oriented:

git status

Right now, it tells you: "No commits yet" and lists your untracked files (if any). We'll fix that next.

Quiz 2 ๐Ÿง  Your First Repo

What does git init do?

Correct! git init creates the .git/ folder that Git uses to store all version history, branches, and configuration.

4. The Three Trees: Working Directory, Staging, Repository Beginner

This is the single most important concept in Git. Understand this, and everything else clicks into place.

Git manages your files across three "trees":

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Working       โ”‚     โ”‚     Staging     โ”‚     โ”‚   Repository    โ”‚
โ”‚   Directory     โ”‚ โ”€โ”€โ–ถ โ”‚     Area        โ”‚ โ”€โ”€โ–ถ โ”‚ (.git/ history) โ”‚
โ”‚                 โ”‚     โ”‚  (Index)        โ”‚     โ”‚                 โ”‚
โ”‚  Your editor    โ”‚     โ”‚  "git add"      โ”‚     โ”‚  "git commit"   โ”‚
โ”‚  changes here   โ”‚     โ”‚  prepares here  โ”‚     โ”‚  saves forever  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
TreeWhat It IsCommand to Move Forward
1. Working Directory The actual files you see and edit in your editor. Messy, alive, changing. You edit files here naturally.
2. Staging Area (Index) A "preview" area where you gather changes before committing. You decide exactly which changes go into the next snapshot. git add <file>
3. Repository (History) The permanent record. Every commit here is immortal โ€” it stays in the log forever. git commit -m "message"
๐Ÿ’ก The key insight The staging area is what makes Git different from older systems like SVN. You don't have to commit everything at once. You can edit 5 files, stage only 3 of them (because the other 2 aren't ready), and commit just those 3. The staging area gives you surgical control.

Quiz 3 ๐Ÿง  The Three Trees

Which Git command moves changes from the Working Directory to the Staging Area?

Correct! git add stages changes. git commit then moves them from staging to the permanent history (repository).

5. Tracking Changes: status, add, commit, log, diff Beginner

Let's create a file and walk through the cycle.

๐Ÿงช Practice 1: Make Your First Commit

1 Create a file
echo "Hello, Git!" > README.md
2 Check status
git status

Git says README.md is "untracked" โ€” it sees the file but isn't watching it yet.

3 Stage it
git add README.md

Now git status shows it as "Changes to be committed" โ€” staged and ready.

4 Commit it
git commit -m "Add README with greeting"
5 View the log
git log

You should see your first commit: a unique hash, your name, your email, the timestamp, and your message.

git log โ€” Your Project's Diary

commit 1a2b3c4d5e6f7g8h9i0j...
Author: Your Name <[email protected]>
Date:   Thu Jul 9 14:30:00 2026 +0530

    Add README with greeting

Every commit has a unique 40-character hash (like a fingerprint). You use this hash to reference the commit when you need to travel back in time.

Useful variants:

git log --oneline          # Compact view: one line per commit
git log --graph            # Show branch structure visually
git log -p                 # Show the actual changes (diff) for each commit

git diff โ€” See Changes Before You Stage

Before staging, see exactly what changed:

# View unstaged changes
git diff

# View staged changes (what will be committed)
git diff --staged

Quiz 4 ๐Ÿง  Tracking Changes

What does git log --oneline do?

Correct! git log --oneline shows each commit on a single line with just the abbreviated hash and message โ€” perfect for scanning.

Quiz 5 ๐Ÿง  Staging vs Committing

You edit 3 files (a.py, b.py, c.py). You only want to commit a.py and b.py because c.py isn't finished. What do you do?

Correct! Stage only the files you want (git add a.py b.py), then commit. c.py stays in the working directory, untouched. This is exactly why the staging area exists.

6. Branching: Work Without Fear Beginner

A branch is a movable pointer to a commit. When you create a new branch, you're creating a parallel universe where you can experiment freely without affecting the main code.

main     โ—โ”€โ”€โ—โ”€โ”€โ—โ”€โ”€โ—
                  โ†‘ (main is here)

feature  โ—โ”€โ”€โ—โ”€โ”€โ—โ”€โ”€โ—โ”€โ”€โ—โ”€โ”€โ—
         (you branch off here)    (new commits only on feature)

Branching Commands

# List branches (* = current branch)
git branch

# Create a new branch
git branch feature-login

# Switch to it
git checkout feature-login

# Create AND switch in one command (modern way)
git checkout -b feature-login

๐Ÿงช Practice 2: Create and Work on a Branch

1 Create a branch
git checkout -b add-footer
2 Make a change
echo "<footer>ยฉ 2026 Our Team</footer>" > footer.html
git add footer.html
git commit -m "Add footer to all pages"
3 Switch back to main
git checkout main

Notice: footer.html is gone from your working directory! It only exists on the add-footer branch. Your main branch is untouched.

โš ๏ธ "main" vs "master" Git's default branch is called master in older versions and main in newer ones (since 2021). Both work identically. This tutorial uses main, but if your system uses master, just substitute the name.

Quiz 6 ๐Ÿง  Branching

You create a branch feature-x and make 3 commits on it. Then you switch back to main. Where are those 3 commits?

Correct! Each branch is an independent timeline. Commits on feature-x stay there until you merge them into main.

7. Merging & Conflict Resolution Intermediate

Merging combines the work from two branches. When your feature is ready, you merge it back into main.

# Switch to the branch you want to merge INTO
git checkout main

# Merge the feature branch INTO current branch
git merge add-footer

If both branches changed the same lines of the same file, Git produces a merge conflict. It doesn't know which version to keep, so it asks you to decide.

When a Conflict Happens

Git marks the conflict in the file:

<<<<<<< HEAD
This line was added on main
=======
This line was added on the feature branch
>>>>>>> add-footer

Your job: Edit the file to keep the correct version (or a combination of both), remove the conflict markers, then:

git add <file>
git commit -m "Resolve merge conflict between main and add-footer"
๐Ÿ’ก Merge conflicts are not failures They're a normal part of collaboration. Git can auto-merge 95% of changes without issue. The remaining 5% just need human judgment. VS Code, IntelliJ, and most editors have built-in merge conflict tools that make resolution visual and easy.

Quiz 7 ๐Ÿง  Merging

You are on main and run git merge feature-nav. What happens?

Correct! Merging brings the commits from feature-nav into the current branch (main). The feature branch still exists unless you delete it with git branch -d feature-nav.

8. GitHub: Connect Your Local Repo to the Cloud Beginner

So far, everything has been local โ€” on your machine. Now let's put it on GitHub so you can share it, back it up, and collaborate.

Step 1: Create a Repository on GitHub

  1. Log in to github.com
  2. Click the + icon (top-right) โ†’ New repository
  3. Name it my-first-repo (the same name as your local project)
  4. Leave it public (or check "Private" if you prefer)
  5. Do NOT check "Initialize with README" โ€” you already have one locally
  6. Click Create repository

Step 2: Add the Remote

A remote is a URL linking your local repo to GitHub. The conventional name is origin.

git remote add origin https://github.com/YOUR_USERNAME/my-first-repo.git

Step 3: Push Your Code

git push -u origin main

The -u flag sets upstream tracking โ€” after this, you can simply type git push and git pull without specifying the remote and branch every time.

โš ๏ธ First-time push? GitHub will ask for your credentials. Use a Personal Access Token (PAT), not your password. Create one: GitHub โ†’ Settings โ†’ Developer settings โ†’ Personal access tokens โ†’ Generate new token. Select repo scope. Copy it, paste it when prompted. Save it somewhere safe.

Quiz 8 ๐Ÿง  Remotes

What does git remote add origin <URL> do?

Correct! git remote add just stores the URL. It doesn't transfer any data. git push is what actually uploads your commits.

9. Push, Pull, and Fetch โ€” The Sync Triad Beginner

Once your local repo is connected to GitHub, you have three commands to sync changes:

CommandDirectionWhat It Does
git push Local โ†’ Remote Uploads your commits to GitHub. Others can now see your work.
git pull Remote โ†’ Local Downloads others' commits AND merges them into your current branch. (git fetch + git merge in one step)
git fetch Remote โ†’ Local (safe) Downloads others' commits but does NOT merge. Lets you inspect changes before integrating.

The Safe Workflow

# Step 1: Get latest info without merging
git fetch origin

# Step 2: See what's different
git log main..origin/main

# Step 3: Now merge (or use pull for a quick shortcut)
git pull origin main
๐Ÿ’ก Golden rule Pull before you push. Always. If someone pushed while you were working, your push will be rejected. Pull first (resolve any conflicts), then push again. This prevents 90% of collaboration headaches.

Quiz 9 ๐Ÿง  Push vs Pull vs Fetch

Which command downloads remote changes WITHOUT merging them into your working files?

Correct! git fetch downloads the data but doesn't touch your working directory. Use it when you want to inspect changes before merging. git pull = git fetch + git merge.

10. Pull Requests: The Heart of Collaboration Intermediate

A Pull Request (PR) is GitHub's mechanism for proposing changes. Instead of merging directly to main, you:

  1. Create a branch, make changes, push the branch to GitHub
  2. Open a PR asking to merge your branch into main
  3. Teammates review the code, leave comments, request changes
  4. Once approved, merge the PR

The Complete PR Workflow

๐Ÿงช Practice 3: Open Your First Pull Request

1 Create a feature branch
git checkout -b add-contact-form
2 Make changes and commit
echo "<form>Contact us...</form>" > contact.html
git add contact.html
git commit -m "Add contact form page"
3 Push the branch to GitHub
git push -u origin add-contact-form
4 Open a PR on GitHub

Go to your repo on GitHub. You'll see a banner: "add-contact-form had recent pushes". Click Compare & pull request. Add a title and description, then click Create pull request.

5 Merge the PR

Since you're the sole contributor, click Merge pull request. GitHub merges your branch into main on the remote.

6 Update your local main
git checkout main
git pull origin main

Your local main now has the latest code. Delete the feature branch: git branch -d add-contact-form

In a team, step 4 is where the magic happens โ€” teammates review your code, catch bugs, and discuss improvements before the code lands in main.

11. Forking: Contributing to Others' Repos Intermediate

Forking is how you contribute to a repository you don't own. A fork is your personal copy of someone else's repo on GitHub.

The Fork Flow

  1. Go to any public repo on GitHub (e.g., facebook/react)
  2. Click Fork (top-right) โ€” creates a copy under YOUR GitHub account
  3. Clone YOUR fork to your local machine
git clone https://github.com/YOUR_USERNAME/react.git
cd react
  1. Add the original repo as an upstream remote
git remote add upstream https://github.com/facebook/react.git
  1. Sync your fork with the original
git fetch upstream
git checkout main
git merge upstream/main
  1. Create a branch, make changes, push to YOUR fork, and open a PR from your fork to the original repo
๐Ÿ’ก Why fork instead of branching? You can't create branches in repos you don't own. Forking gives you your own playground. The original project maintainers review your PR just like any other โ€” they just merge it from your fork instead of from a branch inside their repo.

12. Undoing Changes: reset, revert, restore Intermediate

Everyone makes mistakes. Git has three different tools to undo things, depending on where the mistake is.

If you...CommandWhat it does
Changed a file but haven't staged it yet git restore <file> Discards changes in working directory โ€” file goes back to last commit state
Already staged a file (git add) but want to unstage git restore --staged <file> Unstages the file (keeps your edits, just removes from staging area)
Committed but want to undo the commit (keep changes) git reset --soft HEAD~1 Removes the commit, keeps changes staged
Committed but want to completely discard the commit AND changes git reset --hard HEAD~1 โš ๏ธ Destructive! Removes commit and all its changes. Use with extreme caution.
Already pushed a bad commit to GitHub git revert <commit-hash> Creates a new commit that undoes the old one. Safe to push โ€” doesn't rewrite history.
๐Ÿ”ด Never reset --hard on public commits If you've already pushed a commit to GitHub, use git revert instead. reset --hard rewrites history, which breaks everyone else's repos if they've pulled your bad commit. revert is the safe, collaborative undo.

Quiz 10 ๐Ÿง  Undoing Changes

You accidentally pushed a broken commit to GitHub. What's the safest way to undo it without disrupting your teammates?

Correct! git revert creates a new commit that undoes the bad one. It preserves history and is safe to push. Force-pushing a reset would erase history and break your teammates' clones.

13. .gitignore โ€” What NOT to Commit Beginner

Some files should never be committed to Git:

Create a .gitignore file in your project root listing patterns to exclude:

# .gitignore
node_modules/
.env
*.log
.DS_Store
dist/
__pycache__/
*.pyc

Then commit it:

git add .gitignore
git commit -m "Add .gitignore"
โš ๏ธ .gitignore only works for UNTRACKED files If a file is already tracked (already committed), adding it to .gitignore won't stop Git from tracking it. You must first remove it: git rm --cached <file>.

Bonus Quiz ๐Ÿง  .gitignore

You accidentally committed a file secret.env two weeks ago. You add it to .gitignore now. Will Git stop tracking it?

Correct! .gitignore only prevents Git from automatically adding untracked files. To stop tracking an already-tracked file, use git rm --cached (which removes it from the index but keeps it on disk).

14. Practice Session: Build a Mini Project End-to-End Intermediate

Let's put everything together. From scratch, build a small HTML project and push it to GitHub โ€” using branches, commits, and a pull request.

๐Ÿงช Final Project: Recipe Website

1 Create and initialize the project
mkdir recipe-site
cd recipe-site
git init
echo "# My Recipe Collection" > README.md
git add README.md
git commit -m "Initial commit with README"
2 Create the homepage on main
cat > index.html << 'EOF'


My Recipes

  

Welcome to My Recipe Collection

Delicious recipes from around the world.

EOF git add index.html git commit -m "Add homepage skeleton"
3 Create a branch for a new feature
git checkout -b add-recipe-page
4 Add a recipe page
cat > pancakes.html << 'EOF'


Pancakes Recipe

  

Fluffy Pancakes

Ingredients

  • 1 cup flour
  • 1 cup milk
  • 1 egg

Instructions

Mix, pour, flip, enjoy!

EOF git add pancakes.html git commit -m "Add pancakes recipe page"
5 Link it from the homepage (on the same branch)
sed -i '/<\/ul>/a Pancakes' index.html
# If sed doesn't work on Windows, manually add: Pancakes
git add index.html
git commit -m "Add pancakes link to homepage"
6 Merge the branch
git checkout main
git merge add-recipe-page
git branch -d add-recipe-page
7 Create a GitHub repo and push
# Create "recipe-site" on GitHub (no README, no .gitignore)
git remote add origin https://github.com/YOUR_USERNAME/recipe-site.git
git push -u origin main
8 Verify on GitHub

Refresh your repo page on GitHub. You should see all files, all commits, and the full history. You just shipped a real project with proper version control!

15. Cheat Sheet & Key Takeaways Beginner

๐Ÿ“‹ Quick Reference: Git Commands

CategoryCommandWhat It Does
Setupgit config --global user.name "Name"Set your name for all commits
git initCreate a new repo in current folder
git clone <url>Download a repo from GitHub
Daily Workgit statusCheck what's changed
git add <file>Stage changes
git commit -m "msg"Save staged changes as a snapshot
git log --onelineView history compactly
git diffSee unstaged changes
Branchinggit branch <name>Create a branch
git checkout <branch>Switch to a branch
git checkout -b <name>Create and switch in one step
git merge <branch>Merge branch into current
git branch -d <name>Delete a branch (after merge)
Syncgit push origin mainUpload commits to GitHub
git pull origin mainDownload + merge others' commits
git fetch originDownload without merging
git remote add origin <url>Link local repo to GitHub
Undogit restore <file>Discard unstaged changes
git restore --staged <file>Unstage a file
git revert <hash>Safe undo (creates new commit)
git reset --soft HEAD~1Undo last commit, keep changes

๐Ÿ† Key Takeaways