Posted in

Top 30 Git Interview Questions and Answers for All Levels

Prepare for your Git interviews with these 30 essential questions covering basic, intermediate, and advanced concepts. Whether you’re a fresher, have 1-3 years of experience, or 3-6 years in development, mastering Git is crucial for version control in any team environment.

Basic Git Interview Questions (1-10)

1. What is a Git repository?

A Git repository is a file structure where Git stores all project-based files along with their version history. It allows developers to track changes, revert to previous versions, and collaborate efficiently.

git init

2. How does Git work?

Git works as a distributed version control system that records snapshots of your project over time. Each commit creates a snapshot of changes, enabling branching, merging, and full local history access.

git commit -m "Initial commit"

3. What is git add?

The git add command stages changes from the working directory to the staging area (index) before committing. It prepares specific files or all changes for the next commit.

4. What is git status?

git status shows the difference between the working directory and the staging area. It displays tracked/untracked files, modified files, and commit readiness.

5. What is git push?

git push uploads local commits to a remote repository. It synchronizes your local branch with the remote branch, typically using git push origin main.

6. What is the purpose of the .gitignore file?

The .gitignore file specifies files and folders Git should ignore during tracking, such as logs, temporary files, or build artifacts, keeping the repository clean.

7. What does “origin” mean in Git?

“Origin” is the default name for the remote repository from which your local repository was cloned. It serves as the reference for fetch, pull, and push operations.

8. What is git clone?

git clone creates a copy of a remote repository on your local machine, including all branches, tags, and history. Example: git clone https://github.com/user/repo.git.

9. What is a commit in Git?

A commit is a snapshot of your repository at a specific point, with a unique SHA-1 hash. It records changes, author, date, and message for version tracking.

10. What is the working directory in Git?

The working directory is your project’s local folder where you edit files. Changes here are untracked until added to staging with git add.

Intermediate Git Interview Questions (11-20)

11. What is a branch in Git?

A branch is a lightweight pointer to a commit, allowing parallel development. Create one with git branch feature and switch using git checkout feature.

12. How do you create and switch to a new branch?

Use git checkout -b new-branch to create and switch in one command, or git branch new-branch followed by git checkout new-branch.

13. What is git merge?

git merge integrates changes from one branch into another. Switch to the target branch, then run git merge source-branch to combine histories.

14. What is the difference between git merge and git rebase?

git merge creates a merge commit preserving branch history, while git rebase replays commits linearly onto the target branch for a cleaner history.

15. What is git stash?

git stash temporarily saves uncommitted changes, clearing the working directory. Retrieve with git stash pop to apply stashed changes.

16. How do you view commit history in Git?

Use git log for detailed history, git log --oneline for compact view, or git log --graph to visualize branches.

17. What is git fetch?

git fetch downloads commits, files, and refs from a remote repository without merging. Review changes before integrating with git merge.

18. What is the difference between git pull and git fetch?

git fetch downloads remote changes only, while git pull fetches and merges them automatically into the current branch.

19. How do you rename a branch in Git?

Switch to the branch with git checkout old-name, then git branch -m new-name. For remote, push and delete the old reference.

20. What does git ls-tree do?

git ls-tree displays the tree object of the repository, showing mode, name, and SHA-1 for each item in a commit or tree.

Advanced Git Interview Questions (21-30)

21. How do you resolve merge conflicts in Git?

Identify conflicted files with git status, edit them manually, stage with git add, then commit the resolution with git commit.

22. What is git revert?

git revert <commit-hash> creates a new commit that undoes changes from a specific commit, preserving history safely for shared branches.

23. How do you cherry-pick a commit?

git cherry-pick <commit-hash> applies specific changes from one commit to the current branch as a new commit, without merging the entire branch.

24. What is a detached HEAD state?

Detached HEAD occurs when you’re not on a branch but directly on a commit. Create a new branch with git checkout -b new-branch to save work.

25. How do you squash commits in Git?

Use git rebase -i HEAD~n (interactive rebase) to mark commits as “squash” or “fixup”, then save to combine them into one commit.

26. What are Git configuration levels?

Git configs have –system (all users), –global (current user), and –local (repository) scopes. Set with git config --global user.name "Name".

27. How do you recover a lost commit in Git?

Use git reflog to view recent actions and hashes, then git cherry-pick or git reset --hard <hash> to restore it.

28. What is git bisect?

git bisect performs binary search on commit history to find the commit introducing a bug. Mark good/bad commits until it identifies the culprit.

29. In a scenario at Atlassian, how would you handle multiple Git configurations for different projects?

Use git config --local for repo-specific settings or includeIf in .gitconfig to load configs based on repository path automatically.

30. At Adobe, explain managing large monorepos with Git submodules.

Git submodules link external repositories into your repo. Add with git submodule add <url>, update with git submodule update --init --recursive.

Leave a Reply

Your email address will not be published. Required fields are marked *