=========================
✅ Basic Git Interview Questions and Answers
=========================
1. What is Git?
- Git is a distributed version control system that tracks changes in source code
during software development. It allows multiple developers to work on a project
simultaneously without overwriting each other's changes.
2. What is the difference between Git and GitHub?
- Git is the tool for version control; GitHub is a cloud-based hosting service for
Git repositories.
3. How do you initialize a Git repository?
- Use `git init` to create a new Git repository.
4. What is the purpose of `git init`?
- It initializes a new Git repository in the current directory.
5. What does `git clone` do?
- It creates a copy of an existing Git repository from a remote server to your
local machine.
6. What is `git add`?
- It stages changes (files or modifications) to be included in the next commit.
7. What does `git commit` do?
- It saves the staged changes to the local repository history.
8. What is the difference between `git pull` and `git fetch`?
- `git fetch` gets changes from the remote without applying them; `git pull`
fetches and merges them into your current branch.
9. How do you check the current status of your Git repository?
- Use `git status`.
10. How do you view the commit history?
- Use `git log`.
11. What is the use of `.gitignore`?
- It specifies intentionally untracked files that Git should ignore.
12. How do you stage multiple files at once?
- Use `git add .` or `git add -A`.
13. What does `HEAD` refer to in Git?
- It refers to the current branch reference or the latest commit in the current
branch.
14. How can you undo the last commit?
- Use `git reset --soft HEAD~1` to keep changes, or `git reset --hard HEAD~1` to
discard them.
15. How do you delete a file from both Git and the working directory?
- Use `git rm filename` followed by `git commit`.
=========================
🧠 Intermediate Git Interview Questions and Answers
=========================
16. Explain the difference between `git merge` and `git rebase`.
- `merge` combines changes and creates a new commit; `rebase` rewrites history by
moving or replaying commits.
17. What is a merge conflict and how do you resolve it?
- A merge conflict happens when Git can't auto-merge. You resolve it by manually
editing the file, then adding and committing the fix.
18. What is the staging area in Git?
- It’s where changes are stored before committing to the repo. Think of it as a
preparation area.
19. What does `git stash` do?
- Temporarily saves changes so you can switch branches without committing. Use `git
stash pop` to restore.
20. What is the difference between a local and remote branch?
- Local exists only on your machine; remote is hosted on a platform like GitHub.
21. How do you create and switch to a new branch?
- `git checkout -b branch-name`
22. How do you delete a Git branch?
- Local: `git branch -d branch-name`; Remote: `git push origin --delete branch-
name`
23. What does `git reset` do?
- It undoes changes. `--soft` keeps files staged, `--mixed` unstages them, `--hard`
discards changes.
24. What is `git cherry-pick`?
- It applies a commit from one branch onto another.
25. How do you squash commits?
- Use `git rebase -i HEAD~n` and mark commits as `squash`.
26. What does `git log` show?
- A list of commits. Use `git log --oneline` for a brief view.
27. What is `git reflog` used for?
- To view the history of HEAD changes, useful for recovering lost commits.
28. How do you revert a commit?
- Use `git revert <commit-hash>` to make a new commit that undoes a previous one.
29. What are tags in Git?
- Labels for specific commits, often used to mark releases. Create with `git tag
v1.0.0`
30. What is a detached HEAD?
- When HEAD points to a specific commit, not a branch. Any changes won’t be saved
to a branch unless checked out.
=========================
🔥 Advanced Git Interview Questions and Answers
=========================
31. Explain how Git works internally.
- Git stores data as objects: blobs (files), trees (directories), commits
(snapshots), and refs. It's more like a mini file system with history.
32. Difference between `git revert` and `git reset`?
- `revert` adds a new commit to undo, `reset` changes the commit history directly.
33. When should you use `git rebase` instead of `git merge`?
- When you want a cleaner history or are working on a feature branch before merging
into main.
34. How do you handle conflicts during a rebase?
- Resolve files manually, then run `git rebase --continue`.
35. What is the use of `git bisect`?
- Finds the commit that introduced a bug using binary search.
36. How does `git submodule` work?
- It allows you to include another Git repo inside your repo.
37. Explain `git filter-branch` or `git filter-repo`.
- These rewrite Git history. Used to remove sensitive data or large files.
38. What is a bare repository?
- A repository without a working directory. Used as a remote or central repo.
39. How do you optimize a large Git repository?
- Use `git gc`, `filter-repo`, remove large files or history, prune branches.
40. How does Git ensure data integrity?
- It uses SHA-1 hashing to ensure every file and commit has a unique hash.
41. How do you recover a deleted branch?
- Use `git reflog` to find the commit, then create a new branch from it.
42. How to recover from `git reset --hard`?
- If recently done, `git reflog` can help you find and restore lost commits.
43. What’s the difference between `origin/main` and `HEAD`?
- `origin/main` is the remote tracking branch, `HEAD` is your current local branch
pointer.
44. How does Git LFS work?
- Git LFS replaces large files with text pointers inside Git and stores the file
contents on a remote server.
45. How would you enforce a Git commit message policy?
- Use a commit-msg hook or a CI pipeline check.
=========================
🧪 Scenario-Based Git Questions and Answers
=========================
46. How would you resolve a merge conflict?
- Open conflicted files, fix them, `git add`, then `git commit` or `git merge --
continue`.
47. Teammate pushed broken code. What now?
- Revert commit, or fix and push a new commit. You can also force-push if history
needs cleaning (with caution).
48. Committed sensitive data. What now?
- Use `git filter-repo` to remove the file from history, then force-push.
49. Collaborating with a team using branches?
- Use feature branches, pull requests, and protect `main`. Rebase or merge after
approval.
50. Describe your Git workflow.
- Example: GitHub Flow - create branch → work → commit → push → pull request →
review → merge to `main`.