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.
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
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"
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"!
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!
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!
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!
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
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"
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!
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!
Strengthen your version control mastery by studying these connected classes:
Test your theoretical knowledge with comprehensive BCA & tech interview questions.
Quick-reference syntax for all Git commands used in the lab exercises.
Find instant solutions for any errors encountered during practice scenarios.
Review all 50 classes from fundamentals to enterprise DevOps workflows.
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