All Git Interview Questions

How do you undo a pushed commit in Git?

It depends on who may have already taken your commit.

  • Shared branch (like main): use git revert <commit> and then git push. History is not rewritten, so it is safe for everyone.
  • Your own branch, nobody else uses it: you can use git reset --hard HEAD~1 and then git push --force-with-lease.
git revert HEAD
git push

# only on your own private branch
git reset --hard HEAD~1
git push --force-with-lease

See Git Revert and Git Push.

What is the difference between force push and force with lease in Git?

Both replace the remote branch with your local branch. The difference is the safety check.

–force–force-with-lease
Overwrites the remote branch in any caseOverwrites only if the remote branch is what you last fetched
Can delete commits pushed by your teammatesFails if someone else pushed new commits meanwhile
RiskyMuch safer

Use force push only on your own feature branches after you rewrote the history (rebase or amend), and prefer --force-with-lease. Never force push to main. See Git Push.

What are Git hooks?

Git hooks are scripts in the folder .git/hooks which Git runs before or after events such as commit, merge or push. If a “pre” hook exits with an error, Git stops the action.

HookRunsUse
pre-commitBefore a commitLint or test the code
commit-msgAfter the message is writtenCheck the message format
pre-pushBefore a pushRun the full tests

Hooks are not shared by git push. Teams keep them in a folder and use git config core.hooksPath .githooks, or tools like Husky. See Git Hooks.

What is Git Flow?

Git Flow is a branching model that uses fixed types of branches:

  • main: only released code, tagged with versions.
  • develop: the next release is prepared here.
  • feature/*: new features, created from and merged back to develop.
  • release/*: final fixes before a release.
  • hotfix/*: urgent fixes created from main.

It is good for software with planned versions, but it is heavy for teams which deploy many times a day. Those teams often use a simple feature branch workflow or trunk based development. See Git Branching Strategy.

What is git blame?

Terminal output

$ git blame page.txt
^555961f (Robin Sharma 2026-01-10 10:42:00 +0000 1) Title
a88d5160 (Robin Sharma 2026-01-10 10:56:00 +0000 2) Line 2 updated
a88d5160 (Robin Sharma 2026-01-10 10:56:00 +0000 3) Line 3

Each line shows the short commit hash, the author, the date and the line. With the hash, run git show <hash> to see the full change and message. In VS Code and GitHub, the “blame” view shows the same.

What is git bisect?

git bisect finds the commit that broke something. You tell Git one “bad” commit (where the bug exists) and one “good” commit (where it worked). Git checks out a commit in the middle, you test it and mark it, and Git halves the range each time.

It can even run a test script for you with git bisect run. In this example, a file starts to contain the word BUG in some commit:

The test script test.sh fails (exit code 1) when the file has BUG:

#!/bin/sh
if grep -q BUG status.txt; then exit 1; fi
exit 0
Terminal output

$ git bisect start
status: waiting for both good and bad commits
$ git bisect bad HEAD
status: waiting for good commit(s), bad commit known
$ git bisect good HEAD~7
Bisecting: 3 revisions left to test after this (roughly 2 steps)
[c9f3864712df8e8c2a36c5713f79b4886088788e] Commit 4
$ git bisect run ./test.sh
running './test.sh'
Bisecting: 1 revision left to test after this (roughly 1 step)
[ec5695fc6c5e58f2148f2bee260b170d5bd4c2bf] Commit 6
running './test.sh'
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[ff72f6258a3d508a43c3e2b650038aaf9b6797aa] Commit 5
running './test.sh'
ff72f6258a3d508a43c3e2b650038aaf9b6797aa is the first bad commit
commit ff72f6258a3d508a43c3e2b650038aaf9b6797aa (HEAD)
Author: Robin Sharma <robin@example.com>
Date: Sat Jan 10 11:03:00 2026 +0000

Commit 5

status.txt | 1 +
1 file changed, 1 insertion(+)
bisect found first bad commit
$ git bisect reset
Previous HEAD position was ff72f62 Commit 5
Switched to branch 'main'

Git found “Commit 5” as the first bad commit. git bisect reset brings you back to the original branch.