Advanced Git commands and workflows demonstrate a developer’s capability to handle complex version control scenarios. From rewriting commit histories to reducing repository size, mastering these advanced topics can help you impress interviewers during discussions about Git.
Here’s a detailed guide to common advanced Git interview questions and answers for technical interviews.
Also, check Part 1, PART 2 and Part 3 of this article.
22. You Need to Squash Multiple Commits Into One Before Pushing. How Do You Do That?
Squashing commits is useful for cleaning up a Git history before sharing your work. To squash multiple commits into one:
1- Perform an interactive rebase to pick the commits you want to squash:
# git rebase -i HEAD~<number-of-commits>
2- Replace pick with squash for the commits you want to combine.
3- Save and close the editor. Git will prompt you to edit the commit message.
4- Exit after finalizing the message:
# git rebase --continue
After squashing, the branch’s history will show fewer commits, making it cleaner and easier to review.
23. How Do You Rewrite Commit History to Change a Commit Message From a Few Commits Ago?
To update a commit message from a few commits ago, use an interactive rebase:
1- Rewrite the commits:
# git rebase -i HEAD~<number-of-commits>
2- In your editor, change pick to edit for the commit whose message you want to modify.
Update the commit message:
# git commit --amend
3- Continue the rebase process:
# git rebase --continue
Note: Only rewrite commit history on private branches. Never rewrite history after pushing to a shared remote branch.
24. Explain How git cherry-pick Works and When You Would Use It
What Is git cherry-pick?
The git cherry-pick command allows you to apply a specific commit from one branch to another without merging the entire branch history.
When to Use git cherry-pick?
When you only need a specific fix or feature from another branch.
When you want to include isolated changes without merging unrelated commits.
How It Works
Identify the commit hash you want to apply:
# git log <source-branch>
Apply the commit to your current branch:
# git cherry-pick <commit-hash>
Cherry-picking is particularly useful when pulling in bug fixes or experimental changes without affecting your branch’s code structure.
25. How Do You Create and Apply Patches in Git?
Create a Patch
Use the git format-patch command to create a patch file:
# git format-patch HEAD~<number-of-commits>
This generates .patch files for the specified commits.
Apply a Patch
Use the git apply command to apply patch files:
# git apply <patch-file>
Alternatively, if you need to apply patches as commits, use:
# git am <patch-file>
Patches are useful for sharing changes between repositories or reviewing updates before merging them.
26. You Have Diverged From the Remote Branch, and Your Local Commits Conflict With Remote Changes. How Do You Fix This?
To resolve conflicts caused by diverging local and remote branches:
Use git fetch to get the latest changes from the remote repository:
# git fetch origin
Rebase your local branch onto the updated remote branch:
# git rebase origin/<branch-name>
Resolve conflicts manually if prompted.
Push the updated local branch:
# git push origin <branch-name> --force-with-lease
Rebasing ensures that your local commits appear “on top” of the latest remote commits, resolving divergence effectively.
27. You Need to Find Out Who Made Changes to a Specific Line in a File. What Command Do You Use?
To identify who modified a specific line in a file, use the git blame command:
# git blame <file>
This displays a line-by-line history of changes in the file, including the commit hash, author, and timestamp of each change.
Example Output:
a2c4f19 (John Doe 2023-08-15 12:34) Modified line 1
b3d5g20 (Jane Smith 2023-08-14 11:23) Modified line 2
This is helpful for troubleshooting bugs or understanding historical context.
28. How Do You Temporarily Save Changes That Are Not Ready to Be Committed?
When you’re working on incomplete changes but need to switch branches or pull updates, you can use the git stash command:
Save your changes temporarily:
# git stash
Switch branches or pull updates.
Restore your changes:
# git stash pop
Stashing allows you to preserve uncommitted work without making a permanent commit, ensuring a clean workflow.
29. Your Git Repository Has Become Very Large. How Do You Reduce Its Size Without Affecting Its History?
To shrink a large Git repository:
Remove old and unnecessary branches:
# git branch -D <old-branch>
# git push origin --delete <old-branch>
Cleanup repository objects:
# git gc
Use git filter-repo to remove unused files or directories:
# git filter-repo --path <file-or-folder> --invert-paths
Reducing the repository size ensures faster clone times and better performance.
30. Explain the Difference Between git revert and git reset. When Would You Use Each?
git revert
Creates a new commit that undoes the effects of a previous commit.
Preserves history intact.
Used for correcting production issues without rewriting history.
Example:
# git revert <commit-hash>
git reset
Moves the HEAD backward, removing the specified commit from history.
Can alter staging and working directory, depending on flags (--soft, --hard).
Suitable for private branches where history rewriting is acceptable.
Example:
# git reset --hard <commit-hash>
When to Use Each?
Use git revert for shared repositories to avoid rewriting history.
Use git reset for private branches to clean up commit history.
Conclusion
Advanced Git commands are essential for handling complex version control scenarios, including squashing commits, rewriting history, resolving conflicts, and optimizing repository size. These topics showcase your expertise and problem-solving skills, demonstrating a deeper understanding of Git workflows during interviews.
By mastering these advanced Git scenarios& advanced git interview questions, you’ll be well-prepared to tackle challenging questions and stand out as a strong candidate in technical Git discussions.
We are giving you exclusive deals to try Linux Servers for free with 100$ credit, check these links to claim your 100$,
DigitalOcean - 100$ free credit & Linode - 100$ free credit
Check some Exclusive Deals, HERE.
Also, check out DevOps Book You should read section.