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

Git reflog Command

git reflog आपके project में आप जहाँ भी गए उसकी एक secret diary जैसा है। भले ही आप अपनी जगह खो दें, आप देख सकते हैं कि आप पहले कहाँ खड़े थे।
Syntax
bash
git reflog
git reset --hard HEAD@{n}

Git Reflog क्या है?

git reflog उन हर जगहों का एक chronological log दिखाता है जहाँ आपकी local repository में HEAD (और हर branch) ने point किया है, commits, checkouts, resets, और rebases सहित। क्योंकि यह commit graph के बजाय *reference movements* track करता है, यह ऐसा काम ढूँढ सकता है जो अब किसी branch से reachable नहीं है — exactly उस तरह की "lost" commit जो एक hard reset या rebase पीछे छोड़ देता है।

उदाहरण: What is Git Reflog?

bash
git reflog

Reflog पढ़ना

हर reflog entry एक commit hash को उस action के साथ pair करता है जिसने इसे produce किया (जैसे commit, checkout: moving from..., reset: moving to...), जो आपको exactly reconstruct करने देता है कि आपने क्या किया और कब, entry by entry, सबसे हाल की पहले।

उदाहरण: Reading the Reflog

bash
git reflog
# a1b2c3d HEAD@{0}: commit: Fix login bug
# e4f5g6h HEAD@{1}: checkout: moving from main to feature-x

Lost Commits Restore करना

किसी ऐसी commit को recover करने के लिए जिसकी ओर अब कोई branch point नहीं करती, reflog में इसका hash ढूँढें और इसे inspect करने के लिए git checkout <hash> या इसे एक real branch पर वापस लाने के लिए git reset --hard <hash> / git branch recovery <hash> इस्तेमाल करें। commit खुद कभी delete नहीं हुई — सिर्फ इसका reference हुआ।

उदाहरण: Restoring Lost Commits

bash
git reflog
git reset --hard a1b2c3d

Deleted Branches Restore करना

एक branch delete करना भी उसकी commits तुरंत delete नहीं करता — reflog अभी भी याद रखता है कि उस branch की tip कहाँ थी। deleted branch जिस last commit hash की ओर point करती थी उसे लुकअप करें और इसे exactly वैसे recreate करने के लिए git branch <name> <hash> चलाएँ जैसे यह थी।

उदाहरण: Restoring Deleted Branches

bash
git reflog
git branch recovered-branch a1b2c3d

Reflog Lifetimes Manage करना

Reflog entries हमेशा के लिए नहीं रहतीं — unreachable वाली एक configurable window (default 90 days, अगर बनते समय पहले से unreachable हों तो 30) के बाद expire हो जाती हैं इससे पहले कि Git का garbage collector उन्हें prune कर सके। git reflog expire और git gc आपको ज़रूरत पड़ने पर जल्दी disk space reclaim करने के लिए manually इसे control करने देते हैं।

उदाहरण: Managing Reflog Lifetimes

bash
git reflog expire --expire=90.days.ago --all
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 reflog remote के साथ shared है, जबकि यह सिर्फ local है और कभी push नहीं होता।
  2. lost commits ढूँढने के लिए बहुत देर तक wait करना, जबकि पुरानी reflog entries expire होती हैं (default रूप से लगभग 90 दिनों के बाद) और garbage collected हो सकती हैं।
  3. git reflog चलाना और गलत entry चुनना, जबकि HEAD@{n} numbers हर नई action के साथ बदलते हैं।
🔒

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.