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

Git restore और checkout

restore और checkout किसी file पर undo दबाने के दो तरीके हैं, इसे उस state में वापस लाते हुए जैसा यह आपके last save पर दिखता था।
Syntax
bash
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

bash
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

bash
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

bash
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

bash
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

bash
git restore --source=a1b2c3d file.txt
git checkout a1b2c3d -- file.txt
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. पुराने tutorials में git checkout file.txt इस्तेमाल करना, जबकि git restore file.txt clearer है और एक काम करता है।
  2. staging के बाद git restore file.txt इस्तेमाल करना, unstage होने की उम्मीद करना, जबकि git restore --staged file.txt चाहिए।
  3. यह उम्मीद करना कि git restore discarded uncommitted edits recover कर लेगा, जबकि उनके लिए कोई undo नहीं है।
🔒

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.