Skip to main content

Git Basics: Version Control Introduction

By IT-Journey Team

Learn Git fundamentals including repositories, commits, branches, and basic workflow for tracking code changes and collaborating with developers.

Estimated reading time: 22 minutes

Table of Contents

Greetings, brave adventurer! Welcome to the Git Basics quest โ€” where youโ€™ll learn the version control system that powers nearly every software project on the planet. Git lets you track changes, collaborate with others, and fearlessly experiment with code knowing you can always roll back. This is the single most important tool in any developerโ€™s inventory.

๐ŸŒŸ The Legend Behind This Quest

Long before the age of cloud realms and container kingdoms, developers toiled in darkness โ€” overwriting each otherโ€™s scrolls, losing ancient code to careless keystrokes, and deploying broken incantations with no way to revert. Then came Git, forged by Linus Torvalds in the fires of the Linux kernel. It granted developers the power of perfect memory: every change recorded, every experiment safely branched, every mistake reversible. Today, Git is the foundation spell in every developerโ€™s grimoire โ€” and mastering it is your first step toward true digital sorcery.

๐Ÿ—บ๏ธ Quest Network Position

graph TB
    subgraph "Prerequisites"
        Terminal["๐ŸŒฑ Terminal Fundamentals"]
    end
    
    subgraph "Current Quest"
        Main["๐Ÿฐ Git Basics: Version Control"]
    end
    
    subgraph "Unlocked Adventures"
        GitAdv["๐Ÿฐ Advanced Git Workflows"]
        GitHub["๐Ÿฐ GitHub Collaboration"]
        CICD["โš”๏ธ CI/CD Pipelines"]
    end
    
    Terminal --> Main
    Main --> GitAdv
    Main --> GitHub
    GitHub --> CICD
    
    style Main fill:#ffd700,stroke:#333,stroke-width:2px
    style Terminal fill:#87ceeb
    style GitAdv fill:#98fb98
    style GitHub fill:#98fb98
    style CICD fill:#98fb98

๐ŸŽฏ Quest Objectives

Primary Objectives (Required for Quest Completion)

  • Initialize a Git Repository โ€” Create a new repo and understand the .git directory
  • Track Changes with Commits โ€” Stage files and write meaningful commit messages
  • Work with Branches โ€” Create, switch, and merge branches
  • Collaborate with Remotes โ€” Push to and pull from a remote repository (GitHub)

Secondary Objectives (Bonus Achievements)

  • Resolve a Merge Conflict โ€” Handle conflicting changes between branches
  • Use .gitignore โ€” Exclude files from version control
  • Explore Git Log โ€” Navigate commit history with various log formats
  • Undo Mistakes โ€” Use git restore, git reset, and git revert

Mastery Indicators

  • Can explain the difference between working directory, staging area, and repository
  • Can create a feature branch, make changes, and merge it back
  • Can push code to GitHub and clone a remote repository
  • Can resolve a merge conflict manually

๐Ÿ—บ๏ธ Quest Prerequisites

๐Ÿ“‹ Knowledge Requirements

  • Terminal navigation basics (cd, ls, mkdir)
  • Understanding of files and directories

๐Ÿ› ๏ธ System Requirements

  • Git installed (git --version to verify)
  • GitHub account (free at github.com)
  • Text editor (VS Code recommended)

๐ŸŒ Install Git on Your Platform

๐ŸŽ macOS

# Git comes with Xcode CLI tools, or install via Homebrew
brew install git

# Verify installation
git --version

๐ŸชŸ Windows

# Install with winget
winget install Git.Git

# Or download from https://git-scm.com/download/win
# Verify installation
git --version

๐Ÿง Linux

# Ubuntu/Debian
sudo apt update && sudo apt install git

# Fedora
sudo dnf install git

# Verify installation
git --version

๐Ÿง™โ€โ™‚๏ธ Chapter 1: Your First Repository โ€” The Origin Story

Every great codebase starts with git init. In this chapter, youโ€™ll create your first repository and learn the fundamental rhythm of Git: edit โ†’ stage โ†’ commit.

โš™๏ธ Initial Configuration

Before your first commit, tell Git who you are:

# Set your identity (used in every commit)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Set default branch name to 'main' (recommended)
git config --global init.defaultBranch main

# Verify your settings
git config --list

๐Ÿ—๏ธ Creating a Repository

# Create a new project directory
mkdir my-first-repo
cd my-first-repo

# Initialize Git tracking
git init

Expected Output:

Initialized empty Git repository in /path/to/my-first-repo/.git/

๐Ÿ“ The Git Workflow: Edit โ†’ Stage โ†’ Commit

# 1. Create a file
echo "# My First Project" > README.md

# 2. Check status โ€” see untracked files
git status

# 3. Stage the file (add to staging area)
git add README.md

# 4. Commit with a descriptive message
git commit -m "docs: add initial README"

๐Ÿ” Understanding the Three Areas

flowchart LR
    WD["๐Ÿ“‚ Working Directory\n(your files)"] -->|git add| SA["๐Ÿ“‹ Staging Area\n(index)"]
    SA -->|git commit| REPO["๐Ÿ›๏ธ Repository\n(.git history)"]
    REPO -->|git restore| WD
    SA -->|git restore --staged| WD
    
    style WD fill:#ffcccc,stroke:#cc0000
    style SA fill:#fff3cd,stroke:#cc9900
    style REPO fill:#d4edda,stroke:#28a745
Working Directory    โ†’    Staging Area    โ†’    Repository
   (your files)          (git add)           (git commit)
   
   Edit files here      Preview what        Permanent snapshot
                         will be committed   in project history

โœ… Chapter 1 Checkpoint: Repository Initiation

Validation โ€” confirm each before proceeding:

  • git config user.name returns your name
  • git status inside my-first-repo/ shows a clean working tree after your first commit
  • git log --oneline shows at least one commit with a proper message

๐Ÿง  Knowledge Check:

  • Can you explain what .git/ contains and why deleting it removes all history?
  • What is the difference between git add . and git add README.md?
  • Why does git status show a file as โ€œuntrackedโ€ before staging?

โšก Quick Wins

  • Run git init to create a repository
  • Create a file, git add it, and git commit it
  • Run git status before and after staging to see the difference
  • Run git log to see your first commit

๐Ÿง™โ€โ™‚๏ธ Chapter 2: Branching โ€” Parallel Universes

Branches let you work on features in isolation without affecting the main codebase. Think of them as parallel timelines you can merge together when ready.

๐Ÿ”€ Branch Lifecycle Diagram

gitGraph
    commit id: "init"
    commit id: "add README"
    branch feature/greeting
    checkout feature/greeting
    commit id: "add greeting"
    commit id: "update greeting"
    checkout main
    merge feature/greeting id: "merge feature"
    commit id: "continue work"

๐ŸŒฟ Creating and Switching Branches

# Create a new branch
git branch feature/greeting

# Switch to it
git switch feature/greeting

# Or create and switch in one command
git switch -c feature/greeting

๐Ÿ“ Making Changes on a Branch

# Add a new file on the feature branch
echo "Hello, World!" > greeting.txt
git add greeting.txt
git commit -m "feat: add greeting message"

# See all branches (* marks current)
git branch

๐Ÿ”€ Merging Branches

# Switch back to main
git switch main

# Merge the feature branch into main
git merge feature/greeting

# Delete the branch (optional, it's merged)
git branch -d feature/greeting

โš ๏ธ Handling Merge Conflicts

When two branches modify the same lines, Git asks you to resolve the conflict:

# Git marks conflicts in the file like this:
<<<<<<< HEAD
Current content on main
=======
Different content from feature branch
>>>>>>> feature/greeting

To resolve:

  1. Open the file in your editor
  2. Choose which content to keep (or combine both)
  3. Remove the conflict markers (<<<<<<<, =======, >>>>>>>)
  4. Stage and commit:
git add conflicted-file.txt
git commit -m "fix: resolve merge conflict in greeting"

โœ… Chapter 2 Checkpoint: Branch Mastery

Validation โ€” confirm each before proceeding:

  • git branch shows at least two branches (including main)
  • git log --oneline --graph --all shows a merge commit
  • You have successfully resolved at least one merge conflict

๐Ÿง  Knowledge Check:

  • What happens to your working directory files when you git switch between branches?
  • Why is git branch -d safer than git branch -D?
  • When does Git create a merge commit vs. a fast-forward merge?

โšก Quick Wins

  • Create a branch with git switch -c feature/test
  • Make a commit on that branch
  • Switch back to main and merge it
  • Intentionally create and resolve a merge conflict

๐Ÿง™โ€โ™‚๏ธ Chapter 3: Remote Repositories โ€” Connecting to the World

Local Git is powerful, but the real magic happens when you connect to a remote like GitHub. This enables collaboration, backup, and sharing.

๐ŸŒ Local โ†” Remote Sync Flow

sequenceDiagram
    participant WD as ๐Ÿ’ป Working Directory
    participant LOCAL as ๐Ÿ›๏ธ Local Repo
    participant REMOTE as โ˜๏ธ GitHub (Remote)
    
    WD->>LOCAL: git commit
    LOCAL->>REMOTE: git push
    REMOTE->>LOCAL: git fetch
    LOCAL->>WD: git merge / git pull
    REMOTE-->>WD: git pull (fetch + merge)
    
    Note over WD,REMOTE: git clone creates LOCAL + WD from REMOTE

๐Ÿ”— Connecting to GitHub

# Add a remote repository
git remote add origin https://github.com/yourusername/my-first-repo.git

# Verify the remote
git remote -v

# Push your local commits to GitHub
git push -u origin main

๐Ÿ“ฅ Cloning an Existing Repository

# Clone a repository from GitHub
git clone https://github.com/username/repository.git

# This creates a local copy with full history
cd repository
git log --oneline

๐Ÿ”„ Staying in Sync

# Pull latest changes from remote
git pull origin main

# Push your changes to remote
git push origin main

๐Ÿ“„ The .gitignore File

Tell Git to ignore files that shouldnโ€™t be tracked:

# Create a .gitignore file
cat > .gitignore << 'EOF'
# OS files
.DS_Store
Thumbs.db

# Dependencies
node_modules/
__pycache__/

# Environment files
.env
*.log
EOF

git add .gitignore
git commit -m "chore: add gitignore for common files"

๐Ÿ“œ Exploring History

# View commit log
git log

# Compact one-line format
git log --oneline

# Visual branch graph
git log --oneline --graph --all

# Show changes in a specific commit
git show abc1234

โœ… Chapter 3 Checkpoint: Remote Connectivity

Validation โ€” confirm each before proceeding:

  • git remote -v shows your GitHub URL for both fetch and push
  • Your repository is visible at https://github.com/yourusername/my-first-repo
  • git log --oneline matches between local and GitHub

๐Ÿง  Knowledge Check:

  • What does the -u flag in git push -u origin main do?
  • What is the difference between git fetch and git pull?
  • Why should .env files always be in .gitignore?

โšก Quick Wins

  • Create a repository on GitHub and push your local repo to it
  • Clone a public repository and explore its history
  • Create a .gitignore and verify ignored files donโ€™t appear in git status
  • Use git log --oneline --graph to visualize history

๐Ÿง™โ€โ™‚๏ธ Chapter 4: Undoing Mistakes โ€” The Time-Travel Spells

Every adventurer makes mistakes. The mark of a true Git sorcerer is knowing how to undo them safely without destroying the timeline.

โ†ฉ๏ธ Undo Decision Flowchart

flowchart TD
    A{"What do you want to undo?"} -->|"Unstage a file\n(keep changes)"| B["git restore --staged file"]
    A -->|"Discard local changes\n(lose edits)"| C["git restore file"]
    A -->|"Undo last commit\n(keep changes)"| D["git reset --soft HEAD~1"]
    A -->|"Undo last commit\n(discard changes)"| E["git reset --hard HEAD~1"]
    A -->|"Create a new commit\nthat reverses a previous one"| F["git revert <commit-hash>"]
    
    style B fill:#d4edda,stroke:#28a745
    style C fill:#ffcccc,stroke:#cc0000
    style D fill:#fff3cd,stroke:#cc9900
    style E fill:#ffcccc,stroke:#cc0000
    style F fill:#d4edda,stroke:#28a745

๐Ÿ”ง Common Undo Operations

# Unstage a file (keep your edits)
git restore --staged README.md

# Discard changes in working directory (โš ๏ธ destructive)
git restore README.md

# Undo the last commit but keep changes staged
git reset --soft HEAD~1

# Undo the last commit AND discard all changes (โš ๏ธ destructive)
git reset --hard HEAD~1

# Safely reverse a published commit (creates a new commit)
git revert abc1234

โš ๏ธ Safety Rules

Command Destructive? Safe for shared branches?
git restore --staged No Yes
git restore Yes โ€” loses edits Yes
git reset --soft No โš ๏ธ Only before pushing
git reset --hard Yes โš ๏ธ Only before pushing
git revert No Yes โ€” preferred method

โšก Quick Wins

  • Stage a file, then unstage it with git restore --staged
  • Make a commit, then undo it with git reset --soft HEAD~1
  • Use git revert to reverse a commit safely

๐ŸŽฎ Implementation Challenges

Challenge 1: ๐ŸŸข The Chronicle Keeper (๐Ÿ• 20 minutes)

Objective: Build a personal project repository with a clean, professional commit history.

Acceptance Criteria:

  • Repository initialized with git init
  • Contains a README.md with project title, description, and usage section
  • Contains a .gitignore with at least 5 ignore rules
  • Has exactly 5 commits following conventional commit format (feat:, docs:, chore:, etc.)
  • Repository pushed to GitHub with a visible green checkmark

Verification Command:

# Should show exactly 5 commits
git log --oneline | wc -l

# All commits should follow conventional format
git log --oneline | grep -E '^[a-f0-9]+ (feat|fix|docs|chore|refactor|test):'

Challenge 2: ๐ŸŸก The Branch Weaver (๐Ÿ• 30 minutes)

Objective: Simulate a feature-branch workflow with parallel development and a merge conflict.

Acceptance Criteria:

  • Start from a main branch with at least 2 files
  • Create feature/header and feature/footer branches from main
  • Both branches modify the same line in index.html (to force a conflict)
  • Merge feature/header into main (should fast-forward or clean merge)
  • Merge feature/footer into main โ€” resolve the conflict
  • Final git log --oneline --graph --all shows the merge topology

Expected Branch Topology:

gitGraph
    commit id: "initial commit"
    commit id: "add index.html"
    branch feature/header
    checkout feature/header
    commit id: "add header"
    checkout main
    branch feature/footer
    checkout feature/footer
    commit id: "add footer"
    checkout main
    merge feature/header id: "merge header"
    merge feature/footer id: "merge footer (conflict resolved)"

Verification Command:

# Should show merge commits
git log --oneline --graph --all

# Verify no conflict markers remain
grep -rn '<<<<<<' . && echo 'FAIL: unresolved conflicts' || echo 'PASS: no conflicts'

Challenge 3: ๐Ÿ”ด The Remote Ranger (๐Ÿ• 25 minutes)

Objective: Practice the full local-to-remote workflow including forking and pull requests.

Acceptance Criteria:

  • Fork a public repository on GitHub (e.g., a simple README-only repo)
  • Clone your fork locally
  • Create a feature/my-improvement branch
  • Make at least 2 commits with meaningful changes
  • Push the branch to your fork
  • Open a Pull Request (PR) comparing your branch to your forkโ€™s main
  • Add a PR description explaining what changed and why

Verification Command:

# Verify remote points to YOUR fork
git remote -v | grep 'your-username'

# Verify feature branch exists on remote
git branch -r | grep 'feature/my-improvement'

โš”๏ธ Boss Battle: The Git Gauntlet

Youโ€™ve trained with individual spells. Now face the ultimate trial โ€” a multi-phase challenge that tests every skill from this quest in a single, connected scenario.

๐Ÿฐ The Scenario

Youโ€™re building a โ€œQuest Logโ€ web page โ€” a simple HTML file that tracks your IT-Journey progress. You must use every Git skill from this quest to build it correctly.

Phase 1: Foundation (Repository Setup)

# Create and initialize the project
mkdir quest-log && cd quest-log
git init

# Create the initial file
cat > index.html << 'EOF'
<!DOCTYPE html>
<html>
<head><title>Quest Log</title></head>
<body>
  <h1>My Quest Log</h1>
  <ul>
    <li>Terminal Fundamentals โ€” โœ… Complete</li>
  </ul>
</body>
</html>
EOF

# Create .gitignore
echo -e ".DS_Store\n*.log\n.env" > .gitignore

git add .
git commit -m "feat: initialize quest log project"

Phase 2: Feature Development (Branching)

# Branch 1: Add styling
git switch -c feature/styling
cat > style.css << 'EOF'
body { font-family: sans-serif; max-width: 600px; margin: 2rem auto; }
h1 { color: #2d3748; }
li { padding: 0.5rem 0; }
EOF
# Link the stylesheet in index.html (add <link> in <head>)
git add .
git commit -m "feat: add basic CSS styling"
git switch main

# Branch 2: Add more quests (this will conflict with branch 1 in index.html)
git switch -c feature/more-quests
# Add Git Basics quest to the list in index.html
git add .
git commit -m "feat: add git basics quest to log"
git switch main

Phase 3: Integration (Merging + Conflict Resolution)

# Merge styling first
git merge feature/styling

# Merge more-quests (expect conflict in index.html)
git merge feature/more-quests
# >> RESOLVE THE CONFLICT <<
git add index.html
git commit -m "fix: resolve merge conflict in quest list"

Phase 4: Time Travel (Undo Operations)

# Make a deliberate mistake
echo "BROKEN CONTENT" >> index.html
git add . && git commit -m "feat: this is a mistake"

# Revert it safely
git revert HEAD --no-edit

# Verify the bad content is gone
cat index.html

Phase 5: Deployment (Push to Remote)

# Create the repo on GitHub, then:
git remote add origin https://github.com/yourusername/quest-log.git
git push -u origin main

๐Ÿ† Boss Battle Victory Conditions

  • Repository has at least 6 commits (init, style, quests, merge, mistake, revert)
  • git log --oneline --graph shows branching and merging
  • No unresolved conflict markers in any file
  • A revert commit is visible in the history
  • Repository is live on GitHub with all commits visible
  • .gitignore is present and working

Full Verification Script:

#!/bin/bash
echo "=== ๐Ÿฐ Git Gauntlet Verification ==="

# Check commit count
COMMITS=$(git log --oneline | wc -l | tr -d ' ')
[[ $COMMITS -ge 6 ]] && echo "โœ… Commits: $COMMITS (โ‰ฅ6)" || echo "โŒ Commits: $COMMITS (need โ‰ฅ6)"

# Check for merge commits
MERGES=$(git log --merges --oneline | wc -l | tr -d ' ')
[[ $MERGES -ge 1 ]] && echo "โœ… Merge commits found: $MERGES" || echo "โŒ No merge commits"

# Check for revert commits
REVERTS=$(git log --oneline | grep -c 'Revert\|revert')
[[ $REVERTS -ge 1 ]] && echo "โœ… Revert commits found: $REVERTS" || echo "โŒ No revert commits"

# Check for conflict markers
grep -rn '<<<<<<' . --include='*.html' --include='*.css' 2>/dev/null
[[ $? -ne 0 ]] && echo "โœ… No unresolved conflicts" || echo "โŒ Unresolved conflicts found!"

# Check .gitignore
[[ -f .gitignore ]] && echo "โœ… .gitignore exists" || echo "โŒ Missing .gitignore"

# Check remote
git remote -v | grep -q 'origin' && echo "โœ… Remote configured" || echo "โŒ No remote configured"

echo "=== Verification Complete ==="

๐Ÿ—บ๏ธ Complete Quest Flow

flowchart TD
    START["๐Ÿฐ Quest Start"] --> CH1["๐Ÿง™โ€โ™‚๏ธ Ch1: First Repository"]
    CH1 --> CP1{"โœ… Checkpoint 1\nRepo + Commits?"}
    CP1 -->|Pass| CH2["๐Ÿง™โ€โ™‚๏ธ Ch2: Branching"]
    CP1 -->|Fail| CH1
    CH2 --> CP2{"โœ… Checkpoint 2\nBranch + Merge?"}
    CP2 -->|Pass| CH3["๐Ÿง™โ€โ™‚๏ธ Ch3: Remotes"]
    CP2 -->|Fail| CH2
    CH3 --> CP3{"โœ… Checkpoint 3\nPush + Pull?"}
    CP3 -->|Pass| CH4["๐Ÿง™โ€โ™‚๏ธ Ch4: Undo Mistakes"]
    CP3 -->|Fail| CH3
    CH4 --> CHALLENGES["๐ŸŽฎ Implementation Challenges"]
    CHALLENGES --> BOSS{"โš”๏ธ Boss Battle:\nThe Git Gauntlet"}
    BOSS -->|Victory| REWARDS["๐ŸŽ Quest Rewards"]
    BOSS -->|Defeat| CHALLENGES
    REWARDS --> NEXT["๐Ÿ”ฎ Next Adventures"]
    
    style START fill:#6c5ce7,color:#fff
    style BOSS fill:#e17055,color:#fff,stroke-width:3px
    style REWARDS fill:#00b894,color:#fff
    style NEXT fill:#0984e3,color:#fff

๐Ÿ† Quest Completion Validation

Portfolio Artifacts Created

  • Git Repository โ€” Local repo with multiple commits, branches, and merges
  • GitHub Repository โ€” Remote repo with complete history pushed
  • .gitignore โ€” Properly configured ignore rules
  • Merge History โ€” At least one successful branch merge with conflict resolution
  • Revert History โ€” At least one git revert demonstrating safe undo
  • Quest Log Project โ€” The Boss Battle artifact on GitHub

Skills Demonstrated

  • Repository Management โ€” init, clone, remote add, config
  • Change Tracking โ€” add, commit, status, diff, log
  • Branching โ€” branch, switch, merge, conflict resolution
  • Remote Collaboration โ€” push, pull, clone, fork, PR
  • Undo Operations โ€” restore, reset, revert

๐Ÿง  Final Knowledge Assessment

Before claiming your reward, you should be able to answer:

  1. Explain the three areas (working directory, staging area, repository) in your own words.
  2. Describe when you would use git revert vs. git reset.
  3. Draw (mentally or on paper) the branch topology for a feature-branch workflow.
  4. Troubleshoot a scenario where git push is rejected because the remote has new commits.

๐ŸŽ Quest Rewards and Achievements

๐Ÿ† Achievement Badges Earned

  • ๐Ÿ† Version Control Initiate โ€” Mastered the fundamental Git workflow
  • ๐ŸŒฟ Branch Weaver โ€” Created, merged, and resolved conflicts across branches
  • โ˜๏ธ Remote Ranger โ€” Connected local and remote repositories
  • โช Time Traveler โ€” Demonstrated safe undo operations

โšก Skills and Abilities Unlocked

  • ๐Ÿ› ๏ธ Git Repository Management โ€” Create, configure, and maintain repositories
  • ๐Ÿ› ๏ธ Branch & Merge Operations โ€” Parallel development with clean integration
  • ๐Ÿ› ๏ธ Remote Collaboration โ€” Push, pull, clone, and work with GitHub
  • ๐Ÿ› ๏ธ History Navigation โ€” Read, search, and understand commit history
  • ๐Ÿ› ๏ธ Safe Undo โ€” Reverse mistakes without losing work

๐Ÿ“ˆ Your Journey Progress

  • Previous Skills: Terminal navigation, file system operations
  • Current Mastery: Git version control โ€” commits, branches, remotes, undo
  • Next Adventures: Advanced Git workflows, GitHub collaboration, CI/CD pipelines

๐Ÿ”ฎ Your Next Epic Adventures

  • Advanced Git Workflows โ€” Rebasing, cherry-picking, stashing, and interactive rebase
  • GitHub Collaboration โ€” Issues, pull request reviews, branch protection rules
  • CI/CD Pipelines โ€” Automated testing and deployment with GitHub Actions

๐ŸŒ Skill Web Connections

  • DevOps: Git is the foundation for all CI/CD and infrastructure-as-code workflows
  • Team Collaboration: Understanding Git enables effective code review and pair programming
  • Open Source: Contributing to open source projects requires Git fluency

๐Ÿ“š References & Resources

๐Ÿ“– Essential Documentation

๐ŸŽฎ Interactive Learning

๐Ÿ“‹ Quick References