Git restore और checkout
In this page:
git restore file_name
git restore --staged file_name
git checkout branch_name
Unstaged Local Changes Discard करना
git restore <file> किसी file में uncommitted local edits फेंक देता है और इसे last commit (या --source के साथ एक specified commit) से match करने पर reset करता है। यह पुराने git checkout -- <file> syntax का modern, safer-named replacement है जो पहले file-restore और branch-switch command दोनों की double duty करता था।
उदाहरण: Discarding Unstaged Local Changes
git restore file.txt
Staged Files Unstage करना
अगर आपने git add से एक file stage की लेकिन decide किया कि आप इसे अभी commit नहीं करना चाहते, git restore --staged <file> इसे unstage कर देता है — इसे वापस "modified but not staged" पर move करते हुए — working directory में आपके actual edits छुए बिना। यह common "oops, गलत file stage हो गई" moment के लिए fix है।
उदाहरण: Unstaging Staged Files
git restore --staged file.txt
Branches के बीच Switch करना
git checkout <branch> (या नया git switch <branch>) आपकी working directory को उस branch की latest commit से match करने के लिए update करता है। अगर यह चुपचाप uncommitted changes overwrite करेगा तो Git switch से मना कर देगा, इसलिए पहले commit या stash करना switch के बीच एक confusing error से बचाता है।
उदाहरण: Switching Between Branches
git checkout main
git switch main
नई Branches बनाना और Switch करना
git checkout -b <name> (या git switch -c <name>) आपकी current position से एक नई branch बनाता है और एक step में इस पर switch करता है, git branch <name> के बाद एक अलग switch command चलाने के बजाय — daily use में ज़्यादातर लोग जो shortcut इस्तेमाल करते हैं।
उदाहरण: Creating and Switching to New Branches
git checkout -b feature-x
git switch -c feature-x
Specific Commits से Files Restore करना
git restore --source=<commit> <file> (या पुराना git checkout <commit> -- <file>) आपकी working directory में किसी arbitrary past commit से सिर्फ एक file के contents खींचता है, बिना किसी दूसरी file को छुए या अपना branch pointer move किए — किसी single deleted या overwritten file recover करने के लिए उपयोगी।
उदाहरण: Restoring Files from Specific Commits
git restore --source=a1b2c3d file.txt
git checkout a1b2c3d -- file.txt
- पुराने tutorials में
git checkout file.txtइस्तेमाल करना, जबकिgit restore file.txtclearer है और एक काम करता है। - staging के बाद
git restore file.txtइस्तेमाल करना, unstage होने की उम्मीद करना, जबकिgit restore --staged file.txtचाहिए। - यह उम्मीद करना कि
git restorediscarded uncommitted edits recover कर लेगा, जबकि उनके लिए कोई undo नहीं है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: