Friends Git is a powerful tool that helps developers track changes in their code and collaborate with others. It is widely used in software development and is an important skill for anyone working with code. If you are preparing for a Git interview 2025, you need to be familiar with common Git commands, workflows, and troubleshooting methods.
In this article, we will cover some of the Git Interview Questions 2025 and their answers. Whether you are a beginner or have some experience, these questions will help you understand Git better and boost your confidence for the interview.
Contents
- 1 Git Interview Questions 2025
- 1.1 1. What is Git?
- 1.2 2. What is the difference between Git and GitHub?
- 1.3 3. What is a Git repository?
- 1.4 4. How do you create a new Git repository?
- 1.5 5. What is a commit in Git?
- 1.6 6. How do you check the status of your Git repository?
- 1.7 7. How do you add files to a commit?
- 1.8 8. How do you commit changes?
- 1.9 9. How do you check the commit history?
- 1.10 10. How do you create a new branch in Git?
- 1.11 11. How do you switch to another branch?
- 1.12 12. How do you merge branches in Git?
- 1.13 13. What is a conflict in Git, and how do you resolve it?
- 1.14 14. How do you push changes to a remote repository?
- 1.15 15. How do you pull the latest changes from a remote repository?
- 2 Git Interview Questions For Freshers
- 2.1 16. What are the advantages of using Git?
- 2.2 17. What is the difference between git clone and git fork?
- 2.3 18. How do you remove a file from Git without deleting it from the local system?
- 2.4 19. What is the purpose of the .gitignore file?
- 2.5 20. What is a detached HEAD in Git?
- 2.6 21. How do you rename a branch in Git?
- 2.7 22. What is the difference between git fetch and git pull?
- 2.8 23. What is Git Stash?
- 2.9 24. How do you apply and remove a stashed change?
- 2.10 25. What is rebase in Git?
- 2.11 26. How do you revert a commit in Git?
- 2.12 27. What is the difference between git reset and git revert?
- 2.13 28. How do you undo the last commit without losing changes?
- 2.14 29. What is the difference between git merge and git rebase?
- 2.15 30. How do you check which files have changed between two commits?
- 3 Git Interview Questions For Experienced
- 3.1 31. What is Git cherry-pick, and when would you use it?
- 3.2 32. How do you squash multiple commits into one?
- 3.3 33. How do you undo a pushed commit?
- 3.4 34. How do you find and delete merged branches?
- 3.5 35. What is Git Bisect, and how does it help in debugging?
- 3.6 36. How do you set up and use Git hooks?
- 3.7 37. What is the difference between git pull --rebase and git pull?
- 3.8 38. What is the difference between git stash save and git stash push?
- 3.9 39. How do you resolve conflicts in a rebase?
- 3.10 40. How do you track only specific files in a repository?
- 3.11 41. What is Git Worktree, and how is it useful?
- 3.12 42. How do you remove a file from history in Git?
- 3.13 43. How do you revert a Git repository to a previous state without losing history?
- 3.14 44. How do you verify who made changes to a file in Git?
- 3.15 45. What is the difference between git reflog and git log?
- 4 Git Interview Questions Scenario Based
- 4.1 46. You committed sensitive information (e.g., API keys) in a previous commit. How do you completely remove it from the history?
- 4.2 47. Your local branch is behind the remote branch, and you have local changes. How do you safely update your branch?
- 4.3 48. You accidentally deleted a branch. How do you recover it?
- 4.4 49. You need to compare the differences between two branches before merging. How do you do it?
- 4.5 50. Your team is working on a feature, but the main branch has critical bug fixes. How do you integrate them without merging?
- 4.6 51. You mistakenly committed a file to the wrong branch. How do you move it to the correct branch?
- 4.7 52. Your team is working on the same file, and a merge conflict occurs. How do you resolve it?
- 4.8 53. You need to find a specific commit where a bug was introduced. How do you do it efficiently?
- 4.9 54. You need to temporarily work on a different task but don’t want to lose your current changes. What do you do?
- 4.10 55. Your repository size has grown too large due to unnecessary files. How do you reduce its size?
Git Interview Questions 2025
1. What is Git?
Answer: Git is a version control system that helps developers track changes in their code, collaborate with others, and manage different versions of a project efficiently.
Explanation: It allows multiple people to work on a project without overwriting each other’s changes and provides a history of modifications.
2. What is the difference between Git and GitHub?
Answer: Git is a tool for version control, while GitHub is an online platform for hosting Git repositories.
Explanation: Git is used locally to manage code, while GitHub allows teams to share and collaborate on projects using Git.
3. What is a Git repository?
Answer: A Git repository is a storage space where all the files, commits, and history of a project are kept.
Explanation: It can be local (on your computer) or remote (on GitHub, GitLab, or Bitbucket).
4. How do you create a new Git repository?
Answer: Run the command:
git init
Explanation: This initializes a new Git repository in the current directory.
5. What is a commit in Git?
Answer: A commit is a snapshot of changes in a repository.
Explanation: Every commit has a unique ID (SHA) and helps track modifications over time.
6. How do you check the status of your Git repository?
Answer: Use the command:
git status
Explanation: It shows which files have been modified, added, or are untracked.
7. How do you add files to a commit?
Answer: Use the command:
git add <filename>
or
git add .
Explanation: This stages files before committing them.
8. How do you commit changes?
Answer: Use the command:
git commit -m "Your commit message"
Explanation: This saves the staged changes with a message describing what was changed.
9. How do you check the commit history?
Answer: Use the command:
git log
Explanation: It shows a list of commits with details like author, date, and commit message.
10. How do you create a new branch in Git?
Answer: Use the command:
git branch <branch-name>
Explanation: Branches allow multiple versions of a project to be worked on separately.
11. How do you switch to another branch?
Answer: Use the command:
git checkout <branch-name>
or
git switch <branch-name>
Explanation: This changes the working directory to another branch.
12. How do you merge branches in Git?
Answer: Use the command:
git merge <branch-name>
Explanation: This combines the changes from one branch into another.
13. What is a conflict in Git, and how do you resolve it?
Answer: A conflict occurs when two branches have changes in the same file, and Git doesn’t know which version to keep.
Explanation: To resolve it, open the conflicting file, manually choose the correct changes, then commit the resolved file.
14. How do you push changes to a remote repository?
Answer: Use the command:
git push origin <branch-name>
Explanation: This uploads local commits to a remote repository like GitHub.
15. How do you pull the latest changes from a remote repository?
Answer: Use the command:
git pull origin <branch-name>
Explanation: This updates your local repository with the latest changes from the remote branch.
Git Interview Questions For Freshers
16. What are the advantages of using Git?
Answer:
- Tracks changes in code
- Supports collaboration among multiple developers
- Provides branching and merging features
- Allows rollback to previous versions
- Works offline
Explanation: Git helps in efficient software development by keeping a complete history of project changes and enabling teamwork.
17. What is the difference between git clone
and git fork
?
Answer:
git clone
copies an existing repository to your local machine.git fork
creates a copy of someone else’s repository under your GitHub account for independent development.
Explanation: Cloning is for direct work, while forking is for making independent changes to another repository.
18. How do you remove a file from Git without deleting it from the local system?
Answer: Use the command:
git rm --cached <filename>
Explanation: This removes the file from Git tracking but keeps it in your local directory
19. What is the purpose of the .gitignore
file?
Answer: It tells Git which files or directories to ignore and not track.
Explanation: This is useful for excluding temporary files, logs, or sensitive files from version control.
20. What is a detached HEAD in Git?
Answer: It means Git is in a state where you’re not working on a branch but a specific commit.
Explanation: Changes made in a detached HEAD state may be lost unless committed to a branch.
21. How do you rename a branch in Git?
Answer:
For renaming the current branch:
git branch -m <new-name>
For renaming another branch:
git branch -m <old-name> <new-name>
Explanation: This is helpful when you need a better name for an existing branch.
22. What is the difference between git fetch
and git pull
?
Answer:
git fetch
downloads changes from a remote repository but does not apply them.git pull
downloads and merges changes into the current branch.
Explanation: Fetch is safer if you want to review changes before applying them.
23. What is Git Stash?
Answer: Git Stash temporarily saves uncommitted changes without committing them.
Explanation: This is useful when you need to switch branches without losing work in progress.
24. How do you apply and remove a stashed change?
Answer:
- To apply:
git stash apply
- To remove after applying:
git stash drop
Explanation: This allows you to temporarily save and retrieve unfinished work.
25. What is rebase in Git?
Answer: Rebase moves or combines commits from one branch onto another.
Explanation: It helps maintain a clean history by avoiding unnecessary merge commits.
26. How do you revert a commit in Git?
Answer: Use the command:
git revert <commit-hash>
Explanation: This creates a new commit that undoes the changes from the specified commit without removing history.
27. What is the difference between git reset
and git revert
?
Answer:
git reset
removes commits permanently.git revert
creates a new commit that undoes changes but keeps history intact.
Explanation: Use git revert
for a safer way to undo changes without losing commit history.
28. How do you undo the last commit without losing changes?
Answer: Use the command:
git reset --soft HEAD~1
Explanation: This keeps the changes staged while removing the last commit.
29. What is the difference between git merge
and git rebase
?
Answer:
git merge
combines two branches and keeps the commit history.git rebase
moves changes from one branch to another, creating a linear history.
Explanation: Use merge for preserving history and rebase for cleaner commit structures.
30. How do you check which files have changed between two commits?
Answer: Use the command:
git diff <commit1> <commit2>
Explanation: This shows the differences in files between two commits, helping in code review and debugging.
Git Interview Questions For Experienced
31. What is Git cherry-pick, and when would you use it?
Answer:
git cherry-pick <commit-hash>
Cherry-pick applies a specific commit from one branch to another.
Explanation: This is useful when you need to apply a single commit’s changes without merging an entire branch.
32. How do you squash multiple commits into one?
Answer:
Use interactive rebase:
git rebase -i HEAD~<number-of-commits>
Then change pick
to squash (s)
for commits you want to merge.
Explanation: Squashing helps clean up commit history by combining multiple commits into one meaningful change.
33. How do you undo a pushed commit?
Answer:
To remove the last pushed commit:
git reset --hard HEAD~1
git push --force
Explanation: Be cautious with --force
, as it overwrites history and affects other collaborators.
34. How do you find and delete merged branches?
Answer:
Find merged branches:
git branch --merged
Delete a merged branch:
git branch -d <branch-name>
Explanation: Cleaning up merged branches keeps the repository organized and reduces clutter.
35. What is Git Bisect, and how does it help in debugging?
Answer:
Use:
git bisect start
git bisect bad <latest-bad-commit>
git bisect good <last-working-commit>
Git Bisect helps find the commit that introduced a bug using binary search.
Explanation: This is useful when dealing with large codebases where finding the faulty commit manually is difficult.
36. How do you set up and use Git hooks?
Answer:
Navigate to .git/hooks/
and add a script file like pre-commit
:
#!/bin/sh
echo "Running pre-commit hook"
exit 1
Then make it executable:
chmod +x .git/hooks/pre-commit
Explanation: Git hooks automate tasks like code formatting, running tests, or enforcing commit messages.
37. What is the difference between git pull --rebase
and git pull
?
Answer:
git pull
merges remote changes with a new merge commit.git pull --rebase
applies remote changes on top of local commits, keeping history linear.
Explanation: Use rebase
to avoid unnecessary merge commits when working on feature branches.
38. What is the difference between git stash save
and git stash push
?
Answer:
git stash save "message"
(deprecated) saves changes but doesn’t allow naming.git stash push -m "message"
saves changes with a message for better tracking.
Explanation: git stash push
is the recommended approach in newer Git versions.
39. How do you resolve conflicts in a rebase?
Answer:
During a rebase conflict, Git pauses and asks you to resolve it manually. After resolving, run:
git add <file>
git rebase --continue
Explanation: Unlike merging, rebase modifies commit history, so resolving conflicts carefully is crucial.
40. How do you track only specific files in a repository?
Answer:
Use sparse-checkout
:
git sparse-checkout init
git sparse-checkout set <file-or-folder>
Explanation: This helps when working with large repositories where downloading everything is unnecessary.
41. What is Git Worktree, and how is it useful?
Answer:
To create a separate working directory for another branch:
git worktree add ../new-folder branch-name
Explanation: Worktrees allow working on multiple branches simultaneously without switching in the same directory.
42. How do you remove a file from history in Git?
Answer:
To remove a file from the entire Git history:
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch <file>' --prune-empty --tag-name-filter cat -- --all
Explanation: This is useful for removing sensitive files from past commits.
43. How do you revert a Git repository to a previous state without losing history?
Answer:
Use:
git revert <commit-hash>
Explanation: git revert
creates a new commit that undoes changes from a specific commit without erasing history.
44. How do you verify who made changes to a file in Git?
Answer:
Use:
git blame <filename>
Explanation: This command shows line-by-line authorship, which is useful for tracking changes in a project.
45. What is the difference between git reflog
and git log
?
Answer:
git log
shows the commit history.git reflog
tracks all branch movements, including resets and checkouts.
Explanation: git reflog
is useful for recovering lost commits after accidental resets or deletions.
Git Interview Questions Scenario Based
46. You committed sensitive information (e.g., API keys) in a previous commit. How do you completely remove it from the history?
Answer:
Use:
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch <filename>' --prune-empty --tag-name-filter cat -- --all
Or use BFG Repo-Cleaner
:
bfg --delete-files <filename>
Then force push:
git push --force
Explanation: This completely removes the file from Git history, preventing it from being accessed in older commits.
47. Your local branch is behind the remote branch, and you have local changes. How do you safely update your branch?
Answer:
First, stash your changes:
git stash
Then, update the branch:
git pull --rebase origin <branch-name>
Finally, apply your changes:
git stash pop
Explanation: This avoids unnecessary merge commits and ensures your local changes are reapplied on top of the latest remote changes.
48. You accidentally deleted a branch. How do you recover it?
Answer:
Find the last commit of the deleted branch:
git reflog
Restore it:
git checkout -b <branch-name> <commit-hash>
Explanation: git reflog
helps track recent actions, and using it can recover lost branches.
49. You need to compare the differences between two branches before merging. How do you do it?
Answer:
Use:
git diff <branch-1> <branch-2>
For a more visual comparison, use:
git log --oneline --graph --decorate --all --abbrev-commit --boundary <branch-1>..<branch-2>
Explanation: This allows you to review changes before merging, reducing the risk of conflicts.
50. Your team is working on a feature, but the main branch has critical bug fixes. How do you integrate them without merging?
Answer:
Use:
git rebase main
If conflicts arise, resolve them, then continue:
git rebase --continue
Explanation: Rebasing integrates the latest changes into your feature branch while keeping a clean commit history.
51. You mistakenly committed a file to the wrong branch. How do you move it to the correct branch?
Answer:
Reset the commit:
git reset HEAD~1
Switch to the correct branch:
git checkout <correct-branch>
Reapply the commit:
git commit -m "Correcting commit to the right branch"
Explanation: This allows you to remove a commit from one branch and place it on another.
52. Your team is working on the same file, and a merge conflict occurs. How do you resolve it?
Answer:
View conflicting files:
git status
Open the file, manually resolve conflicts, then:
git add <file>
git commit -m "Resolved merge conflict"
Explanation: Git marks conflicting sections, so you must choose the correct changes before committing.
53. You need to find a specific commit where a bug was introduced. How do you do it efficiently?
Answer:
Use Git Bisect:
git bisect start
git bisect bad <latest-buggy-commit>
git bisect good <last-working-commit>
Git will check commits between them, allowing you to find the exact commit that caused the issue.
Explanation: This is an efficient way to locate problematic commits in large projects.
54. You need to temporarily work on a different task but don’t want to lose your current changes. What do you do?
Answer:
Stash your changes:
git stash push -m "Saving current work"
Later, retrieve them:
git stash list
git stash apply <stash-id>
Explanation: Stashing allows you to save your work temporarily without committing unfinished changes.
55. Your repository size has grown too large due to unnecessary files. How do you reduce its size?
Answer:
Find large files:
git rev-list --objects --all | sort -k 2 -r | head -n 10
Remove them from history:
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch <large-file>'
Then force push:
git push --force --prune
Explanation: This removes large or unwanted files from Git history, reducing repository size.