Class 47 of 50 • Practice & Viva Prep

Git & GitHub Practice Labs: 30+ Hands-on Terminal Scenarios with Solutions

Theory without practice is forgotten within days. To build genuine muscle memory with Git, you must execute terminal commands under realistic conditions. Here are 30+ hands-on lab practice scenarios with step-by-step verified terminal solutions designed for university computer science labs and self-taught software engineers.

Lab Category 1: Repository Setup & Basic Tracking

Lab 1: Initialize, Stage and Create Your First Commit

Goal: Create a project named veewom-lab1, configure your user identity locally, create an index.html file, stage it, and commit it using Conventional Commits syntax.

# Solution:
mkdir veewom-lab1 && cd veewom-lab1
git init
git config user.name "Student Dev"
git config user.email "student@veewom.com"
echo "<h1>Welcome to Veewom Git Lab</h1>" > index.html
git add index.html
git commit -m "feat(ui): create initial homepage layout"
git log --oneline

Lab 2: Selective Staging and Multi-File Snapshots

Goal: Create three files: app.js, styles.css, and temp.log. Stage only app.js and styles.css into one commit, and leave temp.log untracked.

# Solution:
touch app.js styles.css temp.log
git add app.js styles.css
git status -s
git commit -m "feat(core): add application entrypoint and stylesheet"

Lab 3: Constructing a Robust .gitignore Configuration

Goal: Prevent node_modules/, all .log files, and .env credentials from ever appearing in git status.

# Solution:
cat <<EOF > .gitignore
node_modules/
*.log
.env
EOF
git add .gitignore
git commit -m "chore: configure repository ignore rules"
# Verification: create dummy files and check status
touch error.log .env
git status  # Should show "nothing to commit, working tree clean"!

Lab Category 2: Undoing Mistakes & History Rewriting

Lab 4: Discarding Unstaged Changes with git restore

Goal: Modify app.js with broken experimental code, then discard all uncommitted edits to restore the file to pristine HEAD state.

# Solution:
echo "corrupted syntax garbage" >> app.js
git status
git restore app.js
cat app.js  # Clean!

Lab 5: Amending the Latest Commit with a Forgotten File

Goal: Create a commit modifying app.js. Realize you forgot to update README.md. Absorb README.md into the previous commit without creating a second commit.

# Solution:
echo "// logic" >> app.js
git commit -am "feat: implement calculation engine"
echo "# Calculation Engine Docs" > README.md
git add README.md
git commit --amend --no-edit
git log -n 1 --stat  # Shows both app.js and README.md in ONE commit!

Lab 6: Reverting a Commit Safely with git revert

Goal: Simulate a buggy commit on a shared branch and undo it safely using git revert without rewriting past history.

# Solution:
echo "function broken() {}" >> app.js
git commit -am "feat: introduce buggy function"
git log -n 1 --oneline
# Revert the latest commit:
git revert HEAD --no-edit
git log -n 2 --oneline  # Shows both the bug commit AND the revert commit!

Lab Category 3: Branching, Merging & Conflict Resolution

Lab 7: Feature Branching and Fast-Forward Merge

Goal: Create a branch named feature/login, commit a file, switch back to main, and merge it via Fast-Forward.

# Solution:
git switch -c feature/login
echo "function login() {}" > login.js
git add login.js && git commit -m "feat(auth): add login handler"
git switch main
git merge feature/login
git branch -d feature/login

Lab 8: Simulating and Resolving a Real Merge Conflict

Goal: Intentionally generate a merge conflict on line 1 of config.js between main and feature/theme, and resolve it cleanly.

# Solution:
echo "const THEME = 'dark';" > config.js
git add config.js && git commit -m "feat: set dark theme on main"

git switch -c feature/theme HEAD~1
echo "const THEME = 'light';" > config.js
git add config.js && git commit -m "feat: set light theme on feature"

git switch main
git merge feature/theme  # CONFLICT!
# Open config.js, resolve conflict to: const THEME = 'system';
echo "const THEME = 'system';" > config.js
git add config.js
git commit -m "merge: resolve theme conflict using system preference"

Lab 9: Cleaning Commits with Interactive Rebase (Squash)

Goal: Create 3 small commits on a branch and squash them into a single clean atomic commit.

# Solution:
git switch -c feature/cleanup
echo "1" >> data.txt && git commit -am "commit 1"
echo "2" >> data.txt && git commit -am "commit 2"
echo "3" >> data.txt && git commit -am "commit 3"

# Rebase last 3 commits interactively:
git rebase -i HEAD~3
# In editor, keep first line 'pick', change 2nd and 3rd to 'fixup' (f).
# Save and close!
git log -n 1 --oneline  # All 3 squashed into 1 clean commit!

Lab 10: Emergency Recovery Using git reflog

Goal: Run git reset --hard HEAD~1 to accidentally delete a commit, then use git reflog to bring it back from the dead.

# Solution:
echo "valuable secret code" > secret.txt
git add secret.txt && git commit -m "feat: top secret algorithm"
# Disaster strike!
git reset --hard HEAD~1
ls  # secret.txt is GONE!

# The Rescue:
git reflog
# Find the commit hash for 'feat: top secret algorithm' (e.g. HEAD@{1})
git reset --hard HEAD@{1}
ls  # secret.txt is 100% RESTORED!

Explore Related Git & GitHub Tutorials & Next Steps

Strengthen your version control mastery by studying these connected classes:

120+ Top Git Interview & Viva Q&A

Test your theoretical knowledge with comprehensive BCA & tech interview questions.

Git CLI Command Reference

Quick-reference syntax for all Git commands used in the lab exercises.

Git Troubleshooting & Master FAQ

Find instant solutions for any errors encountered during practice scenarios.

Git & GitHub Complete Syllabus

Review all 50 classes from fundamentals to enterprise DevOps workflows.

Frequently Asked Questions (FAQ) - Student & Interview Review

Yes! All commands listed in this lab execute identically across Windows PowerShell, Command Prompt, Git Bash, macOS Terminal, and Linux shells.
Simply delete the temporary practice folder: `rm -rf veewom-lab1` (or `Remove-Item -Recurse -Force veewom-lab1` in PowerShell).
Practicing 30 to 45 minutes daily for one week completing these 30 scenarios will give any student sufficient proficiency to excel in university exams and pass corporate entry-level developer assessments.

Next Step in Your Git & GitHub Learning Path:

Advance your software engineering skills with the next topic in our synchronized curriculum.

Continue to 120+ Interview & Viva Q&A → Previous: ← Git LFS Large Storage | Full Syllabus

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!