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
.gitdirectory - 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, andgit 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 --versionto 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.namereturns your namegit statusinsidemy-first-repo/shows a clean working tree after your first commitgit log --onelineshows 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 .andgit add README.md? - Why does
git statusshow a file as โuntrackedโ before staging?
โก Quick Wins
- Run
git initto create a repository - Create a file,
git addit, andgit commitit - Run
git statusbefore and after staging to see the difference - Run
git logto 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:
- Open the file in your editor
- Choose which content to keep (or combine both)
- Remove the conflict markers (
<<<<<<<,=======,>>>>>>>) - 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 branchshows at least two branches (includingmain)git log --oneline --graph --allshows a merge commit- You have successfully resolved at least one merge conflict
๐ง Knowledge Check:
- What happens to your working directory files when you
git switchbetween branches? - Why is
git branch -dsafer thangit 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
mainand 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 -vshows your GitHub URL for both fetch and push- Your repository is visible at
https://github.com/yourusername/my-first-repo git log --onelinematches between local and GitHub
๐ง Knowledge Check:
- What does the
-uflag ingit push -u origin maindo? - What is the difference between
git fetchandgit pull? - Why should
.envfiles 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
.gitignoreand verify ignored files donโt appear ingit status - Use
git log --oneline --graphto 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 revertto 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.mdwith project title, description, and usage section - Contains a
.gitignorewith 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
mainbranch with at least 2 files - Create
feature/headerandfeature/footerbranches frommain - Both branches modify the same line in
index.html(to force a conflict) - Merge
feature/headerintomain(should fast-forward or clean merge) - Merge
feature/footerintomainโ resolve the conflict - Final
git log --oneline --graph --allshows 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-improvementbranch - 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 --graphshows 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
.gitignoreis 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 revertdemonstrating 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:
- Explain the three areas (working directory, staging area, repository) in your own words.
- Describe when you would use
git revertvs.git reset. - Draw (mentally or on paper) the branch topology for a feature-branch workflow.
- Troubleshoot a scenario where
git pushis 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
๐ฏ Recommended Follow-Up Quests
- 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
- Git Official Documentation โ Primary reference guide
- Pro Git Book (free) โ Comprehensive deep dive
- GitHub Docs โ Remote collaboration reference
๐ฎ Interactive Learning
- Learn Git Branching โ Visual Interactive โ Practice branching in your browser
- GitHub Skills โ Interactive Tutorials โ Guided GitHub exercises
- Oh My Git! โ Game-Based Learning โ Learn Git through a game
๐ Quick References
- Atlassian Git Tutorials โ Beginner to advanced guides
- Git Cheat Sheet (GitHub) โ Printable command reference
- Conventional Commits โ Commit message standard