← Back to Git Course | Chapter 2: Basic Commands | Lesson 16 of 18

Git restore Command

git restore एक undo button जैसा है जो किसी file को वापस वैसा बना देता है जैसा वह आपके last saved snapshot में दिखता था, आपके तब से किए scribbles मिटाते हुए।
Syntax
bash
git restore file_name
git restore --staged file_name
git restore --source=commit_hash file_name

Working Directory Changes Discard करना

git restore <file> एक unwanted edit को file के last committed state पर वापस revert कर देता है, आपके local, unstaged changes discard करते हुए -- यह edits फेंकने के लिए git checkout इस्तेमाल करने का modern, ज़्यादा explicit replacement है।

उदाहरण: Discarding Working Directory Changes

bash
git restore file.txt

एक File Unstage करना

अगर एक file पहले से git add से staged है लेकिन आपने अगली commit में इसे शामिल करने का mind बदल लिया है, git restore --staged <file> आपके actual edits working directory में untouched छोड़ते हुए इसे unstage करता है।

उदाहरण: Unstaging a File

bash
git restore --staged file.txt

एक Specific Commit से Restore करना

git restore --source=<commit> <file> history के किसी भी specific point से एक file के contents खींचता है, सिर्फ last commit से नहीं, आपको उस पूरी commit checkout किए बिना एक पुराना version recover करने देते हुए।

उदाहरण: Restoring from a Specific Commit

bash
git restore --source=a1b2c3d file.txt

साथ Unstaging और Restoring

एक command में --staged और --source combine करना एक file को unstage करता है और उसकी working copy को एक single step में एक past commit के version में reset भी करता है, इसे दो अलग commands की तरह करने के बजाय।

उदाहरण: Unstaging and Restoring Together

bash
git restore --staged --source=a1b2c3d file.txt

Patch Mode Restoration

git restore -p <file> एक interactive, hunk-by-hunk prompt खोलता है ताकि आप किसी file के changes में से सिर्फ कुछ discard कर सकें और बाकी रख सकें -- तब उपयोगी जब एक file एक unwanted experiment को उस fix के साथ mix करे जिसे आप actually रखना चाहते हैं।

उदाहरण: Patch Mode Restoration

bash
git restore -p 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. git restore file.txt चलाना और changes वापस पाने की उम्मीद करना, जबकि यह uncommitted edits permanently discard करता है।
  2. एक file unstage करने के लिए git restore file.txt इस्तेमाल करना, जबकि unstaging के लिए git restore --staged file.txt चाहिए।
  3. एक commit के साथ --source इस्तेमाल करना और यह भूल जाना कि यह working file overwrite कर देता है, इसमें uncommitted changes खोते हुए।

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.