Top 200+ Basic to Advance Git Interview Questions & Answers [2024]
Explore a comprehensive collection of basic to advanced Git interview questions and expertly crafted answers. Prepare for your next Git interview with confidence and master version control concepts effortlessly.
Basic Git Questions
1. What is Git?
Git is a distributed version control system used to track changes in source code during software development.
2. What is a repository in Git?
A repository is a storage location for software packages, which includes the entire history of changes and revisions.
3. How do you initialize a new Git repository?
Use the command: git init
.
4. How do you clone a repository?
Use the command: git clone
.
5. What is a commit in Git?
A commit is a snapshot of the project’s state at a specific point in time.
6. How do you create a new branch in Git?
Use the command: git branch
.
7. How do you switch to a different branch?
Use the command: git checkout
.
8. What is the difference between git fetch
and git pull
?
git fetch
downloads changes from the remote repository without integrating them. git pull
downloads and merges changes.
9. How do you stage changes for a commit?
Use the command: git add
.
10. How do you commit staged changes?
Use the command: git commit -m "commit message"
.
Intermediate Git Questions
11. What is a merge conflict?
A merge conflict occurs when changes in different branches interfere with each other, requiring manual resolution.
12. How do you resolve a merge conflict?
Edit the conflicting files to resolve differences and then stage the resolved files with git add
.
13. What is a tag in Git?
A tag is a reference to a specific point in the Git history, often used to mark releases.
14. How do you create a tag?
Use the command: git tag
.
15. What is a remote repository?
A remote repository is a version of your project hosted on the internet or network.
16. How do you add a remote repository?
Use the command: git remote add
.
17. How do you view the commit history?
Use the command: git log
.
18. How do you undo the last commit?
Use the command: git reset --soft HEAD~1
.
19. What is the purpose of a .gitignore file?
A .gitignore
file specifies intentionally untracked files that Git should ignore.
20. How do you remove a file from Git without deleting it from the filesystem?
Use the command: git rm --cached
.
Advanced Git Questions
21. What is rebasing in Git?
Rebasing is the process of moving or combining a sequence of commits to a new base commit.
22. How do you perform an interactive rebase?
Use the command: git rebase -i
.
23. What is a Git hook?
Git hooks are scripts that run automatically on specific Git events.
24. How do you create a Git hook?
Place an executable script in the .git/hooks
directory with the name of the hook (e.g., pre-commit).
25. What is the difference between git reset
and git revert
?
git reset
moves the branch pointer backward and can alter commit history. git revert
creates a new commit that undoes changes.
26. How do you squash commits?
Use interactive rebase: git rebase -i
, and change pick to squash for the commits you want to combine.
27. What is git cherry-pick
?
git cherry-pick
applies the changes from a specific commit onto the current branch.
28. How do you apply a patch in Git?
Use the command: git apply
.
29. What is git bisect
?
git bisect
is a tool used to find the commit that introduced a bug by performing a binary search through the commit history.
30. How do you use git bisect
?
Start with git bisect start
, mark a commit as good with git bisect good
, and mark a commit as bad with git bisect bad
.
Expert Git Questions
31. What is Git stash?
Git stash temporarily shelves changes in the working directory that are not ready to be committed.
32. How do you create a stash?
Use the command: git stash
.
33. How do you apply a stash?
Use the command: git stash apply
.
34. How do you list stashes?
Use the command: git stash list
.
35. What is the difference between git merge
and git rebase
?
git merge
combines changes from different branches, creating a merge commit. git rebase
moves or combines a sequence of commits to a new base.
36. How do you force push to a remote repository?
Use the command: git push --force
.
37. What is git reflog
?
git reflog
records changes made to the tip of branches and other references.
38. How do you view the reflog?
Use the command: git reflog
.
39. What is git fsck
?
git fsck
verifies the connectivity and validity of objects in the repository.
40. How do you verify the integrity of a repository?
Use the command: git fsck
.
More Real-time Scenarios and Solutions
41. How do you rename a branch?
- Rename the current branch:
git branch -m new-branch-name
. - Rename a branch from another branch:
git branch -m old-branch-name new-branch-name
.
42. How do you delete a remote branch?
Use the command: git push origin --delete
.
43. How do you ignore changes to a tracked file?
Use the command: git update-index --assume-unchanged
.
44. How do you track changes again to a previously ignored file?
Use the command: git update-index --no-assume-unchanged
.
45. What is a bare repository?
A bare repository is a repository that doesn’t have a working directory and is typically used as a remote repository.
46. How do you create a bare repository?
Use the command: git init --bare
.
47. How do you export a Git repository to a tar file?
Use the command: git archive --format=tar --output=
.
48. How do you import a repository from a tar file?
Use the command: tar -xvf
.
49. How do you view changes made in a specific commit?
Use the command: git show
.
50. How do you undo changes in the working directory?
Use the command: git checkout --
.
Additional Intermediate Questions
51. How do you find a specific commit based on a message?
Use the command: git log --grep="
.
52. How do you set up a global Git configuration?
Use the command: git config --global user.name "Your Name"
and git config --global user.email "[email protected]"
.
53. How do you set up a Git alias?
Use the command: git config --global alias.
.
54. What is the difference between HEAD, FETCH_HEAD, ORIG_HEAD, and MERGE_HEAD?
- HEAD: The current commit your working directory points to.
- FETCH_HEAD: The branch you last fetched from.
- ORIG_HEAD: The original head before performing a destructive operation.
- MERGE_HEAD: The commit(s) you’re merging in.
55. How do you reset a file to a specific commit?
Use the command: git checkout
.
56. How do you compare changes between two branches?
Use the command: git diff
.
57. What is a submodule in Git?
A submodule is a repository embedded inside another repository.
58. How do you add a submodule?
Use the command: git submodule add
.
59. How do you update a submodule?
Use the command: git submodule update --remote
.
60. How do you remove a submodule?
Use the commands:
git submodule deinit -f path/to/submodule
git rm -f path/to/submodule
Additional Advanced Questions
61. How do you create a patch from the last commit?
Use the command: git format-patch -1
.
62. How do you apply a patch file?
Use the command: git apply
.
63. How do you create a custom Git command?
Create an executable script named git-
and place it in your PATH.
64. What is the use of the git rerere
command?
git rerere
stands for "reuse recorded resolution" and helps in reusing conflict resolutions.
65. How do you enable rerere in Git?
Use the command: git config --global rerere.enabled true
.
66. How do you archive a specific branch in Git?
Use the command: git archive --format=zip --output=
.
67. How do you view the differences between the staged and working directory?
Use the command: git diff --cached
.
68. How do you amend a commit with new changes?
Use the command: git commit --amend
.
69. How do you clean the working directory by removing untracked files?
Use the command: git clean -f
.
70. How do you list all the branches merged into the current branch?
Use the command: git branch --merged
.
Git Advanced Real-time Scenarios and Solutions (Continued)
71. How do you list all unmerged branches?
Use the command: git branch --no-merged
.
72. How do you create a new branch and switch to it immediately?
Use the command: git checkout -b
.
73. How do you fetch and merge changes from a remote branch?
Use the command: git pull origin
.
74. How do you list all remote branches?
Use the command: git branch -r
.
75. How do you remove a remote-tracking branch?
Use the command: git branch -dr
.
76. How do you find the author of a specific line in a file?
Use the command: git blame
.
77. How do you compare the working directory with the index?
Use the command: git diff
.
78. How do you compare the index with the last commit?
Use the command: git diff --cached
.
79. How do you view changes made by a specific commit?
Use the command: git show
.
80. How do you rename a remote?
Use the command: git remote rename
.
Git Expert Real-time Scenarios and Solutions
81. How do you set up a Git server?
- Install Git on the server.
- Create a bare repository:
git init --bare /path/to/repo.git
. - Set up SSH access for users.
82. How do you configure Git to use a proxy?
Use the command: git config --global http.proxy http://proxy.example.com:8080
.
83. How do you list the contents of a commit?
Use the command: git ls-tree -r
.
84. How do you list all tags in a repository?
Use the command: git tag
.
85. How do you delete a local tag?
Use the command: git tag -d
.
86. How do you delete a remote tag?
Use the command: git push origin --delete
.
87. How do you view the differences between two commits?
Use the command: git diff
.
88. How do you bisect to find a bug?
- Start with:
git bisect start
. - Mark the current commit as bad:
git bisect bad
. - Mark an earlier commit as good:
git bisect good
. - Continue marking commits until the bad commit is found.
89. How do you rebase a branch onto another branch?
Use the command: git rebase
.
90. How do you resolve rebase conflicts?
- Edit the conflicting files.
- Use
git add
to stage the resolved files. - Continue the rebase:
git rebase --continue
.
More Real-time Scenarios and Solutions
91. How do you create a patch for a specific commit?
Use the command: git format-patch
.
92. How do you apply a patch from a specific commit?
Use the command: git apply
.
93. How do you list all commits that changed a specific file?
Use the command: git log --
.
94. How do you view the Git configuration?
Use the command: git config --list
.
95. How do you change the remote URL of a repository?
Use the command: git remote set-url
.
96. How do you find a specific commit by hash?
Use the command: git show
.
97. How do you view a file from a specific commit?
Use the command: git show
.
98. How do you checkout a specific file from another branch?
Use the command: git checkout
.
99. How do you discard all local changes and start fresh?
Use the command: git reset --hard
.
100. How do you list all files changed in a specific commit?
Use the command: git diff-tree --no-commit-id --name-only -r
.
Git Best Practices
101. Commit often with meaningful messages.
Regular commits with clear, concise messages make it easier to track changes and understand the project's history.
102. Branch out for new features and bug fixes.
Using branches for different tasks keeps your main branch clean and organized.
103. Regularly pull from the remote repository.
Keeping your local repository up-to-date with the remote repository helps avoid conflicts.
104. Use tags to mark release points.
Tags provide a way to mark specific points in your project’s history, making it easy to find releases.
105. Keep your repository clean.
Regularly remove old branches, clean up the working directory, and remove unnecessary files.
106. Review your changes before committing.
Use git diff
to review your changes and ensure that only relevant changes are included in the commit.
107. Squash commits before merging.
Squashing commits can simplify your history by combining related changes into a single commit.
108. Avoid committing sensitive information.
Use .gitignore
to prevent sensitive files from being committed to the repository.
109. Use pull requests for code reviews.
Pull requests provide a structured way to review changes before they are merged into the main branch.
110. Backup your repository regularly.
Regular backups ensure that you have a copy of your repository in case of data loss.
Git Real-time Scenarios and Solutions
111. How do you undo the last commit without losing the changes?
Use the command: git reset --soft HEAD~1
.
112. How do you squash multiple commits into one?
- Start an interactive rebase:
git rebase -i HEAD~
. - In the editor, change
pick
tosquash
for the commits you want to squash. - Save and close the editor.
113. How do you remove sensitive data from the repository history?
Use the BFG Repo-Cleaner tool or the command: git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch
.
114. How do you stage parts of a changed file?
Use the command: git add -p
.
115. How do you set up a pre-commit hook?
- Create a file named
pre-commit
in the.git/hooks/
directory. - Add your script to this file.
- Make it executable:
chmod +x .git/hooks/pre-commit
.
116. How do you handle binary files in Git?
Add binary files to .gitattributes
with the binary
attribute to handle them properly, for example: *.png binary
.
117. How do you ignore changes to a tracked file?
Use the command: git update-index --assume-unchanged
.
118. How do you reapply a stash after resolving conflicts?
- Apply the stash:
git stash apply
. - Resolve conflicts.
- Mark them as resolved:
git add
. - Continue with:
git stash drop
.
119. How do you undo a rebase?
Use the command: git reflog
to find the commit before the rebase, then git reset --hard
.
120. How do you split a commit into multiple commits?
- Use the command:
git reset HEAD~
. - Add changes for the first commit:
git add -p
. - Commit the changes:
git commit
. - Repeat for remaining changes.
Advanced Git Configurations
121. How do you configure Git to use different user identities for different repositories?
Use the command in the repository directory: git config user.name "Your Name"
, git config user.email "[email protected]"
.
122. How do you enable Git to automatically correct mistyped commands?
Use the command: git config --global help.autocorrect 1
.
123. How do you configure Git to handle line endings correctly on different OS?
Use the command: git config --global core.autocrlf true
on Windows, input
on macOS and Linux.
124. How do you speed up Git operations in large repositories?
Use Git’s partial clone feature: git clone --filter=blob:none
.
125. How do you show a log of all merges in the repository?
Use the command: git log --merges
.
126. How do you configure a global Git ignore file?
- Create a global
.gitignore
file. - Configure Git to use it:
git config --global core.excludesfile ~/.gitignore
.
127. How do you change the default text editor for Git?
Use the command: git config --global core.editor "editor"
.
128. How do you configure Git to remember your password?
Use the command: git config --global credential.helper cache
.
129. How do you compress the repository to save space?
Use the command: git gc --aggressive --prune=now
.
130. How do you automatically sign commits with GPG?
Use the command: git config --global commit.gpgSign true
.
Git Troubleshooting
131. How do you fix a detached HEAD state?
Use the command: git checkout
or create a new branch from the detached state: git checkout -b
.
132. How do you recover a deleted branch?
Use the command: git reflog
to find the commit, then git checkout -b
.
133. How do you fix a repository with broken references?
Use the command: git fsck
to identify the issue, then git prune
and git gc
to clean up.
134. How do you resolve a "fatal: reference is not a tree" error?
Fetch the missing objects: git fetch --all
.
135. How do you recover from an "index file corrupt" error?
Remove the index file: rm -f .git/index
, then git reset
.
136. How do you deal with "too many open files" errors?
Increase the file descriptor limit: ulimit -n 4096
.
137. How do you fix a "repository not found" error?
Check the repository URL and your access permissions, then use git remote set-url
to update if necessary.
138. How do you fix slow Git performance?
Optimize the repository: git repack -Ad
and git gc --aggressive
.
139. How do you resolve a "branch already exists" error when creating a new branch?
Delete the existing branch: git branch -d
or force delete: git branch -D
.
140. How do you troubleshoot a Git hook not executing?
Ensure the hook script is executable: chmod +x .git/hooks/
.
Git Hooks
141. What are Git hooks?
Git hooks are custom scripts that run at specific points in the Git workflow, such as before or after a commit, push, or merge.
142. How do you set up a pre-commit hook to check code style?
- Create a
pre-commit
file in the.git/hooks/
directory. - Add your code style check script.
- Make it executable:
chmod +x .git/hooks/pre-commit
.
143. How do you set up a post-merge hook to run tests?
- Create a
post-merge
file in the.git/hooks/
directory. - Add your test run script.
- Make it executable:
chmod +x .git/hooks/post-merge
.
144. How do you disable a specific Git hook?
Rename the hook script in the .git/hooks/
directory, e.g., mv pre-commit pre-commit.old
.
145. How do you create a hook to send an email after a push?
- Create a
post-receive
file in the.git/hooks/
directory of the remote repository. - Add your email sending script.
- Make it executable:
chmod +x .git/hooks/post-receive
.
146. How do you set up a client-side hook to prevent committing sensitive files?
- Create a
pre-commit
file in the.git/hooks/
directory. - Add a script to check for sensitive files and prevent the commit.
- Make it executable:
chmod +x .git/hooks/pre-commit
.
147. How do you run a hook only once?
Use the command: rm -f .git/hooks/
within the hook script after its execution.
148. How do you share Git hooks with your team?
- Place the hooks in a directory, e.g.,
.githooks/
. - Set the core.hooksPath configuration:
git config core.hooksPath .githooks
.
149. How do you debug a Git hook?
Add debug statements (e.g., echo "Debug message"
) to the hook script and check the output when the hook runs.
150. How do you set up a server-side hook to enforce branch naming conventions?
- Create a
pre-receive
orupdate
hook in the.git/hooks/
directory of the remote repository. - Add your script to check branch names.
- Make it executable:
chmod +x .git/hooks/
.
Git Advanced Features
151. How do you use Git Submodules?
- Add a submodule:
git submodule add
. - Initialize and update submodules:
git submodule update --init --recursive
.
152. How do you update Git Submodules?
- Navigate to the submodule directory.
- Pull the latest changes:
git pull
. - Return to the main repository and commit the changes.
153. How do you use Git Worktrees?
- Create a new worktree:
git worktree add
. - Navigate to the new worktree and start working.
154. How do you archive a Git repository?
Use the command: git archive --format=tar --output=
.
155. How do you cherry-pick multiple commits?
Use the command: git cherry-pick
.
156. How do you create a patch from a commit?
Use the command: git format-patch -1
.
157. How do you apply a patch?
Use the command: git apply
or git am
for email patches.
158. How do you use Git Bisect to find a bug?
- Start bisect:
git bisect start
. - Mark the current commit as bad:
git bisect bad
. - Mark an older commit as good:
git bisect good
. - Git will checkout commits in between for testing. Use
git bisect good
orgit bisect bad
until the faulty commit is found. - End bisect:
git bisect reset
.
159. How do you use Git Reflog to recover lost commits?
Use the command: git reflog
to find the commit, then git checkout
or git reset --hard
.
160. How do you handle large files in Git?
Use Git LFS (Large File Storage):
- Install Git LFS:
git lfs install
. - Track large files:
git lfs track
. - Add and commit the files as usual.
161. How do you configure Git to perform shallow clones?
Use the command: git clone --depth
.
162. How do you verify the integrity of a Git repository?
Use the command: git fsck
.
163. How do you use Git Replace to rewrite history?
- Create a replacement:
git replace
. - View the replacement:
git log --replace
.
164. How do you graft commits in Git?
Use the command: git replace --graft
.
165. How do you convert a SVN repository to Git?
Use the git svn
command:
- Clone the SVN repository:
git svn clone
.--no-metadata -A authors.txt --stdlayout - Navigate to the cloned directory and fetch updates:
git svn fetch
.
166. How do you use Git attributes to control file handling?
Create a .gitattributes
file and specify attributes for files, e.g., *.txt text
to treat all .txt
files as text.
167. How do you perform a shallow fetch?
Use the command: git fetch --depth
.
168. How do you manage Git notes?
- Add a note:
git notes add -m "note"
. - Show notes:
git log --show-notes
.
169. How do you use Git sparse checkout?
- Enable sparse checkout:
git config core.sparseCheckout true
. - Define the files to checkout:
echo "path/to/file" >> .git/info/sparse-checkout
. - Perform the checkout:
git read-tree -mu HEAD
.
170. How do you enable Git rerere to reuse recorded resolution?
Use the command: git config --global rerere.enabled true
.
171. How do you stash changes temporarily?
Stash changes temporarily using git stash
.
172. How do you apply a specific stash?
Apply a specific stash using git stash apply
.
173. How do you list all stashes?
List all stashes using git stash list
.
174. How do you delete a specific stash?
Delete a specific stash using git stash drop
.
175. How do you clear all stashes?
Clear all stashes using git stash clear
.
176. How do you show the commit history graphically?
Show the commit history graphically using git log --graph
.
177. How do you display the commit history of a specific file?
Display the commit history of a specific file using git log --
.
178. How do you show the changes introduced in each commit?
Show the changes introduced in each commit using git log -p
.
179. How do you find the branches containing a specific commit?
Find the branches containing a specific commit using git branch --contains
.
180. How do you list all remote branches?
List all remote branches using git branch -r
.
181. How do you fetch changes from a remote repository?
Fetch changes from a remote repository using git fetch
.
182. How do you pull changes from a remote repository?
Pull changes from a remote repository using git pull
.
183. How do you push changes to a remote repository?
Push changes to a remote repository using git push
.
184. How do you force-push changes to a remote repository?
Force-push changes to a remote repository using git push
.
185. How do you delete a remote branch?
Delete a remote branch using git push
.
186. How do you revert a specific file to a previous commit?
Revert a specific file to a previous commit using git checkout
.
187. How do you reset the HEAD pointer to a previous commit?
Reset the HEAD pointer to a previous commit using git reset --hard
.
188. How do you undo the last commit but keep changes staged?
Undo the last commit but keep changes staged using git reset --soft HEAD^
.
189. How do you undo the last commit and unstage changes?
Undo the last commit and unstage changes using git reset HEAD^
.
190. How do you delete untracked files in Git?
Delete untracked files in Git using git clean -f
.