Git Interview Questions: Key Concepts and Answers Explained

Git interview questions are designed to assess a candidate’s proficiency with Git, a distributed version control system widely used in software development. These questions typically cover fundamental concepts, practical commands, workflow strategies, and best practices. Interviewers may ask about:

  • Basic Git commands and their functions (e.g., git init, git clone, git add, git commit, git push, git pull).
  • Branching and merging strategies (e.g., feature branches, merge conflicts, rebase vs. merge).
  • Collaboration workflows (e.g., pull requests, code reviews, Gitflow).
  • Advanced topics (e.g., rebasing, cherry-picking, stashing, tagging).
  • Troubleshooting common issues (e.g., resolving conflicts, undoing changes, recovering from errors).

Candidates may also be asked to demonstrate their understanding through practical exercises or scenarios that test their ability to effectively use Git in real-world situations.

Are you preparing for a Git interview? Whether you’re a fresher or an experienced professional, mastering Git is crucial for your career in software development. This comprehensive guide covers essential Git interview questions and answers that cater to both beginners and seasoned developers. You’ll find questions ranging from basic concepts like repositories and branching to advanced topics such as rebasing, cherry-picking, and managing conflicts. Equip yourself with the knowledge and confidence to ace your next Git interview with these expertly curated questions and answers.

Top 47 Git Interview Questions

git interview questions and answers

Q1. What is a Git repository?
Ans: A Git repository is a storage space where your project files and their revision history are stored. It keeps track of changes made to the files and allows multiple people to collaborate on the same project. There are two types of repositories in Git: local and remote. The local repository is on your machine, while the remote repository is on a server.

Q2. What is the functionality of git ls-tree?
Ans: The git ls-tree command shows the content of a tree object, allowing you to view the structure of your repository at a specific commit. It lists the file names and types (blob for files, tree for directories) along with their SHA-1 hash.

Example:

git ls-tree HEAD

Q3. What does git annotate command do?
Ans: The git annotate (or git blame) command shows the last modification for each line of a file, indicating who made the change and when. This is useful for identifying the origin of specific changes.

Example:

git blame filename.txt

Q4. What is the difference between Git and GitHub?
Ans: Git is a distributed version control system that manages and tracks changes in source code. GitHub, on the other hand, is a web-based platform that uses Git for version control and provides additional features like issue tracking, code reviews, and collaboration tools.

Q5. What is a conflict in Git?
Ans: A conflict in Git occurs when changes from different branches or commits interfere with each other, making it unclear how the changes should be merged. Conflicts need to be resolved manually by the developer.

Q6. Differentiate between git pull and git fetch?
Ans:

  • git pull: Fetches changes from a remote repository and directly merges them into your current branch.
  • git fetch: Fetches changes from a remote repository but does not merge them. It updates your remote-tracking branches, allowing you to review changes before merging.

Q7. What is the difference between resetting and reverting?
Ans:

  • Resetting: Changes the commit history by moving the branch pointer to a previous commit, effectively removing later commits.
  • Reverting: Creates a new commit that undoes the changes of a previous commit without altering the commit history.

Q8. What is git add?
Ans: The git add command stages changes (modifications, deletions, additions) in your working directory to be included in the next commit.

Example:

git add filename.txt

Q9. What is git push?
Ans: The git push command uploads local repository content to a remote repository. It updates the remote branch with your local commits.

Example:

git push origin main

Q10. Explain the levels in git config and how can you configure values using them?
Ans: Git configuration values can be set at three levels:

  • System level: Applies to all users on the system. Configured using --system.
  • Global level: Applies to the user. Configured using --global.
  • Local level: Applies to the specific repository. Configured without any option.

Example:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

Q11. What has to be run to squash multiple commits (last N) into a single commit?
Ans: To squash multiple commits into a single commit, use an interactive rebase.

Example:

git rebase -i HEAD~N

Replace N with the number of commits to squash. Change pick to squash (or s) for the commits to be combined.

Q12. What is a merge in Git?
Ans: A merge in Git integrates changes from different branches into a single branch. It combines the histories and changes, creating a new commit if necessary.

Q13. What is the command used to delete a branch?
Ans: To delete a local branch:

git branch -d branch_name

To force delete a local branch:

git branch -D branch_name

To delete a remote branch:

git push origin --delete branch_name

Q14. What is cherry-pick in Git?
Ans: The git cherry-pick command applies changes from a specific commit to your current branch. It’s useful for incorporating specific changes without merging an entire branch.

Example:

git cherry-pick commit_hash

Q15. What do you mean by git reflog?
Ans: git reflog keeps a record of updates to the tip of branches and other references. It allows you to view and recover commits that are otherwise unreachable.

Example:

git reflog

Q16. What is a pull request?
Ans: A pull request is a feature in platforms like GitHub and GitLab where contributors can request to merge their changes into another branch or repository. It facilitates code review and collaboration.

Q17. What is branching in Git?
Ans: Branching in Git allows you to create separate lines of development within a repository. Branches are used to work on features, fixes, or experiments in isolation from the main codebase.

Q18. What is git fetch vs. git pull?
Ans:

  • git fetch: Downloads commits, files, and refs from a remote repository into your local repository but does not merge them.
  • git pull: Combines git fetch and git merge, downloading changes from a remote repository and immediately merging them into your current branch.

Q19. What is a version control system (VCS)?
Ans: A version control system (VCS) is a tool that helps manage changes to source code over time. It allows multiple developers to work on a project simultaneously, keeps track of changes, and enables reverting to previous versions. Examples include Git, Subversion (SVN), and Mercurial.

Q20. Why is it considered to be easy to work on Git?
Ans: Git is considered easy to work with due to its:

  • Distributed nature: Each developer has a full copy of the repository.
  • Branching model: Efficient and lightweight branching and merging.
  • Speed: Operations are fast and efficient.
  • Flexibility: Handles a wide range of workflows and use cases.

Q21. What is git status?
Ans: The git status command displays the state of the working directory and staging area. It shows which changes are staged, unstaged, and untracked.

Example:

git status

Q22. What is the difference between git rebase and git merge? Ans:

  • git merge: Combines changes from one branch into another, creating a new commit to preserve the history of both branches.
  • git rebase: Reapplies commits from a branch onto another, creating a linear history by moving the branch base to the latest commit of the target branch.

Q23. What is the meaning of “Index” or “Staging Area” in GIT?
Ans: The Index or Staging Area is an intermediate area where changes are listed before they are committed. It allows you to group related changes into a single commit.

Q24. What is GIT stash? Explain.
Ans: git stash temporarily saves changes that are not ready to be committed, allowing you to work on something else without losing your progress. You can later apply or pop the stashed changes back.

Example:

git stash
git stash apply
git stash pop

Q25. Define “Index”?
Ans: The Index, also known as the Staging Area, is a space where changes are gathered and reviewed before being committed to the repository. It acts as a buffer between the working directory and the repository.

Q26. What is a detached HEAD and what causes this and how to avoid this?
Ans: A detached HEAD occurs when HEAD points to a commit instead of a branch, often caused by checking out a specific commit or tag. To avoid this, always check out a branch.

Example:

git checkout branch_name

Q27. How does Git work?
Ans: Git works by tracking changes to files in a repository. It uses a directed acyclic graph (DAG) of commits to record changes, allowing for branching, merging, and collaboration. Changes are committed to the repository, and branches can be created for different lines of development.

Q28. What is a commit in Git?
Ans: A commit in Git is a snapshot of the repository at a specific point in time. It includes changes made to the files, a commit message, and metadata like the author and timestamp.

Q29. What is the difference between git stash apply vs git stash pop command?
Ans:

  • git stash apply: Applies the stashed changes but keeps the stash in the stash list.
  • git stash pop: Applies the stashed changes and removes the stash from the stash list.

Q30. What is the function of ‘git config’?
Ans: The git config command is used to set configuration options for Git. It can be used to define user information, aliases, preferences, and other settings at the system, global, or local level.

Example:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

Q31. What is the functionality of “git cherry-pick” command?
Ans: The git cherry-pick command applies changes from a specific commit to your current branch. This allows you to selectively integrate changes without merging entire branches.

Example:

git cherry-pick commit_hash

Q32. How do you find a commit which broke something after a merge operation?
Ans: To find a commit that introduced a bug after a merge, you can use git bisect. It performs a binary search between a known good commit and a bad commit to identify the problematic commit.

Example:

git bisect start
git bisect bad
git bisect good commit_hash

Q33. What differentiates between the commands git remote and git clone?
Ans:

  • git remote: Manages remote repository references. It allows you to add, remove, and modify remote repositories.
  • git clone: Creates a copy of an existing remote repository on your local machine.

Q34. What is the difference between git merge --no-ff and git merge --squash?
Ans:

  • git merge --no-ff: Creates a merge commit even if the merge could be resolved as a fast-forward, preserving the branch history.
  • git merge --squash: Combines all changes from the branch being merged into a single commit on the target branch, without creating a merge commit.

Q35. What are the constituents of the commit object contain?
Ans: A commit object in Git contains:

  • SHA-1 hash: Unique identifier.
  • Author information: Name and email of the person who made the commit.
  • Committer information: Name and email of the person who committed.
  • Commit message: Description of the changes.
  • Timestamp: Date and time of the commit.
  • Parent commits: References to the parent commits.

Q36. How would you remove all untracked files in Git?
Ans: To remove all untracked files in Git, use the git clean command.

Example:

git clean -f

To also remove untracked directories:

git clean -fd

Q37. What are Git hooks? Give examples of how they can be used to enforce code quality checks before commits or pushes?
Ans: Git hooks are scripts that run automatically at certain points in the Git workflow, such as before committing or pushing changes. They can be used to enforce code quality checks.

Example:

  • Pre-commit hook: Runs before a commit to check for coding standards violations.
  • Pre-push hook: Runs before a push to ensure tests pass.

Example of a pre-commit hook:

#!/bin/sh
# Check for coding standards violations
./run_code_checks.sh

Q38. Explain steps involved in removing a file from git index without removing from the local file system?
Ans: To remove a file from the Git index without deleting it from the local file system, use git rm --cached.

Example:

git rm --cached filename.txt

Q39. How do you define a ‘conflict’ in git? Ans: A conflict in Git occurs when changes from different branches or commits interfere with each other, making it unclear how the changes should be merged. Conflicts need to be resolved manually by the developer.

Q40. What are some strategies for managing large binary files in Git?
Ans:

  • Git LFS (Large File Storage): A Git extension for versioning large files.
  • External storage: Store large files outside the repository and use references.
  • Git attributes: Configure Git to handle large files differently.

Q41. Can you tell the differences between git revert and git reset?
Ans:

  • git revert: Creates a new commit that undoes the changes of a previous commit, preserving the commit history.
  • git reset: Moves the branch pointer to a previous commit, optionally modifying the index and working directory, potentially altering the commit history.

Q42. What are the functionalities of git reset --mixed and git merge --abort?
Ans:

  • git reset --mixed: Resets the current branch to a previous commit, updates the index, but does not change the working directory.
  • git merge --abort: Aborts a merge in progress and attempts to reconstruct the pre-merge state.

Q43. What is HEAD in Git, and how many HEADs can be created in a repository?
Ans: HEAD in Git refers to the current branch or commit checked out. Typically, there is only one HEAD per repository, but in detached HEAD state, it can point to a commit instead of a branch.

HEAD -> main -> commit1 -> commit2

Q44. What is the difference between git log and reflog?
Ans:

  • git log: Shows the commit history of the repository.
  • git reflog: Shows the history of updates to the tip of branches and other references, including actions that do not change the commit history.

Q45. What is a version control system? Mention its types?
Ans: A version control system (VCS) is a tool that helps manage changes to source code over time. Types include:

  • Centralized VCS (CVCS): Single central repository (e.g., SVN).
  • Distributed VCS (DVCS): Each user has a full copy of the repository (e.g., Git, Mercurial).

Q46. What are the advantages of Git over SVN?
Ans:

  • Distributed architecture: Each user has a full repository.
  • Branching and merging: Easier and more efficient.
  • Performance: Faster operations due to local repositories.
  • Flexibility: Supports various workflows and development models.

Q47. How to identify if a certain branch has been merged into master?
Ans: To identify if a certain branch has been merged into the master branch, you can use the following methods:

  1. git branch --merged master: This command lists all branches that have been merged into the master branch. If your target branch appears in this list, it has been merged into master.

Example:

git branch --merged master

  1. git log --graph --oneline --decorate master: This command shows a visual representation of the commit history. Look for the commits from the target branch in the history of the master branch.

Example:

git log --graph --oneline --decorate master
  1. git merge-base --is-ancestor branch_name master: This command checks if the specified branch is an ancestor of the master branch. If it returns no output and exits with a status of 0, it indicates that the branch has been merged into master.

Example:

git merge-base --is-ancestor branch_name master && echo "Merged" || echo "Not merged"

Using these methods, you can determine whether a branch has been successfully merged into the master branch.

Click here for more related topics.

Click here to know more about GIT.

About the Author