Git Quiz: Day 3 – Intermediate to Advanced Concepts
Welcome to Git Quiz Day 3! This quiz focuses on intermediate to advanced Git concepts, helping you deepen your understanding of version control workflows, history manipulation, and repository management. Whether you’re a developer refining your Git skills or a team lead optimizing collaboration, mastering these concepts is crucial for efficient codebase management.
In this blog, we’ll explore:
- Key differences between
rebaseandmerge - Using
stashfor temporary work - Selective commit application with
cherry-pick - Recovering lost commits with
reflog - Managing Git submodules
Table of Contents#
Quiz Questions#
Question 1: Rebase vs Merge#
Question: What’s the difference between git rebase and git merge when integrating changes from one branch to another?
- (A) Rebase rewrites history; merge creates a new commit.
- (B) Merge rewrites history; rebase creates a new commit.
- (C) Both rewrite history.
- (D) Neither rewrites history.
Answer: A#
Explanation#
git merge: Integrates changes by creating a new merge commit in the target branch. This preserves the original commit history of both branches (no rewrite).git rebase: Applies changes from the source branch onto the target branch by replaying each commit (new commit hashes are generated), which rewrites history (linearizes the commit graph).
Example Usage#
Merge Example:#
# Switch to the target branch (e.g., main)
git checkout main
# Merge changes from a feature branch
git merge feature
# Creates a new merge commit in mainRebase Example:#
# Switch to the feature branch
git checkout feature
# Rebase onto main (update feature with main’s latest changes)
git rebase main
# Now, merge feature into main (fast-forward, no new commit)
git checkout main
git merge feature Best Practices#
- Use
rebasefor a clean, linear history (e.g., before merging a feature branch intomain). - Use
mergeto preserve branch history (e.g., in shared repositories where others rely on the original branch structure).
Question 2: Git Stash#
Question: How do you temporarily save uncommitted changes to work on something else, and then restore them later?
- (A)
git stash - (B)
git commit -a - (C)
git reset - (D)
git clean
Answer: A#
Explanation#
git stash saves modified (tracked) and staged changes, then resets the working directory to the last commit. Later, you can apply the stash with git stash apply (or git stash pop to apply and remove the stash).
Example Usage#
# Save uncommitted changes
git stash
# Now your working directory is clean. Switch branches, fix a bug, etc.
# Later, apply the stash (keep it in the stash list)
git stash apply
# Or apply and remove from the stash list
git stash pop Best Practices#
- Use
git stashfor temporary work (e.g., switching branches to fix a critical bug). - Manage stashes with
git stash list(view all stashes),git stash drop <stash>(delete a specific stash), orgit stash clear(delete all stashes).
Question 3: Cherry-Pick#
Question: What does git cherry-pick do?
- (A) Picks a commit from one branch and applies it to another.
- (B) Deletes a commit from a branch.
- (C) Merges two branches.
- (D) Reverts a commit.
Answer: A#
Explanation#
git cherry-pick applies the changes from a single commit (or a range of commits) onto the current branch as a new commit.
Example Usage#
# Switch to the target branch (e.g., main)
git checkout main
# Apply a commit (with hash `abc123`) from a feature branch
git cherry-pick abc123
# A new commit with the changes from `abc123` is created in mainBest Practices#
- Use
cherry-pickfor applying specific fixes (e.g., a bug fix from a release branch tomain). - Avoid cherry-picking multiple commits if a merge is better (can lead to duplicate commits).
Question 4: Reflog and Recovery#
Question: How can you recover a commit that was accidentally deleted (e.g., after a git reset --hard)?
- (A)
git reflogand thengit cherry-pickorgit resetto the commit hash. - (B)
git logto find the commit. - (C)
git fetchfrom remote. - (D) It’s impossible.
Answer: A#
Explanation#
git reflog records every change to HEAD (and other refs), including deleted commits. Use it to find the commit hash, then git reset or git cherry-pick to recover the commit.
Example Usage#
# After a `git reset --hard`, lost commits
git reflog
# Find the commit hash (e.g., `def456`) from the reflog output
git reset --hard def456 # Restore the repository to that commit
# Or create a new branch to recover the commit
git checkout -b recover def456 Best Practices#
- Regularly check
git reflogto understand your repository’s history. - Avoid
git reset --hardunless necessary, but rely onreflogfor recovery if you do.
Question 5: Git Submodules#
Question: How do you initialize and clone a Git submodule?
- (A)
git submodule initandgit submodule updateafter cloning a repo with submodules. - (B)
git clone --recurse-submodulesto clone with submodules. - (C) Both A and B.
- (D)
git submodule addto clone.
Answer: C#
Explanation#
Submodules are nested Git repositories. To clone a repo with submodules:
- Use
git clone --recurse-submodules <url>(clones the parent repo and fetches submodules). - Or, after cloning the parent repo, run
git submodule init(initialize submodule config) andgit submodule update(fetch submodule content).
Example Usage#
Clone with Submodules:#
git clone --recurse-submodules https://github.com/parent/repo.git Initialize/Update After Cloning:#
# Clone the parent repo
git clone https://github.com/parent/repo.git
# Initialize and update submodules
git submodule init
git submodule update Best Practices#
- Use
--recurse-submoduleswhen cloning to fetch all submodules at once. - If adding a new submodule to your repo, use
git submodule add <url> <path>.
Best Practices Recap#
- Rebase vs Merge: Use
rebasefor clean history (local branches),mergefor shared branches. - Stash: Temporarily save work with
git stash; manage stashes to avoid clutter. - Cherry-Pick: Apply specific commits across branches (e.g., bug fixes).
- Reflog: Recover lost commits with
git reflog; use it to auditHEADchanges. - Submodules: Clone with
--recurse-submodulesor initialize/update manually.
References#
This quiz wraps up Day 3 of our Git series! Practice these concepts to master version control and collaboration. Stay tuned for more quizzes and deep dives into Git! 🚀