Lesson 10 / الدرس 10
Undoing: restore, reset and revert / التراجع: restore وreset وrevert
Three commands with similar names that do very different things. Choosing between them is entirely a question of which of the three places you are undoing, and whether anyone else has seen it.
ثلاثة أوامر بأسماء متشابهة تفعل أشياء شديدة الاختلاف. والاختيار بينها كله سؤال عن أي المواضع الثلاثة تتراجع عنه، وهل رآه أحد غيرك.
This is the lesson people look up in a panic, so it is arranged as an answer key. Find your situation, use the command beside it. The reasoning underneath is always the same question: which of the three places from lesson 4 holds the thing you regret?
| You want to | Command | Safe? |
|---|---|---|
| Unstage a file | git restore --staged file | Yes — nothing is lost |
| Discard edits to a file | git restore file | NO — the edits are gone |
| Fix the last commit message | git commit --amend | Only if not pushed |
| Undo the last commit, keep the changes | git reset --soft HEAD~1 | Only if not pushed |
| Undo the last commit and the changes | git reset --hard HEAD~1 | NO — and only if not pushed |
| Undo a commit that others have | git revert 3f9a21c | Yes — always |
| Get back a file as it was in a commit | git restore --source 3f9a21c file | Overwrites your copy |
Read the third column before the second. Two of these destroy work permanently, and they are one word away from the ones that do not.
reset and revert are opposites
Before: A ── B ── C (C is the commit you regret)
reset: A ── B C is removed from history — as if it never happened
revert: A ── B ── C ── D D is a NEW commit that undoes C. Both are in the history.
reset rewrites history, which is fine alone and a disaster shared. revert adds to history, which is always safe because nothing anyone else has is changed. Run the demonstration for the rule in one line.
reset يعيد كتابة التاريخ، وهو مقبول وحدك وكارثة مشتركًا. وrevert يضيف إلى التاريخ، وهو آمن دائمًا لأن لا شيء عند غيرك يتغير. شغّل العرض للقاعدة في سطر واحد.git reset --hard وgit restore file هما الأمران في هذه الدورة اللذان يدمّران العمل بشكل دائم، ولا يطلب أي منهما تأكيدًا. فهما يحذفان تغييرات لم تُودَع قط، ولأنها لم تُودَع فلا نسخة لدى git — وهذا هو الموقف الوحيد الذي لا يستطيع git فيه مساعدتك فعلًا. وقبل أي منهما، نفّذ git status واقرأ ما توشك على فقدانه.The safety net for everything else
Anything that was committed is recoverable, even after a reset --hard that appeared to delete it. git reflog lists every position HEAD has been in, including ones no longer reachable from any branch — so the commit you thought you destroyed is usually sitting there with its identifier, and git reset --hard 3f9a21c brings it back.
reset --hard بدا أنه حذفه. فـgit reflog يسرد كل موضع كانت فيه HEAD، بما فيها مواضع لم تعد تُبلغ من أي branch — فالـcommit التي ظننت أنك دمّرتها جالسة هناك بمعرّفها عادةً، وgit reset --hard 3f9a21c يعيدها.That is the practical summary of this whole lesson, and it is worth saying plainly: commit early and often, and almost nothing can be lost. The commands that destroy work only destroy work that was never committed. Everything else has a way back, even when the situation looks final.
↺ بجانب ملف متغيّر في Source Control هي git restore — وهي تطرح تعديلاتك بشكل دائم، وهي أيقونة صغيرة بجانب أيقونات غير مؤذية. عاملها بالحذر الذي يستحقه الأمر. وقائمة … تعرض أيضًا "Undo Last Commit"، وهي git reset --soft HEAD~1.Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1. A commit you made is on GitHub and others have it. How do you undo it?
reset rewrites history, which breaks everyone who already has the old one. revert only adds, so it is always safe.
reset يعيد كتابة التاريخ، فيكسر كل من لديه القديم. أما revert فيضيف فقط، فهو آمن دائمًا.
2.
What does git reset --soft HEAD~1 do to your files?
git reset --soft HEAD~1 بملفاتك؟It is the useful one: undo the commit, keep the work exactly where it was, and commit it again properly.
3.
You ran git reset --hard and lost a commit. Is it recoverable?
git reset --hard وفقدت commit. أهي قابلة للاستعادة؟Anything committed leaves a trace in the reflog. What is genuinely unrecoverable is work that was never committed at all.
Score / النتيجة: 0 / 3
Your task / مهمتك
Practise every recovery in a throwaway repository, not a real one. Make some commits, then demonstrate four things with real output: unstaging a file, undoing a commit with --soft and recommitting it properly, reverting a commit, and recovering something with git reflog after a --hard. Finish with your own one-line rule for choosing between reset and revert.
تدرّب على كل استعادة في مستودع للرمي لا في مستودع حقيقي. اصنع بعض الـcommits، ثم اعرض أربعة أشياء بمخرَج حقيقي: إلغاء تجهيز ملف، والتراجع عن commit بـ--soft وإعادة إيداعها كما ينبغي، وعمل revert لـcommit، واستعادة شيء بـgit reflog بعد --hard. واختم بقاعدتك من سطر واحد للاختيار بين reset وrevert.
- A throwaway repository, said explicitly مستودع للرمي، مقولًا صراحةً
- All four recoveries, with real output الاستعادات الأربع كلها بمخرَج حقيقي
- reflog shown actually finding a lost commit reflog معروض وهو يجد commit ضائعة فعلًا
- Your own rule for reset against revert قاعدتك أنت لـreset في مقابل revert