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 rebase and merge
  • Using stash for temporary work
  • Selective commit application with cherry-pick
  • Recovering lost commits with reflog
  • Managing Git submodules

Table of Contents#

  1. Quiz Questions
  2. Best Practices Recap
  3. References

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 main
Rebase 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 rebase for a clean, linear history (e.g., before merging a feature branch into main).
  • Use merge to 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 stash for 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), or git 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 main

Best Practices#

  • Use cherry-pick for applying specific fixes (e.g., a bug fix from a release branch to main).
  • 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 reflog and then git cherry-pick or git reset to the commit hash.
  • (B) git log to find the commit.
  • (C) git fetch from 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 reflog to understand your repository’s history.
  • Avoid git reset --hard unless necessary, but rely on reflog for recovery if you do.

Question 5: Git Submodules#

Question: How do you initialize and clone a Git submodule?

  • (A) git submodule init and git submodule update after cloning a repo with submodules.
  • (B) git clone --recurse-submodules to clone with submodules.
  • (C) Both A and B.
  • (D) git submodule add to 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) and git 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-submodules when 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 rebase for clean history (local branches), merge for 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 audit HEAD changes.
  • Submodules: Clone with --recurse-submodules or 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! 🚀