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

Guides

Git Commit सुरक्षित Undo कैसे करें (reset, revert, amend)

Local: reset/amend। Shared: git revert। Main पर blind force-push नहीं।

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

Quick answer

Hold this vertical picture:

  1. Local vs pushed पूछें।
  2. Local: reset/amend।
  3. Shared: revert।
  4. reflog।
  5. status+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

Undo सावध क्यों

Accenture shared main पर गलत commit reset से न मिटाएँ — teammates दुखते हैं।

Why: Local rewrite निजी; shared को नया commit। What: reset, amend, revert। How: क्या दूसरों ने tip pull की?

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: Local reset

soft/mixed/hard HEAD हिलाते हैं; hard worktree मिटाता है।

Why: Local tip सुरक्षित हटाना। What: git reset --soft|--mixed|--hard HEAD~1 How: Hard सिर्फ lab।

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 और revert

amend tip rewrite; revert undo commit जोड़ता है।

Why: Shared tip हल्के में rewrite न करें। What: commit --amend; revert HEAD How: TCS main पर revert।

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 और verify

reflog खोई tip ढूँढता है।

Why: Panic के बाद नक्शा चाहिए। What: reflog; status; log How: Secret हो तो rotate।

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/amend\n2. Shared → revert\n3. Verify\n4. Blind force main नहीं\n\n### Suppose: Local गलत message\namend -m\n\n### Suppose: Remote main पर bad commit\nrevert; push।

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?

Soft staged रखता है; hard worktree मिटाता है

Q2. Shared main सुरक्षित undo?

git revert

Q3. Amend कब सुरक्षित?

Tip push न हुई हो

Q4. खोई tip?

git reflog

Q5. Force-push main?

नहीं

Q6. Secret commit?

पहले rotate, फिर team के साथ history

Reading tracking

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

Local → reset/amend। Shared → revert। Verify। 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.