Ravindra BagaleCourses & study guides मराठी Track your progress

Guides

How to Undo a Git Commit Safely (reset, revert, amend)

Undo local commits with reset or amend; undo shared commits with git revert. Never blind force-push main.

Friends, undo tools are different knives. Chala mitrano — local reset is fine; shared history needs revert.

Quick answer

Hold this vertical picture:

  1. Ask local vs pushed.
  2. Local: reset or amend.
  3. Shared: git revert.
  4. Use reflog if lost.
  5. Verify with status and log.

Language: English · मराठी · हिंदी

Wrong commit?Local or already pushed? Local onlyreset or amend Already on remotegit revert Never blind forceProtect shared main Verifylog and status
Undo safely

Why undo carefully

A bad commit on shared main at Accenture cannot be erased with reset without hurting teammates.

Why: Local history rewrite is private; shared history needs a new commit. What: reset, amend, revert — three tools. How: Ask: has anybody else pulled this tip?

Example 1: Lab with three commits

Type this:

rm -rf /tmp/undo-lab && mkdir -p /tmp/undo-lab && cd /tmp/undo-lab && git init -b main && git config user.email lab@example.com && git config user.name Lab && printf 'a\n' > f && git add f && git commit -m 'c1' && printf 'b\n' >> f && git commit -am 'c2' && printf 'BAD\n' >> f && git commit -am 'c3-bad' && git log --oneline

You will see:

... c3-bad\n... c2\n... c1

What it means: Three commits; c3-bad is the mistake we will undo in different ways.

Example 8: Decision: local vs pushed

Type this:

printf '%s\n' 'LOCAL not pushed: reset or amend OK' 'PUSHED shared: git revert' 'PUSHED solo branch: reset + force-with-lease only if alone'

You will see:

LOCAL not pushed: reset or amend OK\nPUSHED shared: git revert\nPUSHED solo branch: reset + force-with-lease only if alone

What it means: Say this card in Infosys interviews.

Concept 1: reset locally

soft / mixed / hard move HEAD; hard also wipes the worktree.

Why: You need to drop a local tip safely. What: git reset --soft|--mixed|--hard HEAD~1 How: Use hard only in labs or disposable work.

Example 2: Soft reset keeps files staged

Type this:

cd /tmp/undo-lab && git reset --soft HEAD~1 && git status -sb && git log --oneline -n 2

You will see:

## main\nM  f   (or staged changes)\n... c2

What it means: soft moves HEAD back; changes stay staged. Good before re-commit.

Example 3: Restore the bad commit for next demos

Type this:

cd /tmp/undo-lab && git commit -m 'c3-bad' && git log --oneline -n 3

You will see:

... c3-bad\n... c2\n... c1

What it means: Put c3-bad back so mixed and hard demos have a target.

Example 4: Mixed reset unstages

Type this:

cd /tmp/undo-lab && git reset HEAD~1 && git status -sb && git log --oneline -n 2

You will see:

## main\n M f\n... c2

What it means: Default mixed reset: HEAD back, changes unstaged in working tree.

Example 5: Hard reset discards worktree (lab only)

Type this:

cd /tmp/undo-lab && git add -A && git commit -m 'c3-bad' >/dev/null; git reset --hard HEAD~1 && git log --oneline -n 2 && git status -sb

You will see:

... c2\n## main

What it means: hard deletes the commit and working changes. Dangerous on real work — lab only.

Concept 2: amend and revert

amend rewrites tip; revert adds an undo commit.

Why: Pushed shared tips must not be rewritten casually. What: git commit --amend; git revert HEAD How: Prefer revert on main used by TCS teams.

Example 6: Amend last local commit message

Type this:

cd /tmp/undo-lab && printf 'x\n' >> f && git add f && git commit -m 'typo-msg' && git commit --amend -m 'clear message' && git log --oneline -n 1

You will see:

... clear message

What it means: amend rewrites the tip commit. Safe only if not pushed yet.

Example 7: Revert creates a new undo commit

Type this:

cd /tmp/undo-lab && printf 'oops\n' >> f && git commit -am 'bad-on-main' && git revert --no-edit HEAD && git log --oneline -n 3

You will see:

... Revert "bad-on-main"\n... bad-on-main\n...

What it means: revert adds a forward commit that undoes changes — safe for shared main.

Concept 3: recover and verify

reflog finds lost tips; always verify.

Why: Panic resets need a recovery map. What: git reflog; git status; git log How: Rotate secrets if they were committed.

Example 9: Recover a soft-reset tip with reflog

Type this:

cd /tmp/undo-lab && git reflog -n 5

You will see:

HEAD@{0}: ...\nHEAD@{1}: ...

What it means: reflog remembers where HEAD was — recovery map after reset.

Example 10: Do not chmod secrets into history

Type this:

printf '%s\n' 'If a secret was committed: rotate the secret, then rewrite history with team help — do not leave it'

You will see:

If a secret was committed: rotate the secret, then rewrite history with team help — do not leave it

What it means: Undo is not enough for leaked passwords — rotate first.

Example 11: Verify after undo

Type this:

cd /tmp/undo-lab && git status -sb && git log --oneline -n 4

You will see:

## main\n...

What it means: Always status + log after undo tools.

Example 12: Undo card

Type this:

printf '%s\n' '1 local? reset/amend 2 shared? revert 3 verify log 4 never blind force main'

You will see:

1 local? reset/amend 2 shared? revert 3 verify log 4 never blind force main

What it means: Memorize for Flipkart-style service repos.

Undo card\n\n1. Not pushed → reset or amend\n2. Pushed shared → git revert\n3. Verify with status and log\n4. Never blind force-push main\n\n### Suppose: Wrong message on local tip\ngit commit --amend -m "clear message"\n\n### Suppose: Bad commit already on remote main\ngit revert HEAD; push the revert.

Ravindra Bagale's Tip

On Accenture shared main, prefer git revert. Soft reset and amend stay on your laptop until you push.

Check yourself (quiz)

Q1. Soft vs hard reset?

Soft keeps changes staged; hard discards worktree changes

Q2. Safe undo on shared main?

git revert

Q3. When is amend safe?

When the tip was never pushed (or only you use the branch)

Q4. What finds a lost tip?

git reflog

Q5. Force-push shared main?

No without written team rules

Q6. Secret in a commit?

Rotate the secret first, then fix history with the team

Reading tracking

Mark this guide when you finish. Your browser remembers it on this device.

Local → reset/amend. Shared → revert. Verify. No blind force on main. Did you understand? Let us try it once more.

Frequently asked questions

Soft vs hard reset?

Soft keeps changes staged; hard resets the worktree too.

How to undo on shared main?

git revert creates a new commit.

When can I amend?

When the commit was not pushed, or only you use the branch.

What is reflog for?

Finding commits after a reset.

Force-push main?

Not without a written team rule.

Secret committed?

Rotate the secret, then fix history with the team.