#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

Interview Insights · Git

The Git Question That Ninety Percent of Candidates Fumble

It sounds like a beginner question: you pushed a bad commit, how do you undo it? Yet it trips up a huge share of candidates. Here is why, what a strong answer sounds like, and how to practise it until it feels obvious.

Tracks
Undo a Pushed Commit · Live Interactive
First command
—
What the candidate types
Team impact
—
What happens to others
My reading
—
What the interviewer sees
Bad commit→Pushed→Revert→Push fix
Click an answer to see what each approach does to a shared branch and what it tells me about the candidate.

Home / Tutorials / Career Guides / The Git Question That Ninety Percent of Candidates Fumble

Interview Insights · Git

The Git Question That Ninety Percent of Candidates Fumble

STEP 1STEP 2STEP 3STEP 4Find commitgit log --onelineCopy the SHAIdentifyIs it shared?Pushed to remote?Others pulled it?DecidePick toolShared: revertLocal: resetActVerifygit log + diffThen pushConfirm
Four stages of the question: spot the bad commit, check whether it is shared, pick the safe tool, then push and confirm.

Quick summary — Undoing a pushed commit

The question is not about memorising a command. It is about whether you protect your teammates' history. When I ask 'you pushed a wrong commit to the main branch, how do you undo it?', most candidates reach for git reset --hard and a force push. That works on a solo project and causes real damage on a shared one. The answer I want starts with a question back: has anyone else pulled it? If yes, use git revert, which adds a new commit that undoes the change. Reset is for commits that never left your machine.

In this guide you will learn:

  1. Why this simple question catches so many candidates off guard
  2. The real difference between git reset and git revert
  3. How the three reset modes soft, mixed and hard behave
  4. How git reflog saves you after a bad reset
  5. A model answer you can say in under a minute

SECTION 01Why such a simple Git question trips people up

Most freshers learn Git alone. They create a repository, push to their own GitHub, and nobody else ever pulls from it. In that world, rewriting history costs nothing, so git reset --hard followed by git push --force feels like the natural undo button. Tutorials often show it that way too.

In a company, the main branch is shared by many developers and usually by a CI pipeline. If you rewrite a commit that others have already pulled, their local branches no longer match the remote. The next time they pull, they get confusing conflicts or, worse, they push your bad commit back. So when I ask this question, I am really testing one thing: do you think about the team before you type?

Common answerWhat it doesProblem on a shared branchWhat it signals
git reset --hard HEAD~1 then push --forceDeletes the commit and rewrites remote historyTeammates' clones diverge from the remoteOnly used Git alone
Delete the repo and clone againThrows away local stateDoes not undo anything on the remoteFear of Git, no mental model
Edit the file and commit againAdds a manual fixWorks, but easy to miss parts of the changePractical but imprecise
git revert SHA then pushAdds a new commit that reverses the bad oneNone, history stays linear and honestUnderstands shared history
Interviewer: You pushed a commit to main that broke the build.
How do you undo it?

Candidate: I will do git reset --hard HEAD~1
and then git push -f.

Interviewer: Three other developers already pulled main.
What happens to them?

Candidate: ... they can also reset?
undo-pushed-commit.txt

Notice that the strong candidate asks the important question before choosing a command. That habit matters more to me than knowing every flag.

Key insight: Rule of thumb: if the commit is only on your machine, reset is fine. If it has reached a shared remote branch, revert it. Saying this rule out loud already puts you ahead of most candidates.

SECTION 02Reset and revert: what each one actually changes

To answer confidently, you need a mental model, not a memorised line. Git has three places your work lives: the working directory (files on disk), the staging area or index (what will go into the next commit), and the commit history (what HEAD and the branch point to).

git reset moves the branch pointer backwards, and depending on the mode it also resets the index and the working directory. git revert never moves the pointer back. It reads the bad commit, computes the opposite change, and records that as a brand new commit. That is why revert is safe on shared branches: it only adds history, it never removes it.

CommandBranch pointerStaging areaWorking files
git reset --soft HEAD~1Moves back one commitKeeps the changes stagedUntouched
git reset --mixed HEAD~1 (default)Moves back one commitUnstages the changesUntouched, changes still there
git reset --hard HEAD~1Moves back one commitReset to match the commitChanges are discarded
git revert SHAMoves forward with a new commitUpdated by the new commitUpdated to the reversed state
# Practise in a throwaway repo
git init reset-lab && cd reset-lab
echo "v1" > app.txt && git add . && git commit -m "v1"
echo "v2" > app.txt && git commit -am "v2"

git reset --soft HEAD~1
git status            # app.txt is staged with v2

git commit -m "v2 again"
git reset --mixed HEAD~1
git status            # app.txt modified, not staged

git commit -am "v2 once more"
git reset --hard HEAD~1
cat app.txt           # v1, the v2 change is gone
reset-vs-revert.sh

If the bad commit is a merge commit, revert needs to know which parent is the main line. That is what git revert -m 1 MERGE_SHA does. Mentioning this as a follow-up shows real experience, but do not lead with it.

Remember: Reverting a revert is allowed. If you revert a commit and later want the change back, you can run git revert on the revert commit. History tells the full story, which reviewers appreciate.

SECTION 03When things already went wrong: reflog and safer force pushes

A good follow-up question from me is: 'Suppose you already ran git reset --hard by mistake and lost a commit. Can you get it back?' Many candidates say no. The answer is usually yes, because Git keeps a local log of where HEAD has pointed, called the reflog. Commits that no branch points to are not deleted straight away, so you can find their SHA and restore them.

The second follow-up is about force pushing. Sometimes rewriting history is correct, for example cleaning up your own feature branch before a pull request. In that case, git push --force-with-lease is the safer form. It refuses to overwrite the remote if someone else pushed in the meantime.

SituationToolCommandSafe on shared branch?
Lost a commit after reset --hardRefloggit reflog then git reset --hard SHALocal recovery only
Want to rewrite your own feature branchForce with leasegit push --force-with-leaseOnly if nobody else uses the branch
Bad commit already on mainRevertgit revert SHAYes
Wrong message on last local commitAmendgit commit --amendOnly before pushing
git reset --hard HEAD~1     # oops, lost a good commit

git reflog
# 7a1d4b0 HEAD@{0}: reset: moving to HEAD~1
# 3f9e1c2 HEAD@{1}: commit: add login validation
# 7a1d4b0 HEAD@{2}: commit: v2

git reset --hard 3f9e1c2    # back to the lost commit
# or keep it on a new branch instead:
git branch rescue 3f9e1c2
reflog-recovery.sh

Note that git rebase -i opens an editor, so in an interview you can simply describe it: 'I would squash my small fixup commits interactively, then push with force-with-lease because it is my branch.'

Pro move: End your answer with prevention: 'We also protect main with branch rules so force pushes are blocked and changes come through reviewed pull requests.' It shows you think about process, not only commands.
How Uncodemy helps: Uncodemy trainers cover Git interview questions as part of the DevOps Training Course: project reviews, mock interviews and resume feedback, with placement assistance once you are interview-ready.

SECTION 04Practice lab: make the undo answer automatic

Reading about reset and revert is not enough. You need to break a repository on purpose and fix it a few times, so that the right answer comes out calmly under pressure. Use these three exercises in a throwaway folder, never in a real project.

SIMULATE A TEAM ON ONE LAPTOP

1. git init --bare team.git
2. git clone team.git alice
3. git clone team.git bob
4. In alice: commit and push two commits.
5. In bob: git pull.
6. In alice: git reset --hard HEAD~1 and push --force.
7. In bob: make a commit and try git pull.
   Observe the confusion.
8. Repeat, but in step 6 use git revert instead.
   Observe that bob pulls cleanly.

Write one line about what you saw each time.
git-undo-lab.md

SECTION 05Habits that make Git questions feel easy

These habits help you answer the undo question and its many cousins without guessing.

  • Ask about the branch first — Before naming any command, ask or state whether the work is local, on your own branch, or on a shared branch. That one question shapes the whole answer.
  • Run git status constantly — Candidates who read git status output aloud during live tasks look calm, because they always know what state the repository is in.
  • Keep a scratch repo — Have a small test repository where you can try any command safely. Experimenting there builds the mental model faster than any video.
  • Learn what each command moves — For every command, know whether it changes the branch pointer, the staging area or the working files. Most Git confusion comes from mixing these up.
  • Prefer adding over deleting — On shared history, a new commit that fixes things is almost always safer than removing old commits. Say this principle in your answer.
Pro tip: When an interviewer asks a Git question, describe the state of the repository before and after your command. Interviewers trust candidates who can picture the commit graph.

SECTION 06Interview Q&A — Undoing a pushed commit

Q1What is the difference between git reset and git revert?

git reset moves the branch pointer back to an earlier commit and, depending on the mode, also resets the staging area and working files. It rewrites history. git revert creates a new commit that applies the opposite of an earlier commit. It keeps history intact, so it is the safe choice for commits already pushed to a shared branch.

Q2When is it acceptable to force push?

Only on a branch that nobody else is working on, such as your own feature branch before a pull request is merged. Even then, I use git push --force-with-lease, which refuses to overwrite the remote if someone pushed new commits since my last fetch. On main or release branches, I would not force push at all.

Q3How do you undo a merge commit that was pushed?

I would use git revert -m 1 MERGE_SHA. The -m 1 tells Git that the first parent, usually main, is the line to keep, so the revert removes the changes that came in from the merged branch. If the branch needs to be merged again later, I may need to revert the revert first.

Q4You ran git reset --hard and lost work. What now?

If the work was committed, I run git reflog to see where HEAD pointed before the reset, then create a branch or reset to that SHA. If the changes were never committed and never staged, Git has no record of them, so they are usually gone. That is why I commit small and often.

Q5What does git reset --soft HEAD~1 do?

It moves the branch pointer back by one commit but keeps all the changes from that commit staged. It is useful when I want to redo the last local commit, for example to split it or change the message along with some files, before pushing.

SECTION 07Test yourself — Undoing a pushed commit quiz

Five questions. No sign-up.

0 / 5

Pick an answer to see why it is right or wrong.

SECTION 08Frequently asked questions

How do I undo a git commit that is already pushed?

If the branch is shared, run git revert COMMIT_SHA and push. This adds a new commit that cancels the old one without rewriting history. If the branch is only yours and nobody pulled it, you can reset and use git push --force-with-lease.

Is git revert the same as git reset?

No. Reset moves the branch back and can discard commits, which rewrites history. Revert keeps all existing commits and adds a new one that undoes a specific change. Revert is designed for shared branches.

Can I recover a deleted commit in Git?

Usually yes, if it was committed. git reflog shows recent positions of HEAD, including commits that no branch points to anymore. Find the SHA and create a branch at it. Uncommitted changes that were never staged generally cannot be recovered this way.

What Git questions are asked in fresher interviews?

Common ones include the difference between merge and rebase, reset versus revert, how to resolve a merge conflict, what git stash does, and how you use branches and pull requests in a team. Interviewers care more about your reasoning than the exact flags.

What is the difference between git reset soft, mixed and hard?

All three move the branch pointer. --soft keeps changes staged, --mixed (the default) keeps changes in your files but unstages them, and --hard discards the changes from both the staging area and the working directory.

Does Uncodemy help with Git interview questions?

Yes. Uncodemy's DevOps Training Course in Noida (classroom and online) includes live projects, one-to-one mock interviews and resume reviews, so you practise Git interview questions with trainer feedback before the real interview. You can book a free demo class from this page to see how the sessions work.

Classroom & online · Noida

Make Git the round you never worry about

Our DevOps Training Course covers Git workflows hands-on, from branching and pull requests to reverts and recovery, alongside CI/CD and cloud tools.

Talk to a counsellor for current fees
  • Hands-on Git labs
  • Team branching workflows
  • CI/CD pipeline projects
  • Weekday & weekend batches