← Back to Git Course | Chapter 3: Branching | Lesson 3 of 9

Git checkout Command

git checkout किसी दूसरे room में चलने जैसा है जहाँ आपके project की एक अलग copy set up है। आपकी files बदल जाती हैं जिस भी branch में आप step करें उससे match करने के लिए।
Syntax
bash
git checkout branch_name
git checkout -b new_branch_name
git checkout -- file_name

एक मौजूदा Branch पर Switch करना

git checkout <branch> आपकी working directory को किसी दूसरी branch की files से match करने के लिए switch करता है -- यह Git के सबसे पुराने और सबसे overloaded commands में से एक है, जो कि आंशिक वजह है कि बाद में git switch सिर्फ इस behavior को अलग करने के लिए introduce किया गया।

उदाहरण: Switching to an Existing Branch

bash
git checkout main

Branches बनाना और Switch करना

git checkout -b <new-branch> आपकी current position से एक बिल्कुल नई branch बनाता है और तुरंत इस पर switch करता है, दो अलग commands (git branch फिर git checkout) जो अन्यथा होतीं उन्हें एक में combine करते हुए।

उदाहरण: Creating and Switching Branches

bash
git checkout -b feature-signup

Files Restore करना

git checkout -- <file> किसी specific file में uncommitted local changes discard कर देता है, इसे वापस जो भी last committed था उस पर revert करते हुए -- बिना कुछ और बदले एक file पर एक experiment फेंकने के लिए उपयोगी।

उदाहरण: Restoring Files

bash
git checkout -- file.txt

Detached HEAD State

एक branch name के बजाय एक specific commit hash या tag checkout करना आपको एक 'detached HEAD' state में डाल देता है -- आप अभी भी देख सकते हैं और commits भी कर सकते हैं, लेकिन वे commits किसी branch से belong नहीं करेंगी और switch away करते ही खो सकती हैं।

उदाहरण: Detached HEAD State

bash
git checkout a1b2c3d

Safety पर वापस जाना

एक detached HEAD से, किसी भी actual branch name (git checkout main) पर वापस switch करना आपको normal tracked development पर वापस ले जाता है -- अगर आपने detached रहते हुए ऐसी commits कीं जिन्हें रखना है, switch away करने से पहले उनसे एक branch बनाएँ।

उदाहरण: Returning to Safety

bash
git checkout main
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. changes discard करने के लिए git checkout file.txt इस्तेमाल करना और उन्हें permanently खो देना क्योंकि कोई undo नहीं है।
  2. conflicting uncommitted changes के साथ git checkout otherbranch चलाना, और switch होने के बजाय एक error पाना।
  3. एक commit hash checkout करना और वहाँ commit करना, एक detached HEAD state में पहुँच जाना जहाँ नई commits खोना आसान है।

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.