This guide provides a curated list of common Git interview questions to help you prepare for your next developer role. Master these concepts to demonstrate your expertise in version control and collaborative software development.
Last Updated: Aug 21, 2025
Table of Contents
Core Concepts (1-10)
1. What is Git?
Git is a distributed version control system (DVCS) designed to handle everything from small to very large projects with speed and efficiency. It's used for tracking changes in source code, enabling multiple developers to work together on non-linear development. You can learn more from the official Git website.
2. What is the difference between Git and GitHub?
- Git is the version control tool itself. It's a command-line tool that runs locally on your machine to manage code history.
- GitHub (along with GitLab, Bitbucket, etc.) is a web-based hosting service for Git repositories. It provides a graphical interface and adds collaboration features on top of Git, such as pull requests, issue tracking, and team management.
3. What is a commit in Git?
A commit is a snapshot of your repository at a specific point in time. Each commit has a unique ID (a SHA-1 hash) and contains a reference to the parent commit(s), creating a linked history. A commit message describing the changes is also included.
4. What are the three main states of a file in Git?
- Modified: The file has been changed but not yet committed to the database.
- Staged: A modified file has been marked in its current version to go into the next commit snapshot. This happens when you run
git add
. - Committed: The data is safely stored in your local database. This happens when you run
git commit
.
5. What is a repository in Git?
A Git repository (or "repo") is a collection of files and folders along with their complete version history. It contains all the commits, branches, and metadata that Git uses to manage the project. Repositories can be local (on your machine) or remote (hosted on a server like GitHub).
6. What is the difference between Git and SVN?
- Git is a distributed version control system where each developer has a complete copy of the repository, including its full history.
- SVN (Subversion) is a centralized version control system where developers check out files from a central server and commit changes back to that server.
- Git allows for offline work and faster operations, while SVN requires a network connection for most operations.
7. What is the purpose of the .git directory?
The .git directory is where Git stores all the metadata and object database for your project. It contains information about commits, references (branches and tags), configuration, and more. This directory is what makes a directory a Git repository.
8. What is a SHA-1 hash in Git?
A SHA-1 hash is a 40-character checksum that Git uses to uniquely identify commits, trees, blobs, and tags. It's calculated based on the content of the object and its metadata, ensuring that each object has a unique identifier that changes if the content changes.
9. What is the working tree in Git?
The working tree (or working directory) is the directory containing the files you're currently working on. It represents a single checkout of one version of the project that you can modify. Changes in the working tree need to be staged and then committed to become part of the repository history.
10. What is the staging area (index) in Git?
The staging area (also called the index) is a file that stores information about what will go into your next commit. When you run git add
, you're adding changes to the staging area. The staging area allows you to selectively choose which changes to include in a commit.
Staging & Committing (11-20)
11. How do you stage changes for commit?
You use the git add
command to stage changes:
git add filename
- stages a specific filegit add .
- stages all changes in the current directory and subdirectoriesgit add -A
- stages all changes in the entire repositorygit add -p
- interactively stage changes patch by patch
12. How do you commit staged changes?
Use the git commit
command:
git commit -m "Commit message"
- commits with a messagegit commit
- opens the default text editor to write a commit messagegit commit -a
- stages all tracked files that have been modified and commits them (skips untracked files)
13. How do you amend the most recent commit?
Use git commit --amend
. This allows you to:
- Modify the commit message
- Add forgotten changes to the commit
- Combine it with
git add
to include staged changes:git add forgotten_file.txt && git commit --amend --no-edit
14. What is the difference between 'git add .' and 'git add -A'?
In older versions of Git, there was a difference:
git add .
adds all new and modified files in the current directory and its subdirectoriesgit add -A
adds all new, modified, and deleted files in the entire repository, regardless of your current directory- In modern Git (version 2.x and above),
git add .
behaves the same asgit add -A
when run from the root directory
15. How do you remove a file from the staging area?
Use git reset HEAD filename
to unstage a file while keeping the changes in your working directory. Alternatively, use git restore --staged filename
(Git 2.23+).
16. How do you write a good commit message?
A good commit message should:
- Have a short summary (50 characters or less)
- Be followed by a blank line and a more detailed description if needed
- Use the imperative mood ("Add feature" not "Added feature")
- Explain what and why, not how (the code shows how)
- Reference issue or ticket numbers if applicable
17. How do you see what changes are staged for commit?
Use git diff --staged
or git diff --cached
to view the differences between the staging area and the last commit.
18. How do you skip the staging area and commit directly?
Use git commit -a -m "Commit message"
. The -a
option automatically stages all tracked files that have been modified or deleted, but it doesn't add new (untracked) files.
19. How do you view the commit history?
Use git log
with various options:
git log
- shows the commit historygit log --oneline
- shows condensed one-line per commitgit log --graph
- shows branch history as a graphgit log -p
- shows the patch (diff) for each commitgit log --since="2 weeks ago"
- shows commits from a specific time period
20. How do you create an empty commit?
Use git commit --allow-empty -m "Empty commit message"
. Empty commits contain no changes to the code but can be useful for triggering CI/CD processes or marking specific points in history.
Branching & Merging (21-40)
21. What is a branch in Git?
A branch is a lightweight movable pointer to a commit. Branches allow you to develop features, fix bugs, or experiment with ideas in an isolated area without affecting the main codebase. The default branch is typically called "main" or "master".
22. How do you create a new branch?
Use git branch branch_name
to create a new branch, or git checkout -b branch_name
to create and switch to it immediately. In Git 2.23+, you can use git switch -c branch_name
.
23. How do you switch between branches?
Use git checkout branch_name
or, in Git 2.23+, git switch branch_name
. This updates your working directory to match the branch and moves the HEAD pointer to that branch.
24. How do you delete a branch?
Use git branch -d branch_name
to delete a branch that has been merged. Use git branch -D branch_name
to force delete a branch that hasn't been merged.
25. What is the difference between 'git merge' and 'git rebase'?
- git merge integrates changes from one branch into another by creating a new merge commit that has two parents.
- git rebase moves or combines a sequence of commits to a new base commit, creating a linear history.
- Merging preserves history exactly as it happened, while rebasing creates a cleaner, linear history.
26. How do you merge a branch?
First, switch to the branch you want to merge into (e.g., main), then run git merge branch_name
. This will integrate the specified branch into your current branch.
27. What is a fast-forward merge?
A fast-forward merge occurs when the branch you're trying to merge hasn't diverged from the current branch. Instead of creating a merge commit, Git simply moves the branch pointer forward. You can prevent this with git merge --no-ff
to always create a merge commit.
28. How do you rebase a branch?
Switch to the branch you want to rebase and run git rebase target_branch
. For example, to rebase a feature branch onto main: git checkout feature && git rebase main
.
29. What is a merge conflict and how do you resolve it?
A merge conflict occurs when Git can't automatically resolve differences between branches. To resolve conflicts:
- Open the conflicted files and look for conflict markers (
<<<<<<<
,=======
,>>>>>>>
) - Edit the files to resolve the conflicts
- Stage the resolved files with
git add
- Complete the merge with
git commit
30. What is a detached HEAD state?
A detached HEAD occurs when you check out a specific commit rather than a branch. In this state, any new commits you make won't belong to any branch and may be lost if you switch to another branch. To avoid this, create a new branch from the detached HEAD state with git switch -c new_branch_name
.
31. How do you rename a branch?
To rename the current branch: git branch -m new_name
. To rename a different branch: git branch -m old_name new_name
. If you've already pushed the branch, you'll need to delete the old remote branch and push the new one.
32. What is the difference between 'git pull' and 'git fetch'?
- git fetch downloads objects and refs from another repository but doesn't integrate them into your working directory.
- git pull is equivalent to
git fetch
followed bygit merge
(orgit rebase
with--rebase
option).
33. How do you see all branches?
Use git branch
to see local branches, or git branch -a
to see both local and remote branches. Add the -v
flag to see the last commit on each branch: git branch -v
or git branch -av
.
34. What is a tracking branch?
A tracking branch is a local branch that has a direct relationship with a remote branch. When you clone a repository, Git automatically creates a tracking branch for the default branch (usually main/master). You can set up tracking with git branch --set-upstream-to=remote/branch
or when pushing: git push -u origin branch_name
.
35. How do you compare two branches?
Use git diff branch1..branch2
to see differences between the tips of two branches. Use git diff branch1...branch2
(three dots) to see changes from their common ancestor to the tip of branch2.
36. What is a tag in Git?
A tag is a reference to a specific commit, often used to mark release points (v1.0, v2.0, etc.). Unlike branches, tags don't move as new commits are added. There are two types: lightweight tags (just a pointer to a commit) and annotated tags (store extra metadata like tagger name, date, and message).
37. How do you create and push tags?
Create a lightweight tag: git tag v1.0
. Create an annotated tag: git tag -a v1.0 -m "Version 1.0"
. Push tags to remote: git push origin v1.0
or push all tags: git push origin --tags
.
38. What is cherry-picking in Git?
Cherry-picking applies the changes introduced by an existing commit to your current branch. Use git cherry-pick commit_hash
. This is useful for applying specific fixes or features from one branch to another without merging the entire branch.
39. How do you stash changes?
Use git stash
to temporarily save changes that aren't ready to be committed. This reverts your working directory to the last commit. Use git stash list
to see stashes, git stash apply
to restore changes, and git stash drop
to remove a stash.
40. What is the difference between 'git stash pop' and 'git stash apply'?
- git stash apply applies the stashed changes but keeps them in the stash list.
- git stash pop applies the stashed changes and removes them from the stash list.
Working with Remotes (41-50)
41. What is a remote in Git?
A remote is a common repository that all team members use to exchange their changes. Typically, this is a repository hosted on a server like GitHub, GitLab, or Bitbucket. The default remote is usually named "origin".
42. How do you add a remote repository?
Use git remote add remote_name remote_url
. For example: git remote add origin https://github.com/user/repo.git
.
43. How do you see information about remotes?
Use git remote -v
to see all remotes and their URLs. Use git remote show origin
to see detailed information about a specific remote.
44. How do you rename a remote?
Use git remote rename old_name new_name
. For example: git remote rename origin upstream
.
45. How do you remove a remote?
Use git remote remove remote_name
or git remote rm remote_name
. For example: git remote remove origin
.
46. How do you push changes to a remote repository?
Use git push remote_name branch_name
. For example: git push origin main
. To set upstream tracking: git push -u origin branch_name
(after this, you can just use git push
).
47. How do you pull changes from a remote repository?
Use git pull remote_name branch_name
. For example: git pull origin main
. This fetches changes and merges them into your current branch.
48. What is the difference between 'git clone' and 'git fork'?
- git clone creates a local copy of a remote repository on your machine.
- Forking is a GitHub/GitLab concept (not a Git command) that creates a copy of someone else's repository in your own account, which you can then clone and work on independently.
49. How do you update your local repository with changes from the remote?
Use git fetch
to download changes without merging, then git merge
or git rebase
to integrate them. Alternatively, use git pull
which does both steps at once.
50. What is a pull request?
A pull request (called Merge Request in GitLab) is a method of submitting contributions to a project. It's a request to merge changes from one branch into another, typically used in collaborative development on platforms like GitHub. It allows for code review and discussion before changes are integrated.
Inspecting & Rewriting History (51-60)
51. How do you view the changes in a specific commit?
Use git show commit_hash
to see the changes introduced by a specific commit. You can also use git log -p
to see changes for all commits, or git log -p -n 5
to see changes for the last 5 commits.
52. How do you find a commit by message?
Use git log --grep="search term"
to search commit messages. Use git log --all --grep="search term"
to search across all branches.
53. How do you find which commit introduced a bug?
Use git bisect
to perform a binary search through history:
- Start with
git bisect start
- Mark a known bad commit:
git bisect bad
- Mark a known good commit:
git bisect good commit_hash
- Git will checkout a commit in the middle; test if it's good or bad
- Repeat until Git identifies the first bad commit
- End with
git bisect reset
54. How do you revert a commit?
Use git revert commit_hash
to create a new commit that undoes the changes from the specified commit. This is a safe way to undo changes because it doesn't rewrite history.
55. How do you reset changes in Git?
There are three types of reset:
- Soft reset (
git reset --soft
): moves HEAD but keeps changes staged - Mixed reset (
git reset --mixed
): moves HEAD and unstages changes (default) - Hard reset (
git reset --hard
): moves HEAD and discards all changes
56. What is the difference between 'git reset' and 'git revert'?
- git reset moves the branch pointer backward, effectively erasing commits from history (dangerous if already pushed).
- git revert creates a new commit that undoes changes, preserving history (safe for shared branches).
57. How do you edit a commit message?
Use git commit --amend
to edit the most recent commit message. To edit older commits, use interactive rebase: git rebase -i HEAD~n
(where n is the number of commits to go back), then change "pick" to "reword" for the commits you want to edit.
58. How do you squash multiple commits into one?
Use interactive rebase: git rebase -i HEAD~n
(where n is the number of commits to include), then change "pick" to "squash" or "fixup" for the commits you want to combine. Squash keeps the commit messages, while fixup discards them.
59. How do you change the author of a commit?
For the most recent commit: git commit --amend --author="New Author
. For older commits, use interactive rebase and add --exec 'git commit --amend --author="New Author
after each commit you want to change.
60. How do you recover a deleted branch?
Find the commit hash of the branch tip using git reflog
, then recreate the branch: git branch branch_name commit_hash
. The reflog keeps track of where HEAD and branch references have been for the last few months.
Advanced Topics & Workflows (61-80)
61. What is Git Hooks?
Git hooks are scripts that run automatically before or after specific Git commands. They are stored in the .git/hooks
directory. Common hooks include pre-commit (run before committing), pre-push (run before pushing), and post-receive (run after receiving pushed commits on the server).
62. What is Git Flow?
Git Flow is a branching model that defines a strict branching structure designed around project releases. It uses two main branches (main and develop) and three supporting branch types (feature, release, and hotfix). While popular, it has been criticized for being too complex for some projects.
63. What is GitHub Flow?
GitHub Flow is a simpler alternative to Git Flow. It involves:
- Anything in the main branch is deployable
- Create descriptive branches off of main
- Push to named branches regularly
- Open a pull request when you need feedback or ready to merge
- Merge only after pull request review
- Deploy immediately after merging
64. What is a submodule in Git?
A submodule is a repository embedded inside another repository. It allows you to keep a Git repository as a subdirectory of another Git repository. This is useful for including external dependencies or libraries in your project while maintaining their separate version history.
65. How do you work with submodules?
Key commands for submodules:
- Add a submodule:
git submodule add repository_url path
- Initialize and clone submodules:
git submodule init && git submodule update
- Clone a repository with submodules:
git clone --recurse-submodules repository_url
- Update submodules to latest commits:
git submodule update --remote
66. What is Git LFS?
Git Large File Storage (LFS) is an extension that replaces large files (like audio, video, datasets) with text pointers inside Git while storing the actual file contents on a remote server. This prevents repository bloat when working with large files.
67. What is a .gitignore file?
The .gitignore file specifies intentionally untracked files that Git should ignore. This typically includes build artifacts, log files, environment configurations, and dependencies. Files already tracked by Git are not affected by .gitignore rules.
68. How do you ignore files that are already tracked?
First, remove the file from the index: git rm --cached filename
. Then add the file to .gitignore. The --cached
option removes the file from the index but keeps it in your working directory.
69. What is the difference between 'git fetch --prune' and 'git remote prune'?
Both commands remove remote-tracking references that no longer exist on the remote. git fetch --prune
prunes during a fetch operation, while git remote prune origin
only prunes without fetching.
70. What is the Git worktree feature?
Git worktree allows you to have multiple working trees attached to the same repository. This lets you work on multiple branches simultaneously without stashing or cloning the repository again. Useful for context switching between features or testing.
71. How do you use Git with SSH keys?
To use Git with SSH:
- Generate an SSH key pair:
ssh-keygen -t ed25519 -C "your_email@example.com"
- Add the public key to your Git hosting service (GitHub, GitLab, etc.)
- Clone repositories using the SSH URL:
git clone git@github.com:user/repo.git
- Configure Git to use SSH:
git config --global url."git@github.com:".insteadOf "https://github.com/"
72. What is the Git reflog?
The reference log (reflog) records when the tips of branches and other references were updated in the local repository. It's a safety net that allows you to recover lost commits or branches. Use git reflog
to view the log and git reflog show branch_name
to see changes to a specific branch.
73. How do you configure Git?
Use git config
to set configuration options:
- System level (all users):
git config --system
(stored in /etc/gitconfig) - Global level (current user):
git config --global
(stored in ~/.gitconfig) - Repository level:
git config
without options (stored in .git/config)
74. What are some important Git configuration settings?
Some useful configurations:
- Set user info:
git config --global user.name "Your Name"
andgit config --global user.email "your@email.com"
- Set default editor:
git config --global core.editor "code --wait"
- Enable color output:
git config --global color.ui true
- Set default branch name:
git config --global init.defaultBranch main
- Create aliases:
git config --global alias.co checkout
75. How do you create a Git alias?
Use git config --global alias.alias_name "command"
. For example: git config --global alias.co checkout
creates git co
as an alias for git checkout
. For complex aliases with multiple commands, use quotes: git config --global alias.ll "log --oneline --graph --all"
.
76. What is the difference between 'git diff' and 'git difftool'?
- git diff shows differences in the terminal.
- git difftool launches an external tool (like vimdiff, meld, or beyond compare) to view differences. You need to configure the tool first.
77. How do you set up a difftool in Git?
Configure your preferred diff tool:
- For VS Code:
git config --global diff.tool vscode
andgit config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"
- For Beyond Compare:
git config --global diff.tool bc
andgit config --global difftool.bc.path "/usr/bin/bcomp"
- Then use
git difftool
instead ofgit diff
78. What is Git blame and how is it used?
git blame
shows what revision and author last modified each line of a file. It's useful for tracking when changes were made and by whom. Use git blame filename
or git blame -L 10,20 filename
to blame specific lines.
79. How do you find which commit changed a specific line?
Use git log -S"search term" filename
to find commits that added or removed the specific text. Use git log -L start,end:filename
to see the history of a specific range of lines.
80. What is Git archive?
git archive
creates a compressed archive (zip or tar) of files from a named tree (usually a commit or tag). Useful for creating release packages: git archive --format=zip --output=release.zip v1.0
.
Troubleshooting & Best Practices (81-100)
81. How do you recover from a bad merge?
If you haven't pushed yet, use git reset --hard HEAD^
to undo the merge. If you have pushed, use git revert -m 1 merge_commit_hash
to create a new commit that reverses the merge. The -m 1
option specifies which parent is the mainline.
82. How do you handle large files accidentally committed?
Use the BFG Repo-Cleaner or git filter-branch
to remove large files from history. Warning: this rewrites history and should not be done on shared branches without coordination.
83. What should you do if you committed to the wrong branch?
If not pushed yet:
- Switch to the correct branch:
git checkout correct_branch
- Cherry-pick the commit:
git cherry-pick commit_hash
- Switch back to the original branch:
git checkout original_branch
- Reset the branch:
git reset --hard HEAD~1
(if it was the last commit)
84. How do you resolve a merge conflict during rebase?
When a conflict occurs during rebase:
- Resolve the conflicts in the files
- Stage the resolved files:
git add filename
- Continue the rebase:
git rebase --continue
- If you want to abort instead:
git rebase --abort
85. What is the golden rule of rebasing?
Never rebase public branches (branches that others might be working on). Rebasing rewrites history, which can cause serious issues for collaborators who have based their work on the original history. Only rebase private branches that haven't been shared.
86. How do you clean untracked files?
Use git clean
to remove untracked files:
git clean -n
- dry run (show what would be removed)git clean -f
- force removal of filesgit clean -fd
- force removal of files and directoriesgit clean -x
- remove ignored files too
87. How do you troubleshoot authentication issues with Git?
Common solutions:
- Use SSH instead of HTTPS for authentication
- Use a credential helper:
git config --global credential.helper cache
(stores in memory) orgit config --global credential.helper store
(stores on disk) - For GitHub, use a personal access token instead of a password
- Check SSH keys:
ssh -T git@github.com
88. What are some Git best practices?
- Commit often with meaningful messages
- Make focused, single-purpose commits
- Write descriptive commit messages
- Use branches for new features and bug fixes
- Test before committing
- Pull changes regularly
- Don't commit generated files or sensitive data
- Use .gitignore appropriately
89. How do you optimize Git performance for large repositories?
Tips for large repositories:
- Use shallow clones:
git clone --depth=1
- Use sparse checkout:
git sparse-checkout init && git sparse-checkout set dir1 dir2
- Use Git LFS for large files
- Run maintenance:
git gc
(garbage collection) - Use
git config --global core.preloadindex true
- Use
git config --global core.fscache true
90. How do you collaborate effectively with Git?
Effective collaboration practices:
- Establish a branching strategy (Git Flow, GitHub Flow, etc.)
- Use pull requests for code review
- Protect important branches (main, develop)
- Use issue tracking integrated with commits
- Communicate about branch naming and workflow
- Regularly fetch and merge upstream changes
- Write comprehensive commit messages
91. How do you handle binary files in Git?
Binary files don't diff well in Git. Best practices:
- Use Git LFS for large binary files
- Keep binary files to a minimum
- If small, commit them directly but be aware they can't be merged
- Consider storing binaries in a separate artifact repository
92. What is Git's object model?
Git has four main object types:
- Blobs: store file contents
- Trees: represent directories, contain blobs and other trees
- Commits: point to a tree and parent commit(s), contain metadata
- Tags: point to commits, contain metadata
All objects are content-addressable by their SHA-1 hash.
93. How does Git store data?
Git stores data as snapshots, not differences. When you commit, Git takes a snapshot of what all your files look like at that moment and stores a reference to that snapshot. If files haven't changed, Git doesn't store the file again—just a link to the previous identical file it has already stored.
94. What is the Git index?
The index (also called staging area) is a binary file (generally located in .git/index) that contains a sorted list of path names, each with permissions and the SHA-1 of a blob object. It represents the proposed next commit. Git checks against the index rather than the working tree for many operations.
95. How do you migrate from SVN to Git?
Use git svn
to migrate from SVN to Git:
- Clone the SVN repo:
git svn clone -s http://svn.example.com/project
(the -s assumes standard trunk/branches/tags layout) - Clean up branches and tags:
git for-each-ref --format="%(refname:short) %(objectname)" refs/remotes/origin/tags | sed 's/^origin\///' | while read ref; do git tag -a $ref -m "Tag from SVN"; done
- Push to a new Git remote:
git remote add origin git@github.com:user/project.git && git push origin --all && git push origin --tags
96. How do you contribute to an open source project using Git?
Typical workflow:
- Fork the project on GitHub/GitLab
- Clone your fork:
git clone git@github.com:yourname/project.git
- Add upstream remote:
git remote add upstream git@github.com:original/project.git
- Create a feature branch:
git checkout -b feature-branch
- Make changes and commit
- Push to your fork:
git push origin feature-branch
- Create a pull request from your fork to the original repository
- Keep your branch updated:
git fetch upstream && git rebase upstream/main
97. How do you handle Git in a CI/CD pipeline?
Best practices for CI/CD:
- Use shallow clones to save time and space
- Fetch only the needed branches
- Use Git tags for deployment versions
- Run tests on feature branches before merging
- Automate version tagging
- Use Git hooks for pre-commit checks
- Integrate with pull request status checks
98. What are Git worktrees and when would you use them?
Git worktrees allow you to have multiple working directories connected to the same repository. Useful for:
- Working on multiple features simultaneously
- Keeping a long-running build or test in one worktree while working in another
- Comparing different versions side by side
- Creating a worktree:
git worktree add ../path/to/new/worktree branch-name
99. How do you secure a Git repository?
Security practices:
- Use SSH keys with passphrases or deploy keys for automation
- Protect important branches with branch protection rules
- Use signed commits and tags
- Regularly audit access permissions
- Use pre-receive hooks for policy enforcement
- Avoid storing secrets in repositories (use environment variables or secret managers)
- Keep Git updated with security patches
100. How do you troubleshoot common Git problems?
Common issues and solutions:
- Authentication issues: Check SSH keys or use personal access tokens
- Merge conflicts: Communicate with team members, resolve carefully
- Detached HEAD: Create a branch from the current commit
- Accidental commit to wrong branch: Use cherry-pick to move the commit
- Lost commits: Check git reflog to find them
- Large repository: Use Git LFS, shallow clone, or sparse checkout
- Slow performance: Run git gc, use preloadindex and fscache settings