REVIEWED STARTER COLLECTION

Git Interview Questions

Short answer se start karo, phir interview-level explanation, small workflow example aur common follow-up revise karo. Ye collection existing Git Commands Reference ko duplicate nahi karta.

1. What is the difference between the working tree, staging area and a commit?

Short Answer

The working tree contains your current files, the staging area selects changes for the next snapshot, and a commit records that staged snapshot in repository history.

Detailed Explanation

Editing a tracked file changes the working tree. git add copies selected changes into the index/staging area. git commit then creates a new commit from what is staged, not automatically from every file currently modified.

git status
git add app.js
git diff --staged
git commit -m "Update app behavior"
Review Git change commands →

2. What is a Git branch, and what happens when you merge one?

Short Answer

A branch is a movable name pointing to a commit. Merging combines another branch's history into the branch you currently have checked out.

Detailed Explanation

Branches let parallel lines of development move independently. A merge may simply move the current branch forward when possible, or create a merge commit when histories diverged. If Git cannot reconcile overlapping changes automatically, you resolve the conflict before completing the merge.

git switch -c feature/search
# make and commit changes

git switch main
git merge feature/search
Review branches & merging →

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

Short Answer

git fetch downloads remote refs and objects without integrating them into your current branch. git pull fetches and then integrates the configured upstream changes.

Detailed Explanation

Fetch is useful when you want to inspect remote updates before changing your local branch. Pull is a convenience workflow when you are ready for the fetch-plus-integration step and understand how your repository is configured to integrate.

git fetch
git log --oneline HEAD..origin/main

# When ready:
git pull
Review remote commands →

4. What is the difference between git restore and git revert?

Short Answer

git restore changes files in the working tree or staging area. git revert creates a new commit that reverses the effect of an earlier commit.

Detailed Explanation

Restore is commonly used before a commit—for example, to discard working-tree edits or unstage a file. Revert is history-preserving and is usually safer for undoing a commit that may already be shared with others.

# Unstage but keep working changes
git restore --staged app.js

# Reverse a committed change with a new commit
git revert <commit>
Review safe undo commands →

5. What is the difference between Git and GitHub?

Short Answer

Git is the distributed version-control system. GitHub is a hosting and collaboration platform built around Git repositories.

Detailed Explanation

You can use Git entirely on your own machine without GitHub. A service such as GitHub adds remote repository hosting and collaboration features around Git workflows, while Git itself handles commits, branches, merges and repository history.

# Local Git repository
git init

# Connect it to a hosted remote later
git remote add origin <repository-url>
git push -u origin main
Review repository basics →Review remotes →