Git reset Command
In this page:
git reset --soft commit_reference
git reset [--mixed] commit_reference
git reset --hard commit_reference
Git Reset क्या है?
git reset आपकी current branch को एक पहले वाली commit की ओर point करने के लिए rewind करता है, effectively उस point के बाद की commits को undo करते हुए जहाँ तक branch history का सवाल है। git revert के उलट, यह ऊपर एक नई commit add करने के बजाय history बदलता है, जो powerful है लेकिन shared branches पर ज़्यादा risky।
उदाहरण: What is Git Reset?
git reset a1b2c3d
Soft Reset
--soft सिर्फ branch pointer वापस move करता है — आपके staged changes और working directory files exactly वैसे रहते हैं जैसे थे, इसलिए "undone" commits से सब कुछ staged changes की तरह वापस दिखता है जो फिर से अलग तरीके से commit होने के लिए ready हैं। यह सबसे safe reset mode है और कई छोटी commits को एक में squash करने का एक common तरीका।
उदाहरण: Soft Reset
git reset --soft HEAD~1
Mixed Reset (Default)
Default mode (कोई flag नहीं, या --mixed) branch pointer वापस move करता है और staging area भी खाली कर देता है, लेकिन आपकी working directory files untouched छोड़ता है — इसलिए आपके edits survive करते हैं, बस अब staged नहीं। इसे तब इस्तेमाल करें जब आप दोबारा commit करने से पहले changes को review और re-stage करना चाहें।
उदाहरण: Mixed Reset (Default)
git reset HEAD~1
git reset --mixed HEAD~1
Hard Reset (Dangerous)
--hard branch pointer वापस move करता है AND staging area और आपकी working directory दोनों को उस commit से exactly match करने के लिए overwrite करता है, इस process में किसी भी uncommitted work को permanently discard करते हुए। कोई confirmation prompt नहीं है, इसलिए इसे चलाने से पहले double-check करें कि आप actually उन changes को खोना चाहते हैं।
उदाहरण: Hard Reset (Dangerous)
git reset --hard HEAD~1
Reset से Recover करना
एक hard reset के बाद भी, वे commits जो आपने "खोई" आमतौर पर गई नहीं होतीं — Git का reflog एक record रखता है कि आपकी branch tip हाल ही में कहाँ point कर चुकी है, इसलिए आप पुराना commit hash लुकअप करके उस पर वापस reset कर सकते हैं। यही safety net है जिस वजह से hard resets, भले ही scary हों, reflog की retention window के अंदर शायद ही कभी truly catastrophic होते हैं।
उदाहरण: Recovering from Reset
git reflog
git reset --hard a1b2c3d
git reset --hard HEAD~1चलाना और uncommitted changes खो देना बिना working tree के through उन्हें वापस पाने के किसी तरीके के।- पहले से pushed commits पर
git resetइस्तेमाल करना, जो shared history rewrite करता है। - modes को confuse करना,
--mixedके files staged रखने की उम्मीद करना, जबकि--softउन्हें staged रखता है और--mixed(default) उन्हें unstage करता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: