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.

Top 200+ Basic to Advance Git Interview Questions & Answers [2024]

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=.tar .

48. How do you import a repository from a tar file?

Use the command: tar -xvf .tar.

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=.zip .

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?

  1. Install Git on the server.
  2. Create a bare repository: git init --bare /path/to/repo.git.
  3. 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?

  1. Start with: git bisect start.
  2. Mark the current commit as bad: git bisect bad.
  3. Mark an earlier commit as good: git bisect good .
  4. 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?

  1. Edit the conflicting files.
  2. Use git add to stage the resolved files.
  3. 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?

  1. Start an interactive rebase: git rebase -i HEAD~.
  2. In the editor, change pick to squash for the commits you want to squash.
  3. 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 ' --prune-empty --tag-name-filter cat -- --all.

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?

  1. Create a file named pre-commit in the .git/hooks/ directory.
  2. Add your script to this file.
  3. 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?

  1. Apply the stash: git stash apply.
  2. Resolve conflicts.
  3. Mark them as resolved: git add .
  4. 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?

  1. Use the command: git reset HEAD~.
  2. Add changes for the first commit: git add -p.
  3. Commit the changes: git commit.
  4. 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?

  1. Create a global .gitignore file.
  2. 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?

  1. Create a pre-commit file in the .git/hooks/ directory.
  2. Add your code style check script.
  3. Make it executable: chmod +x .git/hooks/pre-commit.

143. How do you set up a post-merge hook to run tests?

  1. Create a post-merge file in the .git/hooks/ directory.
  2. Add your test run script.
  3. 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?

  1. Create a post-receive file in the .git/hooks/ directory of the remote repository.
  2. Add your email sending script.
  3. Make it executable: chmod +x .git/hooks/post-receive.

146. How do you set up a client-side hook to prevent committing sensitive files?

  1. Create a pre-commit file in the .git/hooks/ directory.
  2. Add a script to check for sensitive files and prevent the commit.
  3. 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?

  1. Place the hooks in a directory, e.g., .githooks/.
  2. 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?

  1. Create a pre-receive or update hook in the .git/hooks/ directory of the remote repository.
  2. Add your script to check branch names.
  3. Make it executable: chmod +x .git/hooks/.

Git Advanced Features

151. How do you use Git Submodules?

  1. Add a submodule: git submodule add .
  2. Initialize and update submodules: git submodule update --init --recursive.

152. How do you update Git Submodules?

  1. Navigate to the submodule directory.
  2. Pull the latest changes: git pull.
  3. Return to the main repository and commit the changes.

153. How do you use Git Worktrees?

  1. Create a new worktree: git worktree add .
  2. Navigate to the new worktree and start working.

154. How do you archive a Git repository?

Use the command: git archive --format=tar --output=.tar .

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?

  1. Start bisect: git bisect start.
  2. Mark the current commit as bad: git bisect bad.
  3. Mark an older commit as good: git bisect good .
  4. Git will checkout commits in between for testing. Use git bisect good or git bisect bad until the faulty commit is found.
  5. 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):

  1. Install Git LFS: git lfs install.
  2. Track large files: git lfs track .
  3. 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?

  1. Create a replacement: git replace .
  2. 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:

  1. Clone the SVN repository: git svn clone --no-metadata -A authors.txt --stdlayout .
  2. 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?

  1. Add a note: git notes add -m "note" .
  2. Show notes: git log --show-notes.

169. How do you use Git sparse checkout?

  1. Enable sparse checkout: git config core.sparseCheckout true.
  2. Define the files to checkout: echo "path/to/file" >> .git/info/sparse-checkout.
  3. 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 --force.

185. How do you delete a remote branch?

Delete a remote branch using git push --delete .

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.

191. How do you undo the last commit and discard changes?

Undo the last commit and discard changes using git reset --hard HEAD^.

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

Create a new branch and switch to it using git checkout -b .

193. How do you rename a branch?

Rename a branch using git branch -m .

194. How do you merge two branches in Git?

Merge two branches in Git using git merge .

195. How do you resolve merge conflicts in Git?

Resolve merge conflicts in Git by editing the conflicted files, marking conflicts as resolved with git add, and then completing the merge with git commit.

196. How do you abort a merge in Git?

Abort a merge in Git using git merge --abort.

197. How do you rebase a branch onto another branch?

Rebase a branch onto another branch using git rebase while on the branch you want to rebase.

198. How do you continue a rebase after resolving conflicts?

Continue a rebase after resolving conflicts by adding the resolved files with git add and then using git rebase --continue.

199. How do you abort a rebase in Git?

Abort a rebase in Git using git rebase --abort.

200. How do you cherry-pick a commit from one branch to another?

Cherry-pick a commit from one branch to another using git cherry-pick .

201. How do you cherry-pick multiple commits?

Cherry-pick multiple commits using git cherry-pick ....

202. How do you cherry-pick a range of commits?

Cherry-pick a range of commits using git cherry-pick ^...

203. How do you stash changes and switch branches?

Stash changes and switch branches using git stash followed by git checkout .

204. How do you stash changes and apply them to a different branch?

Stash changes, switch branches, and apply the stash using git stash branch .

205. How do you create a Git tag?

Create a Git tag using git tag .

206. How do you list all Git tags?

List all Git tags using git tag.

207. How do you delete a Git tag?

Delete a Git tag using git tag -d .

208. How do you push tags to a remote repository?

Push tags to a remote repository using git push .

209. How do you delete a remote Git tag?

Delete a remote Git tag using git push --delete .

210. How do you create a Git alias?

Create a Git alias using git config --global alias. .

211. How do you squash multiple commits into one?

Squash multiple commits into one using an interactive rebase: git rebase -i and then replacing "pick" with "squash" for the commits you want to squash.

212. How do you edit the last commit message?

Edit the last commit message using git commit --amend.

213. How do you revert a commit without creating a new commit?

Revert a commit without creating a new commit using git revert -n .

214. How do you see the changes introduced by a commit?

See the changes introduced by a commit using git show .

215. How do you move a commit from one branch to another?

Move a commit from one branch to another using cherry-pick: git cherry-pick .

216. How do you change the author of a commit?

Change the author of a commit using git commit --amend --author="New Author Name ".

217. How do you remove untracked files from the working directory?

Remove untracked files from the working directory using git clean -f.

218. How do you show the changes between two commits?

Show the changes between two commits using git diff .

219. How do you compare the changes between branches?

Compare the changes between branches using git diff ...

220. How do you check out a specific commit?

Check out a specific commit using git checkout .

221. How do you show the history of a file?

Show the history of a file using git log --follow .

222. How do you show the branches that contain a specific commit?

Show the branches that contain a specific commit using git branch --contains .

223. How do you list the files changed in a commit?

List the files changed in a commit using git show --name-only .

224. How do you discard changes to a specific file?

Discard changes to a specific file using git checkout -- .

225. How do you apply changes from one branch to another?

Apply changes from one branch to another using git cherry-pick or merging the branches.

226. How do you check the status of the Git repository?

Check the status of the Git repository using git status.

227. How do you show the commit graph in a repository?

Show the commit graph in a repository using git log --graph.

228. How do you list all remote branches?

List all remote branches using git branch -r.

229. How do you list all branches, including remote branches?

List all branches, including remote branches using git branch -a.

230. How do you set up a tracking branch?

Set up a tracking branch using git branch --set-upstream-to=/ .

231. How do you list all tracked files in a repository?

List all tracked files in a repository using git ls-files.

232. How do you show the last N commits in a branch?

Show the last N commits in a branch using git log -n .

233. How do you show the branches that have been merged into another branch?

Show the branches that have been merged into another branch using git branch --merged .

234. How do you show the commits that are not merged into another branch?

Show the commits that are not merged into another branch using git log ^.

235. How do you revert a merge commit?

Revert a merge commit using git revert -m 1 .

236. How do you create a new repository on GitHub?

Create a new repository on GitHub by clicking on the "New" button in your GitHub account and following the instructions.

237. How do you clone a repository from GitHub?

Clone a repository from GitHub using git clone .

238. How do you push changes to a remote repository?

Push changes to a remote repository using git push .

239. How do you pull changes from a remote repository?

Pull changes from a remote repository using git pull .

240. How do you set up SSH keys for Git authentication?

Set up SSH keys for Git authentication by generating SSH keys and adding them to your GitHub or GitLab account.