Interview questions with answers
Git commands interview questions and answers
Branching, merging, rebasing, undoing and recovering work. Open any question to read its model answer; the multiple-choice set marks the right option and explains it.
- Subjective
- 30
- Objective
- 30
Subjective questions 30
1What is the difference between git merge and git rebase?Asked almost every time
Merge joins two branches with a merge commit, keeping the exact history of both. Rebase replays your commits on top of another branch, producing a linear history with new commit IDs. Rebase your own local work to tidy it up; never rebase commits others have already pulled, because it rewrites shared history.
2Explain the working directory, staging area and repository.Asked almost every time
The working directory holds your files. git add moves chosen changes into the staging area (index), which describes the next commit. git commit saves the staged snapshot into the repository's history. This lets you commit part of your changes deliberately.
3How do you undo changes at different stages?Asked almost every time
Discard unstaged edits: git restore <file>. Unstage: git restore --staged <file>. Change the last commit: git commit --amend. Undo a commit locally: git reset --soft HEAD~1 (keep changes staged) or --hard (discard them). Undo a pushed commit safely: git revert <sha>, which adds a new commit reversing it.
4git fetch vs. git pull?Asked almost every time
fetch downloads new commits and updates remote-tracking branches (such as origin/main) without touching your work. pull is fetch followed by merge (or rebase with --rebase) into your current branch. Fetching first lets you inspect what changed.
5What is a commit object made of?Asked almost every time
A snapshot of the project (a tree object pointing to blobs and sub-trees), the parent commit or commits, author and committer details with timestamps, and the message. Its SHA is a hash of all this, so changing anything gives a new commit ID.
6What is interactive rebase used for?Asked almost every time
git rebase -i HEAD~5 opens a list of commits where you can reorder, squash, fixup, edit, reword or drop them, to clean up local history before sharing. Avoid rewriting commits others already pulled.
7How do you keep a feature branch up to date with main?Asked almost every time
Either merge main into it (git merge main), which preserves history with merge commits, or rebase it onto main (git rebase main), which keeps a linear history but rewrites your branch. Rebase only branches nobody else is working on, then push with --force-with-lease.
8How do you resolve a merge conflict?Very often asked
Git marks the conflicting sections with <<<<<<<, ======= and >>>>>>>. Edit the file to the correct result, remove the markers, git add it, then git commit (or git rebase --continue). git status lists conflicted files, and git merge --abort returns to the state before the merge.
9What is git stash for?Very often asked
It saves uncommitted changes and cleans the working directory so you can switch branches or pull. git stash pop reapplies and removes the latest stash; git stash list shows them; git stash push -m "msg" -- path stashes specific files; -u includes untracked files.
10How do you recover a commit you lost after a reset?Very often asked
git reflog lists where HEAD and branches pointed recently, including commits no branch references. Find the commit ID, then git branch rescue <sha> or git reset --hard <sha>. Reflog entries last about 90 days by default.
11What does git cherry-pick do?Very often asked
It applies the changes of a specific commit onto the current branch as a new commit, for example to bring one bug fix from main to a release branch. Ranges can be picked with A^..B.
12How would you squash several commits into one?Very often asked
Interactive rebase: git rebase -i HEAD~3, change pick to squash (or fixup) for the commits to fold in, and edit the message. Alternatively, git reset --soft HEAD~3 then commit once. Many teams simply squash-merge pull requests.
13What is a fast-forward merge?Very often asked
When the target branch has no new commits since the feature branch started, Git can just move its pointer forward to the feature's tip, with no merge commit. --no-ff forces a merge commit to record that a branch existed.
14How do you change the last commit?Very often asked
Stage the fix and run git commit --amend (use --no-edit to keep the message). It replaces the commit with a new one, so only do it before pushing, or force-push carefully if it was your own branch.
15What are remotes and upstream branches?Very often asked
A remote is a named URL of another repository (usually origin). Remote-tracking branches like origin/main record its state at the last fetch. An upstream branch links a local branch to a remote one so git push and git pull know where to go.
16What does git revert do differently from git reset?Very often asked
revert creates a new commit that undoes an earlier one, keeping history intact, which is safe for shared branches. reset moves the branch pointer back (optionally changing the index and working tree), rewriting history, which is only safe for local work.
17How do Git hooks work?Very often asked
Scripts in .git/hooks that run at events such as pre-commit, commit-msg and pre-push, for linting, tests or message checks. They are not shared by cloning, so teams use tools like Husky or lefthook to install them.
18What are submodules and their downsides?Very often asked
A submodule embeds another repository at a fixed commit inside yours. It keeps projects separate, but needs extra commands to clone and update (--recurse-submodules), is easy to get out of sync and confuses contributors; monorepos or packages are often simpler.
19What is a detached HEAD?Often asked
HEAD points directly at a commit instead of a branch, for example after git checkout <sha> or a tag. New commits are not on any branch and can be lost; create a branch with git switch -c <name> to keep them.
20How do you find which commit introduced a bug?Often asked
git bisect start, mark a bad commit and a known good one, and Git checks out midpoints for you to test (git bisect good / bad) until it finds the first bad commit. git bisect run <script> automates it. git log -S and git blame help too.
21What branching strategies do teams use?Often asked
Trunk-based development: short-lived branches merged into main often, with feature flags. GitHub flow: branch, pull request, review, merge to main, deploy. Git Flow: long-lived develop and release branches, suited to versioned releases but heavier. Most web teams use trunk-based or GitHub flow.
22What is the .gitignore file and how do you stop tracking a file already committed?Often asked
.gitignore lists patterns Git should not track, such as node_modules/ and .env. Ignoring does not untrack committed files; run git rm --cached <file>, commit, and keep it in .gitignore. If it was a secret, rotate it, because it remains in history.
23What does git log --oneline --graph --all show?Often asked
A compact, one-line-per-commit history of every branch drawn as an ASCII graph, useful for seeing how branches diverged and merged.
24What is git worktree?Often asked
It checks out another branch into a separate directory linked to the same repository, so you can work on two branches at once (for example a hotfix) without stashing or cloning again.
25How do you sign commits and why?Often asked
Configure a GPG or SSH signing key and use git commit -S (or commit.gpgsign true). Platforms show a 'Verified' badge, proving the commit came from the key owner, which matters for supply-chain security.
26How do you deal with large binary files in Git?Often asked
Use Git LFS, which stores pointers in the repository and the files on a separate server, keeping clones small. Avoid committing build outputs or datasets; store them elsewhere.
27What does git clean do?Often asked
It deletes untracked files from the working tree: -n previews, -f deletes, -d includes directories and -x also removes ignored files. It cannot be undone, so preview first.
28How do you remove a secret that was committed?Often asked
Rotate the secret immediately (it must be treated as leaked), then rewrite history with git filter-repo or BFG to remove it, force-push, and ask collaborators to re-clone. Add scanning (pre-commit hooks, push protection) to prevent repeats.
29What are tags and how are they used?Sometimes asked
Tags mark specific commits, usually releases (v1.4.0). Annotated tags (git tag -a) store a message, author and date and are recommended for releases; lightweight tags are just names. Push them with git push origin v1.4.0 or --tags.
30What is a shallow clone?Sometimes asked
git clone --depth 1 fetches only recent history, which is faster and smaller for CI builds. It limits commands that need full history; git fetch --unshallow gets the rest.
Objective questions 30
1Which command safely undoes a commit that is already pushed?Asked almost every time
Why: Revert adds a new commit and does not rewrite shared history.
2Which command shows the history of where HEAD has been?Asked almost every time
Why: Reflog records HEAD movements, even for lost commits.
3git pull is equal to…Asked almost every time
Why: Pull fetches and integrates.
4Which command lets you squash and reorder recent commits?Asked almost every time
Why: Interactive rebase.
5Which command fixes the most recent commit?Asked almost every time
Why: It replaces the last commit.
6Which is safe on a shared branch to undo a commit?Asked almost every time
Why: History is preserved.
7Which command stages changes for the next commit?Very often asked
Why: add moves changes to the staging area.
8Which command creates and switches to a new branch?Very often asked
Why: switch -c (or checkout -b) creates and switches.
9What does git reset --soft HEAD~1 do?Very often asked
Why: Soft reset moves the branch back and keeps changes staged.
10Which command temporarily shelves uncommitted changes?Very often asked
Why: Stash stores changes and cleans the working directory.
11A fast-forward merge…Very often asked
Why: Possible when the target has no new commits.
12What does origin/main represent?Very often asked
Why: A remote-tracking branch.
13Which hook runs before a commit is created?Very often asked
Why: Used for linting.
14Which tool stores large files outside the repository?Very often asked
Why: Pointers stay in Git.
15What must happen first when a secret is committed?Very often asked
Why: Assume it is leaked.
16Which command applies one commit from another branch?Often asked
Why: Cherry-pick copies a single commit's changes.
17Which command finds the commit that introduced a bug by binary search?Often asked
Why: Bisect halves the range each step.
18How do you stop tracking a committed file but keep it locally?Often asked
Why: --cached removes it from the index only.
19Which shows who last changed each line of a file?Often asked
Why: Blame annotates lines with commit and author.
20Which command checks out another branch in a separate folder?Often asked
Why: Linked to the same repository.
21Which flag previews what git clean would delete?Often asked
Why: A dry run that only lists files.
22Which clone fetches only the latest commit?Often asked
Why: A shallow clone.
23Which flag forces a merge commit?Often asked
Why: Records the branch.
24What does a commit's tree point to?Often asked
Why: A full snapshot.
25Which tool rewrites history to remove a file?Often asked
Why: Recommended over filter-branch.
26Which flag lets git push overwrite a remote branch only if no one else pushed meanwhile?Sometimes asked
Why: It refuses if the remote has commits you have not seen.
27Which command shows changes that are staged but not committed?Sometimes asked
Why: Plain git diff shows unstaged changes.
28Which command discards unstaged changes to a file?Sometimes asked
Why: restore resets the working copy to the index.
29Which flag signs a commit?Sometimes asked
Why: -s adds a Signed-off-by line instead.
30Which command lists remotes with URLs?Sometimes asked
Why: Shows fetch and push URLs.
Track what you have practised and search every bank in your dashboard.
Start learning