Git Interview Questions and Answers for experienced

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

    • Answer: Git is a distributed version control system (DVCS) that tracks changes in files and allows you to revert to previous versions. It's designed for speed, efficiency, and data integrity. Unlike centralized VCSs, every developer has a complete copy of the repository, including its history.
  2. Explain the difference between Git and GitHub.

    • Answer: Git is the version control system; GitHub is a web-based hosting service for Git repositories. Git is the underlying technology, while GitHub provides a platform for collaboration, code review, and project management features.
  3. What are the three main states of a Git file?

    • Answer: Modified, Staged, and Committed. Modified means the file has been changed but not yet tracked by Git. Staged means the changes are ready to be committed. Committed means the changes are permanently saved in the repository's history.
  4. Explain the difference between `git add` and `git commit`.

    • Answer: `git add` stages changes in the working directory, preparing them for the next commit. `git commit` saves the staged changes to the local repository's history, creating a new snapshot.
  5. What is a Git branch?

    • Answer: A branch is an independent line of development. It allows developers to work on new features or bug fixes without affecting the main codebase (usually the `main` or `master` branch). Branches can be merged back into the main branch once completed.
  6. How do you create a new branch in Git?

    • Answer: `git checkout -b `
  7. How do you switch between branches?

    • Answer: `git checkout `
  8. How do you merge a branch into another branch?

    • Answer: Checkout the target branch, then `git merge `. Resolve any merge conflicts that arise.
  9. 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 the same file. Git will mark the conflict in the file. You must manually edit the file, choosing which changes to keep, and then stage and commit the resolved file.
  10. What is `git rebase`? How does it differ from `git merge`?

    • Answer: `git rebase` rewrites the commit history by moving a branch's commits onto another branch, creating a linear history. `git merge` preserves the branch history, resulting in a merge commit. Rebase is generally preferred for cleaner history, but should be avoided on publicly shared branches.
  11. 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. It allows collaboration with other developers.
  12. How do you clone a remote repository?

    • Answer: `git clone `
  13. How do you push your local changes to a remote repository?

    • Answer: `git push `
  14. How do you pull changes from a remote repository?

    • Answer: `git pull `
  15. What is `git fetch`?

    • Answer: `git fetch` downloads the changes from a remote repository without merging them into your local branch. This allows you to see what changes have been made before merging them.
  16. What is a Git tag?

    • Answer: A tag is a pointer to a specific commit, usually used to mark important points in the project's history, such as releases (e.g., v1.0.0).
  17. How do you create a tag?

    • Answer: `git tag ` (lightweight tag) or `git tag -a -m "message"` (annotated tag)
  18. How do you delete a local branch?

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

    • Answer: `git push origin --delete ` or `git push origin :`
  20. What is `.gitignore`?

    • Answer: A `.gitignore` file specifies files and directories that should be excluded from version control. This is useful for ignoring temporary files, build artifacts, and other files that shouldn't be tracked.
  21. Explain the concept of a Git stash.

    • Answer: A stash temporarily saves uncommitted changes so you can switch branches or clean up your working directory without losing your work. You can later reapply the stashed changes.
  22. How do you stash your changes?

    • Answer: `git stash`
  23. How do you apply a stashed change?

    • Answer: `git stash pop` (applies and removes from stash) or `git stash apply` (applies without removing)
  24. What is `git cherry-pick`?

    • Answer: `git cherry-pick` applies specific commits from one branch to another. This is useful for applying a bug fix from one branch to another without merging the entire branch.
  25. What is a Git hook?

    • Answer: Git hooks are scripts that run automatically before or after Git events (e.g., commit, push). They can be used to automate tasks like code style checks, testing, or deployment.
  26. What is `git bisect`?

    • Answer: `git bisect` is a tool to help find the commit that introduced a bug. It uses a binary search algorithm to quickly identify the problematic commit.
  27. What is `git reflog`?

    • Answer: `git reflog` shows the history of your local repository's head pointer, which can help recover lost commits.
  28. How do you view the commit history?

    • Answer: `git log` (various options like `--oneline`, `--graph`, `--decorate` are available for customization)
  29. What is a detached HEAD state?

    • Answer: A detached HEAD state occurs when you checkout a commit that isn't on a branch. You're working directly on a specific commit. Changes should be branched or committed before switching back to a branch.
  30. How do you resolve a detached HEAD state?

    • Answer: Create a new branch from the detached HEAD commit using `git checkout -b ` or commit your changes and switch back to a normal branch.
  31. Explain the difference between `git pull --rebase` and `git pull`.

    • Answer: `git pull` performs a merge, while `git pull --rebase` performs a rebase. The latter integrates changes by re-writing the commit history, making it cleaner.
  32. What are some best practices for using Git?

    • Answer: Write clear and concise commit messages, use branches effectively, frequently commit small changes, review your code before pushing, use pull requests for collaboration, and keep your local repository clean.
  33. How can you revert a commit?

    • Answer: Use `git revert ` to create a new commit that undoes the changes of a previous commit. `git reset --hard ` will rewrite the history (use with caution!).
  34. What is the difference between `git reset --soft`, `git reset --mixed`, and `git reset --hard`?

    • Answer: `--soft` keeps changes in the staging area, `--mixed` (default) keeps changes in the working directory, and `--hard` discards changes entirely. Use with extreme caution, especially `--hard`.
  35. How do you view the differences between two commits?

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

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

    • Answer: `git diff`
  38. What is a Git submodule?

    • Answer: A Git submodule allows you to include another Git repository as a part of your project. Changes to the submodule are managed separately.
  39. What is a Git subtree?

    • Answer: A Git subtree is an alternative to submodules. It merges the code of another repository directly into your project's history, making it simpler to manage than submodules, but potentially causing conflicts.
  40. How do you handle large files in Git?

    • Answer: Use Git Large File Storage (LFS) to store large binary files outside of the main repository, while keeping track of their versions within Git.
  41. Explain the concept of a Git workflow (e.g., Gitflow).

    • Answer: A Git workflow is a set of guidelines for using Git to manage your project's development. Gitflow is a popular workflow that uses different branches for features, releases, and hotfixes.
  42. What is a fork in GitHub?

    • Answer: A fork is a personal copy of a remote repository. It allows you to make changes without affecting the original repository. Pull requests can be used to contribute changes back to the original repository.
  43. What is a pull request (or merge request)?

    • Answer: A pull request is a request to merge changes from one branch (typically a feature branch) into another branch (typically the main branch).
  44. How do you create a pull request on GitHub?

    • Answer: On GitHub, navigate to your forked repository, create a new branch for your changes, push the branch, and then create a pull request from that branch to the upstream repository.
  45. What is the significance of the `.git` directory?

    • Answer: The `.git` directory contains all of the Git metadata for the repository, including the repository's history, branches, and other internal data. It should never be modified directly.
  46. Describe the concept of a Git repository.

    • Answer: A Git repository is a directory that contains all of the files and metadata for a project, including the entire history of changes.
  47. What are some common Git commands you use daily?

    • Answer: `git status`, `git add`, `git commit`, `git push`, `git pull`, `git checkout`, `git log`.
  48. How do you find the commit hash for a specific commit?

    • Answer: Use `git log` to view the commit history and identify the hash, or use other Git commands that output the commit hash as part of their output.
  49. How do you undo the last commit?

    • Answer: `git reset HEAD^` (or `git reset --soft HEAD^` to keep changes staged). Use caution.
  50. How do you view the configuration of your Git installation?

    • Answer: `git config --list`
  51. How do you set your username and email in Git?

    • Answer: `git config --global user.name "Your Name"` and `git config --global user.email "your.email@example.com"`
  52. Explain the difference between a local and a remote branch.

    • Answer: A local branch exists only on your computer, while a remote branch exists on a remote repository (e.g., on GitHub).
  53. How do you rename a branch?

    • Answer: `git branch -m `
  54. What is the role of the HEAD pointer in Git?

    • Answer: The HEAD pointer always points to the currently checked out branch or commit.
  55. How do you create a bare Git repository?

    • Answer: `git init --bare .git`
  56. What are the benefits of using a bare Git repository?

    • Answer: Bare repositories are typically used as central repositories, ideal for server-side deployments, and are generally not intended for direct development work since they don't have a working directory.
  57. How do you create a patch file in Git?

    • Answer: `git diff > patch_file.patch`
  58. How do you apply a patch file in Git?

    • Answer: `git apply patch_file.patch` (or `patch -p1 < patch_file.patch`)
  59. Explain Git's object model.

    • Answer: Git's object model is based on four main object types: blobs (files), trees (directories), commits (snapshots), and tags (pointers to commits). These objects are content-addressed, meaning their hash is determined by the content, ensuring data integrity.
  60. What is a Git packfile?

    • Answer: Git packfiles are files that store multiple Git objects efficiently, reducing disk space and improving performance.
  61. How would you handle a situation where a large number of files have been accidentally added to the Git repository?

    • Answer: Use `.gitignore` to prevent future additions and possibly use `git reset` or `git filter-branch` to remove them from the history. The latter is very resource-intensive and should be considered carefully and tested thoroughly before application.
  62. How do you find all commits that modified a specific file?

    • Answer: `git log --follow -- `
  63. Explain the concept of a shallow clone.

    • Answer: A shallow clone downloads only a portion of the repository's history, reducing download time and disk space. Useful for large repositories where only the recent history is needed.
  64. What is the significance of the `--force` flag in Git commands (e.g., `git push --force`)?

    • Answer: The `--force` flag overwrites the remote branch with your local branch, potentially losing other developers' work. It should be used with extreme caution and only when absolutely necessary.
  65. How to revert a merge commit?

    • Answer: The best approach is to `git revert `. This creates a new commit that undoes the changes introduced by the merge.
  66. Explain how Git handles symbolic references.

    • Answer: Symbolic references (like branches) act as pointers to other references (like commits). They provide a human-readable way to refer to a specific commit.

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