Git interview questions and answers guide
🌱 Beginner-Level Git Questions
1. What is Git?
Git is a distributed version control system (VCS) that allows multiple developers to track changes in source code, collaborate, and maintain a history of code revisions.
2. Difference between Git and GitHub?
Git: A version control system installed locally.
GitHub: A cloud platform to host Git repositories and enable collaboration.
3. What is a repository (repo) in Git?
A repository is a directory or storage space where your project files and their revision history are stored. It can be local or remote.
4. Explain Git clone, fetch, pull, and push.
clone: Copy a remote repository to your local machine.
fetch: Download changes from remote without merging.
pull: Fetch + merge changes from remote to local.
push: Upload local commits to a remote repository.
5. What is the difference between Git add and Git commit?
git add stages changes for the next commit.
git commit records staged changes into the repository history.
🧩 Intermediate-Level Git Questions
6. What is the difference between Git merge and Git rebase?
merge: Combines two branches into one, preserving history.
rebase: Moves a branch to start from a different commit, creating a linear history.
7. What are Git branches and why are they used?
Branches allow developers to work independently on features or bug fixes without affecting the main codebase.
8. Explain Git stash.git stash temporarily saves uncommitted changes, allowing you to switch branches without committing incomplete work.
9. What is a Git tag?
Tags mark specific points in history as important (e.g., release versions).
Lightweight tag: a simple pointer.
Annotated tag: includes metadata like author, date, and message.
10. How do you resolve a Git merge conflict?
Identify conflicting files (
git status).Edit files to resolve conflicts manually.
Stage resolved files (
git add) and commit.
⚙️ Advanced-Level Git Questions
11. What is Git rebase interactive (git rebase -i)?
It allows rewriting commit history by squashing, editing, or reordering commits for a cleaner history.
12. What are Git hooks?
Scripts that run automatically at certain Git events, like pre-commit or post-merge, to enforce checks or automation.
13. Explain Git reflog.git reflog tracks changes to the tip of branches, allowing recovery of lost commits or branches.
14. How do you revert a commit in Git?
git revert <commit>: creates a new commit that undoes changes.git reset --hard <commit>: moves the branch pointer backward (dangerous for shared branches).
15. What is the difference between Git reset, checkout, and revert?
reset: moves branch pointer and optionally changes staging/working tree.
checkout: switch branches or restore files.
revert: safely undoes changes by creating a new commit.
16. How do you squash commits in Git?
Use git rebase -i and mark commits as squash to combine multiple commits into one.
17. Explain Git submodules.
Submodules allow embedding one Git repository inside another, useful for dependency management.
18. How do you handle large files in Git?
Use Git LFS (Large File Storage) to store big files outside the main repo while tracking versions.
19. What is the difference between Git pull --rebase and Git pull?
git pull: fetch + merge.git pull --rebase: fetch + rebase, producing a linear commit history.
20. How do you undo a pushed commit?
Revert: safe for shared branches (
git revert <commit>).Force push: risky, rewrites history (
git push --force).
