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

Git switch Command

git switch आपके project के rooms के बीच चलने का simpler तरीका है। यह इसलिए बनाया गया क्योंकि पुराना checkout command बहुत सारे अलग-अलग जॉब्स करता था।
Syntax
bash
git switch branch_name
git switch -c new_branch_name
git switch -

git switch से Basic Switching

git switch Git 2.23 में specifically पुराने, overloaded git checkout command के branch-switching आधे हिस्से को replace करने के लिए add किया गया था, branch changes को एक dedicated, कम error-prone command देते हुए एक narrower, clearer purpose के साथ।

उदाहरण: Basic Switching with git switch

bash
git switch main

-c के साथ बनाना और Switch करना

git switch -c <new-branch> एक नई branch बनाता है और एक step में इस पर move करता है -- functionally git checkout -b जैसा ही outcome, बस नए, ज़्यादा explicit command name के तहत।

उदाहरण: Creating and Switching with -c

bash
git switch -c feature-cart

वापस Switch करना और Tracking

git switch - आपको उस branch पर वापस jump करता है जिस पर आप अपने last switch से पहले थे, दो branches के बीच bounce करने के लिए एक तेज़ shortcut; git switch <remote-branch> सीधे एक remote branch की local tracking set up करता है।

उदाहरण: Switching Back and Tracking

bash
git switch -
git switch origin-feature

--detach के साथ Detached HEAD

git checkout के उलट, जो एक commit hash दिए जाने पर चुपचाप detached HEAD में enter हो जाता है, git switch को वही करने के लिए explicit --detach flag चाहिए -- गलती से आपकी branch का track खोने से बचने के लिए एक deliberate design choice।

उदाहरण: Detached HEAD with --detach

bash
git switch --detach a1b2c3d

Conflicting Local Changes Handle करना

अगर आपके uncommitted changes उस branch से overwrite हो जाएँगे जिस पर आप switch करने की कोशिश कर रहे हैं, git switch मना कर देता है और चुपचाप उन्हें discard या merge करने के बजाय एक error दिखाता है -- आगे बढ़ने के लिए आपको पहले commit, stash, या switch force करना होगा।

उदाहरण: Handling Conflicting Local Changes

bash
git switch main
# error: Your local changes would be overwritten
git stash
git switch 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. एक file restore करने के लिए git switch file.txt इस्तेमाल करना, जबकि switch सिर्फ branches बदलता है; git restore इस्तेमाल करें।
  2. एक exist न करने वाली branch के लिए git switch new-branch चलाना, जबकि एक बनाने के लिए -c चाहिए।
  3. 2.23 से पहले Git version पर पुराने tutorials follow करना, जहाँ git switch available नहीं है।

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.