Git Interview Questions and Answers

Prepare for Git interview questions with straightforward answers and practical examples. This guide covers the Git fundamentals employers expect, the difference between common workflows, and safe ways to handle everyday problems.

Practice Git workflows →

Git basics interview questions

1. What is Git?

Git is a distributed version control system. It records project history as commits, helps developers work on changes in parallel, and lets them compare or combine work. Each clone contains the repository history needed for most local operations.

2. What is the difference between Git and GitHub?

Git is the version control software. GitHub is a web platform for hosting Git repositories and collaborating through features such as pull requests, code review, and issue tracking. Other platforms, such as GitLab and Bitbucket, also host Git repositories.

3. What is a repository?

A Git repository contains project files and the metadata Git uses to track history, branches, and other references. A working tree is the checked-out set of files you edit. A repository can be local or hosted on a remote service.

4. What is a commit?

A commit is a saved snapshot of tracked project content, along with metadata such as its author, message, and parent commit or commits. Git identifies commits by object IDs; a branch name is a movable reference to a commit.

Staging, commits, and HEAD

5. What is the staging area in Git?

The staging area, also called the index, holds the changes selected for the next commit. Use git add to stage changes and git diff --staged to review what is staged before committing.

6. What does HEAD mean in Git?

HEADis Git's reference to the current checkout, usually the currently checked-out branch. A branch normally points to its latest commit, so HEAD indirectly identifies that commit. In detached HEAD state, it points directly to a commit instead of a branch.

7. How do you change the last commit message?

If the commit is the current tip and has not been shared, run git commit --amend and edit the message. Amending creates a replacement commit, so its commit ID changes. Coordinate with your team before replacing a commit that others may already have based work on.

8. What is the difference between git diff and git diff --staged?

git diff shows unstaged changes between the working tree and the index. git diff --staged shows staged changes between the index and the current commit.

Branches and collaboration

9. What is a Git branch?

A branch is a lightweight, movable reference to a commit. Branches make it possible to develop a feature or fix separately from another line of work, then merge the changes when they are ready.

10. What is the difference between git fetch and git pull?

git fetch downloads remote updates and updates remote tracking references without integrating them into the current branch. git pull fetches and then integrates updates, typically by merging or rebasing depending on configuration and options. Fetching first gives you a chance to inspect incoming changes.

11. What is the difference between merging and rebasing?

Merging integrates histories and may create a merge commit, preserving the original branch structure. Rebasing replays commits onto a new base, creating new commit IDs and a more linear history. Avoid rebasing commits that teammates already rely on unless your team has agreed to rewrite that shared history.

12. What is a merge conflict?

A merge conflict occurs when Git cannot automatically reconcile changes, often because different changes overlap in a file. Inspect the conflict markers, decide what the final content should be, remove the markers, stage the resolved file, and complete the merge or rebase as instructed by Git.

Remotes and everyday commands

13. What is a remote in Git?

A remote is a named reference to another repository, often a hosted copy used for collaboration. origin is a common default name, not a special requirement. Use git remote -v to inspect configured remote URLs.

14. What is the difference between git push and git pull?

git push sends local commits to a remote repository. git pull retrieves remote updates and integrates them into the current branch. A push can be rejected if the remote branch has commits your local branch does not contain.

15. What is the purpose of .gitignore?

A .gitignore file tells Git which untracked paths to ignore, such as generated build output or local environment files. It does not stop tracking files that are already committed; those need to be removed from the index if the project should stop tracking them.

16. How do you inspect recent commits?

Run git log --oneline for a compact history, or git log --oneline --graph --decorate --all to view a graph of branches and references. Use git status to check the current branch and working tree state.

Recovery and safety questions

17. How do you undo a commit?

For an unshared commit, git reset can move the branch backward while keeping changes staged or unstaged, depending on the option. For a commit already shared with others, git revert is usually safer because it creates a new commit that reverses the change without rewriting existing history.

18. How can you save changes without committing them?

Use git stash push -m "short description" to save tracked changes temporarily. Add -u if you also need to include untracked files. Review git stash list before applying or dropping an entry.

19. What is git reflog used for?

The reflog records recent movements of local references such as HEAD. It can help locate a commit after a reset or branch change. Reflog entries are local and expire over time, so use them as a recovery aid rather than a permanent backup.

20. What would you do if a push is rejected as non-fast-forward?

First fetch and inspect the remote changes. Then integrate them by merging or rebasing according to the project workflow, resolve any conflicts, run relevant checks, and push again. Do not force-push blindly, especially to a shared branch.

How to prepare for a Git interview

Git interview questions FAQ

Which Git commands should I know for an interview?

Be comfortable with status, add, commit, log, diff, branch, switch, merge, fetch, pull, push, revert, and stash. Understand when and why you would use each command.

What is the most important Git concept to explain clearly?

Understand how the working tree, staging area, and commit history relate. That model helps explain what common commands change and makes troubleshooting scenarios easier to reason through.

Learn Git Faster by Practicing

Reading is just the first step. Real learning happens when you practice.

Start Learning Git →