Git is a distributed version control system (DVCS) that tracks changes in source code. Unlike centralized VCS like SVN, Git allows local commits, branching, and merging, improving speed and collaboration.
"Testing1"
GIT Interview Questions
1. What is Git and how is it different from other VCS?
2. What is the difference between Git and GitHub?
- Git is the version control tool (CLI).
- GitHub is a cloud-based hosting service for Git repositories with additional features (PRs, Issues, CI/CD integration).
3. What is the difference between git pull and git fetch?
- git fetch downloads commits without merging.
- git pull = git fetch + git merge.
4. What is the staging area in Git?
Also known as the index, it’s where changes are kept before they are committed. You add files to staging using git add.
5. How do you create and switch to a new branch?
git checkout -b feature-xyz # or (modern way) Git switch -c feature-xyz
6. What is the difference between merge and rebase?
- Merge creates a new merge commit and keeps history.
- Rebase rewrites commit history by applying your changes on top of another branch.
7. What is a conflict in Git? How do you resolve it?
A conflict occurs when two branches modify the same line.
Resolve by manually editing the file, then:
git add <file> Git commit
8. How do you delete a Git branch?
git branch -d branch-name # local git push origin --delete branch-name # remote
9. How do you undo a commit in Git?
- Undo last commit, keep changes: git reset --soft HEAD~1
- Undo and discard changes: git reset --hard HEAD~1
10. What is the difference between git reset, git revert, and git checkout?
- git reset: move HEAD to another commit (can modify history).
- git revert: creates a new commit that undoes a previous commit.
- git checkout: switch branches or restore files.
11. How to view commit history?
git log # full history git log --oneline # condensed Git log -p # with patch
12. How do you link a local Git repo to a remote?
git remote add origin https://github.com/user/repo.git Git push -u origin main
13. What is the difference between origin and upstream?
- origin is your fork (default remote).
- upstream is the original repo you forked from.
14. How do you clone a repository?
git clone https://github.com/user/repo.git
15. How do you handle SSH authentication for Git in 2025?
- Use ssh-keygen to generate a key.
- Add public key to GitHub/GitLab.
- Set remote URL with SSH: git remote set-url origin git@github.com:user/repo.git
16. What is a detached HEAD state?
When HEAD points directly to a commit instead of a branch — any changes made will be lost unless a new branch is created.
17. What are Git tags?
Tags are used to mark release versions or checkpoints in history.
git tag v1.0 Git push origin v1.0
18. How do you squash commits?
git rebase -i HEAD~3 # Mark commits with 'squash' or 's'
19. What is .gitignore?
A file listing patterns of files/directories Git should ignore (e.g., node_modules, *.log, env/).
20. How do you recover a deleted branch or commit?
Use the Git reflog:
git reflog git checkout <commit-id> Git checkout -b recovered-branch