Git Interview Questions and Answers

100 Git Interview Questions and Answers
  1. What is Git?

    • Answer: Git is a distributed version control system (DVCS) used to track changes in source code during software development. It allows multiple developers to collaborate on a project, manage different versions of the code, and revert to previous states if necessary.
  2. What is a repository in Git?

    • Answer: A repository (or repo) is a directory containing all the files and directories for a particular project, along with the Git metadata (information about the history of changes).
  3. Explain the difference between Git and GitHub.

    • Answer: Git is a version control system; GitHub is a web-based hosting service for Git repositories. Git is the tool, and GitHub is a platform that provides additional features like collaboration tools, issue tracking, and pull requests.
  4. What is the command to initialize a new Git repository?

    • Answer: git init
  5. How do you stage changes in Git?

    • Answer: Use the command git add or git add . (to stage all changes in the current directory and its subdirectories).
  6. What is the command to commit changes in Git?

    • Answer: git commit -m "Your commit message"
  7. What is the difference between git commit and git push?

    • Answer: git commit saves changes locally to your repository. git push uploads your local commits to a remote repository (like GitHub).
  8. How do you clone a Git repository?

    • Answer: git clone
  9. What is a branch in Git?

    • Answer: A branch is an independent line of development. It allows you to work on new features or bug fixes without affecting the main codebase.
  10. How do you create a new branch in Git?

    • Answer: git checkout -b
  11. How do you switch between branches in Git?

    • Answer: git checkout
  12. How do you merge a branch into another branch in Git?

    • Answer: Checkout the target branch, then git merge
  13. What is a merge conflict? How do you resolve it?

    • Answer: A merge conflict occurs when two branches have made changes to the same lines of code. You resolve it by editing the conflicted files, marking the changes you want to keep, and then staging and committing the resolved files.
  14. What is git rebase?

    • Answer: git rebase rewrites the project history by moving a branch's commits onto another branch. It creates a cleaner, linear history but should be used cautiously, especially on shared branches.
  15. What is a remote repository?

    • Answer: A remote repository is a version of your project that is hosted on a server, such as GitHub, GitLab, or Bitbucket.
  16. What is the command to add a remote repository?

    • Answer: git remote add origin
  17. What is the command to fetch changes from a remote repository?

    • Answer: git fetch origin
  18. What is the difference between git fetch and git pull?

    • Answer: git fetch downloads changes from the remote repository without merging them into your local branches. git pull fetches changes and merges them immediately.
  19. What is a tag in Git?

    • Answer: A tag is a pointer to a specific commit, often used to mark important points in the project's history, such as releases.
  20. How do you create a tag in Git?

    • Answer: git tag -a -m "Your tag message"
  21. How do you delete a local branch in Git?

    • Answer: git branch -d (use -D to force delete a branch that hasn't been merged)
  22. How do you delete a remote branch in Git?

    • Answer: git push origin --delete
  23. What is .gitignore?

    • Answer: A .gitignore file specifies files and directories that Git should ignore and not track.
  24. What is HEAD in Git?

    • Answer: HEAD is a pointer to the currently checked-out commit.
  25. What is the command to view the commit history?

    • Answer: git log
  26. What is the command to undo the last commit?

    • Answer: git reset --soft HEAD^ (or git reset --hard HEAD^, but use with caution)
  27. What is a stash in Git?

    • Answer: A stash is a way to temporarily save changes without committing them. Useful when you need to switch branches or clean your working directory.
  28. How do you stash changes in Git?

    • Answer: git stash push
  29. How do you apply a stashed change in Git?

    • Answer: git stash pop
  30. What is cherry-picking in Git?

    • Answer: Cherry-picking allows you to select individual commits from one branch and apply them to another.
  31. How do you cherry-pick a commit in Git?

    • Answer: git cherry-pick
  32. What is a Git hook?

    • Answer: Git hooks are scripts that run automatically before or after events like commits, pushes, or merges.
  33. What is Git bisect?

    • Answer: Git bisect is a tool to help find the commit that introduced a bug.
  34. Explain Git's staging area.

    • Answer: The staging area is a temporary holding area for changes before they are committed. It allows you to selectively choose which changes to include in a commit.
  35. What is a detached HEAD state in Git?

    • Answer: A detached HEAD state occurs when you're checking out a commit that isn't part of any branch. You should create a new branch if you want to make changes.
  36. How do you resolve a detached HEAD state?

    • Answer: Create a new branch from the detached HEAD commit using git checkout -b
  37. What is the difference between `git rm` and `rm`?

    • Answer: `rm` is a standard Unix command that deletes a file from your filesystem. `git rm` removes a file from both your filesystem and the Git repository.
  38. How do you view the differences between two commits?

    • Answer: git diff
  39. How do you view the differences between the working directory and the staging area?

    • Answer: git diff --staged
  40. How do you view the differences between the working directory and the last commit?

    • Answer: git diff
  41. What is the purpose of `git reflog`?

    • Answer: `git reflog` shows the history of changes to the tip of branches and HEAD. It is useful for recovering lost commits.
  42. What is a Git submodule?

    • Answer: A Git submodule is a way to include another Git repository as a subdirectory within your main project.
  43. What is a Git subtree?

    • Answer: A Git subtree is an alternative to submodules, offering a more integrated approach where the subproject's history is merged into the main project's history.
  44. Explain the concept of a working tree in Git.

    • Answer: The working tree is the copy of the project files you're currently working on. Changes made here are not yet tracked by Git until staged and committed.
  45. What is the command to revert a commit?

    • Answer: git revert
  46. What is the difference between `git revert` and `git reset`?

    • Answer: `git revert` creates a new commit that undoes the changes of a previous commit, preserving the project history. `git reset` moves the branch pointer, potentially losing commits.
  47. What is Git's object database?

    • Answer: Git stores all its data in an object database, which consists of various types of objects (blobs, trees, commits, tags) that are uniquely identified by their SHA-1 hashes.
  48. Explain the different types of Git objects.

    • Answer: Blobs store file data, trees represent the directory structure, commits store changes and metadata, and tags point to specific commits.
  49. How does Git handle large files efficiently?

    • Answer: Git LFS (Large File Storage) is designed to handle large files more efficiently by storing them outside the main Git repository and only storing pointers within the repo.
  50. What are some common Git workflows?

    • Answer: Gitflow, GitHub Flow, GitLab Flow, and feature branching are common workflows.
  51. Explain the Gitflow workflow.

    • Answer: Gitflow is a branching model that uses separate branches for development, features, releases, and hotfixes, ensuring a structured and organized development process.
  52. Explain the GitHub Flow workflow.

    • Answer: GitHub Flow emphasizes simplicity, using only the `main` branch and feature branches that are frequently merged. Pull requests are central to code review and collaboration.
  53. How can you protect your branches in Git?

    • Answer: On platforms like GitHub and GitLab, you can configure branch protection rules to prevent direct pushes to certain branches, requiring pull requests and code reviews.
  54. What are some best practices for using Git?

    • Answer: Write clear and concise commit messages, use feature branches, regularly push your changes, use pull requests for code review, and follow a consistent workflow.
  55. How can you find a specific commit in Git?

    • Answer: Use `git log` with various options to filter by author, date, message, or use `git rev-list` for more advanced searching. You can also search by parts of the commit message.
  56. How do you rename a branch in Git?

    • Answer: Checkout the branch, then use `git branch -m `. For remote branch rename, you also need `git push origin --delete ` and `git push origin `
  57. What is the significance of the `.git` folder?

    • Answer: The `.git` folder is the hidden folder in a Git repository that contains all the repository's metadata, including the object database, branches, and other internal data structures.
  58. How to undo changes in the working directory?

    • Answer: Use `git checkout -- ` to discard changes to a specific file, or `git reset --hard HEAD` to discard all changes in the working directory (use with extreme caution).
  59. What is the difference between a shallow clone and a full clone?

    • Answer: A shallow clone downloads only a portion of the repository's history, saving time and disk space. A full clone downloads the entire history.
  60. How to create a shallow clone?

    • Answer: Use `git clone --depth= ` to specify the number of commits to clone.
  61. What is a force push and when should it be avoided?

    • Answer: A force push (`git push --force`) overwrites the remote branch with your local branch. It should be avoided in collaborative environments as it can cause data loss for other developers.
  62. What is `git clean`?

    • Answer: `git clean` removes untracked files from your working directory. Use with caution as it permanently deletes files.
  63. Explain Git's concept of "porcelain" and "plumbing".

    • Answer: Porcelain refers to Git's high-level commands designed for ease of use (like `git commit`). Plumbing refers to its low-level commands for more advanced manipulation (like `git hash-object`).
  64. How to find the author of a specific commit?

    • Answer: Use `git log --pretty=format:"%an" ` to show the author name.
  65. How to see which branches are merged into a particular branch?

    • Answer: Use `git log --graph --oneline --decorate --all` to visualize the branch history and see which branches have merged into the one you are interested in.
  66. How does Git handle binary files?

    • Answer: Git can handle binary files, but it's less efficient for managing large binary files due to the way it stores changes as diffs. Git LFS is recommended for large binaries.

Thank you for reading our blog post on 'Git Interview Questions and Answers'.We hope you found it informative and useful.Stay tuned for more insightful content!