← Back to Git Course | Chapter 5: Undoing Changes | Lesson 6 of 6

Git Recovery

एक branch delete करना एक page को mark करने वाला sticky note फाड़ने जैसा है, page को खुद फाड़ने जैसा नहीं। अगर आपको पता हो कैसे देखना है तो saved work अभी भी मिल सकता है।
Syntax
bash
git reflog
git branch recovered_branch commit_hash
git reset --hard commit_hash

एक Deleted Branch Recover करना

git branch -D से एक branch delete करना सिर्फ branch pointer हटाता है, इसकी referenced commits नहीं, इसलिए वे commits repository में तब तक रहती हैं जब तक garbage collection आख़िरकार unreachable वालों को न हटा दे। git reflog चलाना delete के बाद भी branch का last known commit hash दिखाता है, और उस hash की ओर point करने वाली एक नई branch बनाना branch को उसकी history सहित पूरी तरह वापस ले आता है।

उदाहरण: Recovering a Deleted Branch

bash
git reflog
git branch recovered-branch a1b2c3d

एक Hard Reset के बाद Recover करना

एक git reset --hard branch pointer move करता है और उस पर मौजूद commits discard करता है, जो permanent लगता है लेकिन commits खुद आमतौर पर garbage collected होने तक reflog में survive करती हैं। git reflog चलाना reset से ठीक पहले की state के लिए एक entry दिखाता है, और उस hash पर दोबारा reset करना hard reset को ऐसे undo कर देता है जैसे यह कभी हुआ ही न हो।

उदाहरण: Recovering After a Hard Reset

bash
git reflog
git reset --hard a1b2c3d

एक Uncommitted File Recover करना

अगर आप एक ऐसी file delete करें जो पहले किसी point पर staged या committed थी, git checkout (या newer Git में git restore) index या सबसे हाल की commit से last known version वापस खींच सकता है। यह सिर्फ उस content के लिए काम करता है जिसे Git पहले से जानता था — एक ऐसी file जो कभी staged या committed नहीं हुई उसका कोई recorded version नहीं है जिसे Git recover कर सके।

उदाहरण: Recovering an Uncommitted File

bash
git checkout -- deleted-file.txt
git restore deleted-file.txt

git rm के बाद Recover करना

git rm एक step में working directory और staging area दोनों से एक file हटाता है, जो एक plain filesystem delete से ज़्यादा destructive है क्योंकि यह removal भी stage करता है। अगर removal अभी commit नहीं हुई है, git reset HEAD -- <file> के बाद एक checkout file को सीधे वापस ले आता है; अगर यह commit हो चुकी है, आपको removal से ठीक पहले वाली commit से file checkout करनी होगी।

उदाहरण: Recovering After git rm

bash
git reset HEAD removed-file.txt
git checkout -- removed-file.txt

एक Stash से Recover करना

एक botched apply के बाद एक stash automatically delete नहीं होता, और यहाँ तक कि एक dropped stash का commit-like object अक्सर कुछ समय के लिए reflog में survive करता है, git fsck --unreachable से findable। अगर आपको roughly पता है कि इसमें क्या था, git stash list और git stash show आपको apply, pop, या drop करने का decide करने से पहले exact stash entry locate करने में मदद कर सकते हैं।

उदाहरण: Recovering from a Stash

bash
git fsck --unreachable
git stash apply a1b2c3d
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि git branch -D या git reset --hard commits को तुरंत destroy कर देता है, जबकि वे git reflog में कुछ समय के लिए findable रहती हैं।
  2. एक deleted branch की commits के लिए git log में देखना, जो उन्हें नहीं दिखाएगा; इसके बजाय git reflog इस्तेमाल करें।
  3. एक ऐसी file recover करने की कोशिश करना जो कभी staged या committed नहीं हुई, जिसे Git restore नहीं कर सकता क्योंकि यह कभी saved ही नहीं हुई।
चैप्टर सारांश
  • git reset और git revert commits को अलग-अलग तरीकों से undo करते हैं।
  • git reflog और recovery techniques खोया हुआ काम वापस लाने में मदद करती हैं।
  • एक commit amend करना और restore तथा checkout इस्तेमाल करना हाल की गलतियाँ fix करते हैं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.