I have a soft spot for Git. Not because it’s perfect or easy. But because it’s patient. Every time you make a mistake, it offers a second chance. Every time you’re lost, git log gently shows you the way. There’s something almost affectionate about a tool that says, “It’s okay. Let’s rewind. Try again.”
And you? The way you keep showing up, commit after commit, learning branch by branch—that deserves adoration. Truly. Because mastering Git isn’t about memorizing commands. It’s about falling in love with the safety net of version control, the joy of a clean history, the quiet confidence of git push before a deadline.
These 100 Git Interview Questions & Answers are my gift to you—wrapped in warmth and rooted in care. Each question is a gentle nudge. Each answer is a small hug for your memory. I want you to feel held, not haunted, by the details of rebase, merge, and reset.
Let your adoration for clean workflows shine. Let your affection for problem-solving whisper the right command at the right moment. You’re not just preparing for an interview. You’re building a love story with a tool that will never leave you stranded.
Now, with all my heart—let’s commit to this together.
What is Git and what are its main advantages?
Answer: Git is a distributed version control system (DVCS) that tracks changes in source code during software development. Main advantages: distributed architecture (every developer has a full copy of the repository), fast performance, strong support for non-linear development (branching and merging), data integrity (SHA-1 hashing), and staging area for committing specific changes.
What is the difference between Git and SVN (Subversion)?
Answer: Git is distributed: every user has a full copy of the repository (including history). SVN is centralized: a single central server stores the history. Git branches are lightweight and easy to create; SVN branches are heavier (copies of directories). Git supports offline commits; SVN requires network for most operations. Git uses cryptographic hashes for integrity; SVN uses sequential revision numbers.
What is a repository in Git?
Answer: A repository (repo) is a directory that contains all the files, folders, and metadata (including the .git directory) that Git uses to track changes. It can be local (on your machine) or remote (on a server). The .git directory holds the object database, references, and configuration.
What is the difference between git init and git clone?
Answer: git init creates a new empty Git repository in the current directory. git clone copies an existing remote repository to your local machine, creating a working copy and setting up remote tracking (usually origin). Clone also creates a remote connection automatically.
What is a commit in Git?
Answer: A commit is a snapshot of the repository at a specific point in time. It has a unique SHA-1 hash (40 characters), author and committer information, timestamp, commit message, and a pointer to the previous commit(s) (parent). Commits form a directed acyclic graph (DAG).
What are the three states (trees) of a file in Git?
Answer: Working Directory (the file on your file system, can be modified), Staging Area (Index) where changes are added before committing, and Repository (the committed snapshot, stored in .git). The typical flow: modify → stage → commit.
What is the staging area (index) in Git?
Answer: The staging area (also called index) is an intermediate area where you place changes that you want to include in the next commit. It allows you to commit only a subset of modified files, or even parts of a file (git add -p). Staging is done with git add.
What is a branch in Git?
Answer: A branch is a movable pointer to a specific commit. It allows independent lines of development. The default branch is usually main or master. Branching is lightweight because Git only stores a reference to the commit hash, not copies of files.
What is the difference between git merge and git rebase?
Answer: Both integrate changes from one branch into another. git merge creates a new “merge commit” that ties the histories together, preserving the timeline of both branches. git rebase reapplies commits from the current branch onto the tip of the target branch, rewriting history to create a linear sequence. Rebase results in cleaner history but should not be used on public/shared branches.
What is a merge conflict and how do you resolve it?
Answer: A merge conflict occurs when Git cannot automatically reconcile differences between two commits (e.g., same line modified differently). Git marks the conflict in the affected files with <<<<<<<, =======, >>>>>>>. To resolve: edit the file to keep the desired changes, stage the file (git add), then complete the merge (git commit). Use merge tools like git mergetool.
What is git fetch and how is it different from git pull?
Answer: git fetch downloads new commits from a remote repository but does not integrate them into your local working branch; it updates remote-tracking branches (e.g., origin/main). git pull is git fetch followed by git merge (or git rebase if configured). Use fetch to inspect changes before merging.
What is git push and what does it do?
Answer: git push uploads local commits to a remote repository, updating remote branches. It requires write permission. The typical syntax: git push <remote> <local-branch>:<remote-branch>. If the remote branch does not exist, Git can create it. Use git push -u to set upstream tracking.
What is a remote in Git?
Answer: A remote is a named reference to another Git repository (usually on a server). origin is the default name for the remote you cloned from. Manage remotes with git remote add, git remote remove, git remote -v (list). Fetching and pushing use remotes.
What is the difference between git reset and git revert?
Answer: Both undo changes but in different ways. git revert creates a new commit that reverses the effect of a previous commit (safe for shared history). git reset moves the branch pointer back, discarding commits (rewrites history). Reset has modes: --soft (keep changes staged), --mixed (keep working directory, unstage), --hard (discard all changes). Never reset shared branches.
What is git stash and when would you use it?
Answer: git stash temporarily saves uncommitted changes (working directory and staging area) and reverts the working directory to the last commit. Use it when you need to switch branches or pull updates but are not ready to commit. Later restore with git stash pop (applies and removes) or git stash apply (applies and keeps stash).
What is the .gitignore file?
Answer: .gitignore is a plain text file that lists patterns of files and directories that Git should ignore and not track (e.g., compiled binaries, logs, dependencies, IDE settings). It can be placed in any directory. Patterns can be negated with !. Use git add -f to force-add ignored files.
How do you view the commit history?
Answer: git log shows the commit history. Common options: --oneline (compact), --graph (ASCII graph), --decorate (show refs), --author, --since, -p (patch), --stat. git reflog shows reference logs (including lost commits). git show <commit> displays details of a specific commit.
What is a detached HEAD state?
Answer: HEAD is a pointer to the current branch reference (or directly to a commit). Detached HEAD occurs when you check out a specific commit, tag, or remote branch without creating a local branch. In this state, new commits are not associated with any branch (they will be lost if you switch away). To fix, create a new branch: git switch -c new-branch.
How do you delete a branch?
Answer: Local branch: git branch -d branch-name (safe delete, only if merged). Force delete: git branch -D branch-name. Remote branch: git push origin --delete branch-name or git push origin :branch-name.
What is the purpose of git tag?
Answer: Tags are permanent references to specific commits, often used to mark release points (v1.0, v2.0). Two types: lightweight (just a pointer) and annotated (stored as full object with metadata: tagger, date, message). Annotated tags are recommended. Create with git tag -a v1.0 -m "message". Push tags to remote with git push origin --tags.
What is the difference between git merge --squash and a regular merge?
Answer: git merge --squash combines all the commits from the source branch into a single set of staged changes, but does not create a merge commit; you then make a single commit. This collapses history but loses granularity. Regular merge keeps individual commits and creates a merge commit (or fast-forwards).
What is a fast-forward merge?
Answer: When the current branch is directly upstream of the target branch (i.e., the target branch has no new commits), Git simply moves the pointer forward, without creating a merge commit. This results in a linear history. To disable fast-forward, use git merge --no-ff.
What is the git reflog command and when is it useful?
Answer: The reflog (reference log) records when the tips of branches and other references are updated in your local repository. It’s a safety net; you can recover lost commits even if they are not reachable from any branch. Use git reflog to find the hash and then reset or cherry-pick.
How do you change the last commit message?
Answer: git commit --amend -m "new message". You can also amend staged changes (add forgotten files) without changing message: git commit --amend --no-edit. Amend rewrites history; do not use on pushed commits.
How do you unstage a file that you added with git add?
Answer: git reset HEAD <file> (or git restore --staged <file> in newer Git). This removes the file from staging but keeps your changes in the working directory.
How do you discard local changes to a file?
Answer: git checkout -- <file> or git restore <file> (recommended in modern Git). This reverts the file to the last committed version. Caution: changes are lost.
How do you list all branches (local and remote)?
Answer: git branch -a (local and remote). git branch -r (only remote). Remote branches are shown as remotes/origin/branch-name.
What is git cherry-pick?
Answer: Cherry-pick applies the changes introduced by an existing commit to your current branch as a new commit. It is useful for selectively transferring specific commits without merging entire branches. Example: git cherry-pick <commit-hash>.
What is git bisect and how does it help?
Answer: git bisect is a binary search tool to find the commit that introduced a bug. You mark a known good commit and a known bad commit; Git checks out the middle commit, you test, then mark as good or bad, repeating until the offending commit is identified. Speeds up debugging.
How do you see the difference between two commits?
Answer: git diff commit1 commit2 (or using .. or ...). git diff HEAD~3 HEAD shows last three commits aggregated. git difftool opens a visual diff tool.
What is the difference between git pull --rebase and git pull?
Answer: git pull (default) does a fetch + merge, creating a merge commit when there are divergent histories. git pull --rebase does fetch + rebase, replaying local commits on top of the remote branch, resulting in a linear history without merge commits.
What are Git hooks?
Answer: Git hooks are scripts that run automatically at key points (e.g., pre-commit, post-commit, pre-push). They live in .git/hooks/. Use them to enforce coding standards, run tests, or prevent bad commits. Hooks are client-side but can be shared via templates or tools like pre-commit.
What is a submodule in Git?
Answer: A submodule is a reference to another Git repository embedded inside your main repository at a specific path. It allows you to include external projects while keeping their history independent. Update submodules manually: git submodule update --init. Useful for shared libraries.
How do you squash multiple commits into one?
Answer: Use git rebase -i HEAD~n (interactive rebase). Replace pick with squash (or s) for the commits you want to combine. Keep the first commit as pick. The commit messages will be combined, and you can edit the final message.
What is the purpose of git blame?
Answer: git blame shows, line by line, the commit hash, author, and timestamp of the last change for each line in a file. Useful for identifying when a bug was introduced or understanding code evolution.
How do you revert a commit that has already been pushed?
Answer: Use git revert <commit-hash> to create a new commit that undoes the changes. This is safe because it does not rewrite history. Then push the revert commit normally. Avoid git reset on shared branches.
What does git clean do?
Answer: git clean removes untracked files from the working directory. Use git clean -n to dry-run, git clean -f to force remove, git clean -fd to remove directories as well. Use with caution as it deletes files not in Git.
How do you move or rename a file in Git?
Answer: git mv old-name new-name. This stages the move. You can also manually move file and then do git add new-name and git rm old-name. Git detects the rename using similarity heuristics.
What is the difference between git reset --soft, --mixed, and --hard?
Answer: --soft moves HEAD but keeps changes staged (index unchanged). --mixed (default) moves HEAD and unstages changes (staging area reset, working directory unchanged). --hard moves HEAD and discards all changes in working directory and staging area (dangerous).
What is a tracking branch (upstream branch)?
Answer: A local branch that is associated with a remote branch. When you run git push without arguments, Git knows where to push. git branch -u origin/feature sets upstream. git branch -vv shows tracking relationships.
What happens when you run git push --force?
Answer: Force push overwrites the remote branch with your local branch, discarding any commits on the remote that you do not have locally. It rewrites public history and can cause collaborators to lose work. Use --force-with-lease as a safer alternative (checks that remote hasn’t changed).
What is a Git fork (on platforms like GitHub)?
Answer: A fork is a copy of a repository (server-side) under your own account. It allows you to contribute without write access to the original repository. Workflow: fork, clone, make changes, push to your fork, then open a pull request.
What is a pull request (or merge request)?
Answer: A pull request is a mechanism to propose changes from one branch (or fork) into another (typically into the main branch). It enables code review, discussion, and automated checks before merging. Git itself does not include pull requests; they are platform features (GitHub, GitLab, Bitbucket).
How do you fix a commit that was made to the wrong branch?
Answer: 1) git log to find the commit hash. 2) git checkout correct-branch. 3) git cherry-pick <commit>. 4) git checkout wrong-branch and git reset --hard HEAD~1 (remove from wrong branch). If the commit was pushed, force push may be needed (collaborate carefully).
How do you see which files have changed between two commits?
Answer: git diff --name-only commit1 commit2. Also git log --stat, git show --name-only.
What is git worktree?
Answer: git worktree allows you to have multiple working directories attached to the same repository. Useful for running concurrent branches without switching or cloning. Example: git worktree add ../branch-name branch-name.
How do you exclude a file from being tracked after it was already committed?
Answer: First add the pattern to .gitignore. Then remove the file from the index without deleting it: git rm --cached filename. Commit the change. The file will be ignored from then on.
What is git archive used for?
Answer: git archive creates a tar or zip archive of the files from a specific tree (commit, branch, tag). It does not include the .git directory. Useful for distributing releases. Example: git archive --format=zip HEAD > release.zip.
How do you find commits that affect a specific line or function?
Answer: git log -S "search string" (pickaxe) searches for commits that added or removed a string. git log -L :function:file traces line ranges. git blame helps then git log on the commit.
What is the difference between git merge --ff-only and git merge --no-ff?
Answer: --ff-only only performs a merge if it can be fast-forwarded; otherwise it aborts. --no-ff always creates a merge commit even if fast-forward is possible, preserving feature branch history explicitly.
What is a Git alias and how do you create one?
Answer: A Git alias is a shortcut for a longer Git command. Create with git config --global alias.co checkout (so git co works). Also aliases for complex commands: git config --global alias.graph "log --graph --oneline --decorate".
How do you check the current branch?
Answer: git branch shows local branches with * on current branch. git status also shows branch name. git symbolic-ref --short HEAD gives programmatic output.
How do you compare the working directory with the last commit?
Answer: git diff HEAD compares working directory to last commit. git diff --staged compares staging area to last commit.
What is the difference between git pull and git fetch (already answered)?
How do you change the author of a commit?
Answer: For the most recent commit: git commit --amend --author="New Author <email>". For older commits, interactive rebase (git rebase -i) and set edit, then git commit --amend --author, then git rebase --continue. Rewrites history.
What is a bare repository?
Answer: A bare repository has no working directory (no checked-out files). It contains only the .git directory contents. Usually used as a central remote repository (e.g., on servers). Clone with git clone --bare. You cannot edit files directly.
How do you handle large files in Git?
Answer: Git is not designed for large binary files. Use Git LFS (Large File Storage) to store large files (videos, datasets) by replacing them with text pointers. Alternatively, use git annex or exclude them.
What does git fsck do?
Answer: git fsck (file system check) verifies the integrity of the Git object database. It can find dangling commits or corrupted objects. Useful for repository health checks.
How do you export a Git repository without the .git directory?
Answer: git archive or clone and then remove .git (but clone creates .git). A simple method: git checkout-index -a -f --prefix=export/ (if in a clean working directory). Or use git ls-files with tar.
What is the purpose of git gc?
Answer: git gc (garbage collection) optimizes the repository by compressing file revisions, removing unreachable objects, and packing objects. Git runs it automatically occasionally, but you can run manually.
How do you revert a file to a previous version?
Answer: git checkout <commit-hash> -- <file> or git restore --source=<commit-hash> <file>. This replaces the file in your working directory with the version from that commit.
What is a SHA in Git?
Answer: SHA is the 40-character hexadecimal hash (SHA-1) used to uniquely identify each commit, tree, blob, and tag. Git uses it for integrity and as object identifiers.
How do you see commits by a specific author?
Answer: git log --author="John Doe". Also --since, --until filters.
What is the difference between git merge --abort and git merge --continue?
Answer: During a merge conflict, --abort stops the merge and restores the pre-merge state. --continue is used after resolving conflicts and staging changes to complete the merge.
What is git rebase --interactive used for?
Answer: Interactive rebase (-i) allows you to edit, reorder, squash, drop, or reword commits before applying them. It’s a powerful tool to clean up local commit history before sharing.
How do you clone only a specific branch?
Answer: git clone --branch <branch-name> --single-branch <remote-url>. This reduces time and disk space.
What is a “detached HEAD” and how to recover?
Answer: Already covered. To save work: create a new branch git switch -c new-branch. Then reset or merge as needed.
How do you update a submodule to the latest commit?
Answer: cd submodule-dir, git pull origin main, then cd .. and git add submodule-dir, git commit.
What is the git rerere feature?
Answer: git rerere (reuse recorded resolution) remembers how you resolved merge conflicts and automatically applies the same resolution when the same conflict appears again. Enable with git config --global rerere.enabled true.
How do you see the difference between staging area and last commit?
Answer: git diff --cached or git diff --staged.
What is git show used for?
Answer: git show <commit> displays the commit details and the diff of changes introduced by that commit. Also works on tags, trees, or blobs.
How do you restore a deleted file that hasn’t been committed yet?
Answer: If the file was tracked and you deleted it, git restore <file> (or git checkout -- <file>) restores from the index. If the deletion was staged, unstage first.
What is a topic branch?
Answer: A short-lived branch created to work on a specific feature or bug fix. Once finished, it is merged (and often deleted). This keeps the main branch clean.
How do you delete a tag locally and remotely?
Answer: Locally: git tag -d tag-name. Remotely: git push origin --delete tag-name or git push origin :refs/tags/tag-name.
What is the difference between git and GitHub?
Answer: Git is the version control system software. GitHub is a cloud-based platform for hosting Git repositories, providing collaboration features (pull requests, issues, actions, etc.). Similarly GitLab, Bitbucket.
How do you configure Git user name and email?
Answer: git config --global user.name "Your Name" and git config --global user.email "your.email@example.com". Use --local to set per-repository.
What is a pre-commit hook example?
Answer: A pre-commit hook (e.g., .git/hooks/pre-commit) could run linting or tests. If the script exits non-zero, the commit is aborted.
How do you see all commits affecting a specific file?
Answer: git log --follow <file>. --follow handles renames.
What is git grep used for?
Answer: git grep searches for a pattern in tracked files (not just the working directory but also old revisions if you specify commit). Faster than grep because it uses the index.
How do you remove a remote from your repository?
Answer: git remote remove <remote-name>. Also git remote rm.
What is the difference between git rebase and git merge in terms of history?
Answer: Merge preserves the exact timeline with merge commits; rebase rewrites history to appear linear, making it cleaner but dangerous on shared branches.
How do you list all tags?
Answer: git tag. git tag -l "v1.*" for pattern.
What is the purpose of git shortlog?
Answer: git shortlog summarizes git log output by author, grouping commits. Useful for release notes.
How do you find which branch contains a specific commit?
Answer: git branch --contains <commit-hash> lists branches that have the commit. git tag --contains for tags.
What is a “bare” clone and when would you use it?
Answer: Bare clone (git clone --bare) copies only the .git directory without a working tree. Used for central repositories, backup mirrors, or server-side repos.
How do you stage only parts of a file (hunks)?
Answer: git add -p (patch mode). It interactively asks to stage each hunk (chunk of changes). Very useful for splitting changes.
What is git blame -w?
Answer: -w ignores whitespace only changes when blaming.
How do you see the total number of commits?
Answer: git rev-list --count HEAD. Also git log --oneline | wc -l.
What is the difference between git switch and git checkout?
Answer: In newer Git, git switch is specifically for switching branches, while git checkout is overloaded (branch switching and file restoration). Use git switch to avoid confusion.
How do you create a new branch and switch to it in one command?
Answer: git checkout -b new-branch or git switch -c new-branch.
What is git restore used for?
Answer: git restore restores working tree files (undo changes). It replaces git checkout -- <file> for file restoration and git reset for staging. Example: git restore --staged file unstages.
How do you see the difference between two branches?
Answer: git diff branch1..branch2 or git diff branch1...branch2 (three dots shows differences between the common ancestor and branch2).
What is the purpose of git maintenance?
Answer: git maintenance automates repository optimization tasks (like git gc, git commit-graph, git pack-refs). Built-in cron or systemd timers.
How do you prune remote tracking branches that no longer exist?
Answer: git remote prune origin removes local references to remote branches that have been deleted. Also git fetch --prune.
What is git sparse-checkout?
Answer: It allows you to check out only a subset of files in a large repository, reducing disk usage. Useful for monorepos.
Why should we hire you as a Git expert?
Answer: I have deep understanding of Git’s object model, branching strategies, and advanced debugging tools like bisect and reflog. I can resolve complex merge conflicts, design efficient workflows (GitFlow, GitHub Flow), and teach best practices to teams. I also integrate Git with CI/CD pipelines and automate repetitive tasks with hooks and aliases. My problem-solving approach minimizes lost work and maximizes team productivity.
Conclusion
That wraps up 100 Git interview questions with clear, practical answers. You’ve covered everything from basic commits and branches to merging, rebasing, stash, cherry-pick, and handling real-world workflow scenarios. With this preparation, you’re not just memorizing commands — you understand how to manage version control confidently in a team environment. Keep this guide handy as a quick refresher before your interview. You’re ready to demonstrate your Git skills with clarity and precision. Good luck!