MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Switching Branches with git switch and git checkout -b0%
Study Materials
Git & GitHub BasicsProgress 0%
1.1What is Version Control? (Local, Centralized vs Distributed VCS)1.2Why Git? History, Architecture & Core Benefits1.3The Three Trees: Working Directory, Staging Area & Git Repository1.4Installing Git and Initial Configuration (git config user.name / email)
4.1Git Branching Mechanics: Pointers, HEAD, and git branch4.2Switching Branches with git switch and git checkout -b4.3Merging Branches: Fast-Forward vs Three-Way Merges (git merge)4.4Resolving Merge Conflicts Step-by-Step with Conflict Markers
HomeTutorialsGit & GitHub BasicsSwitching Branches with git switch and git checkout -b
Merging Branches: Fast-Forward vs Three-Way Merges (git merge)

Switching Branches with git switch and git checkout -b

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

Creating branches is only half the battle; navigating between them is an everyday developer reflex. Over Git's history, the command for switching branches has evolved to be safer and more intuitive.


1. The Modern Command: git switch

Historically, the git checkout command was used for both switching branches AND discarding file changes. Because this dual role caused confusion and accidental data loss, Git 2.23 introduced two dedicated commands:

  • git switch: Exclusively for switching and creating branches.
  • git restore: Exclusively for discarding or un-staging file changes.

Switching Between Existing Branches:

1# Switch to an existing local branch named 'dev'
2git switch dev

Terminal output:

1Switched to branch 'dev'
2Your branch is up to date with 'origin/dev'.

Creating and Switching in One Step (-c flag):

To create a brand new branch and immediately switch HEAD to it:

1git switch -c feature-user-profile

Terminal output:

1Switched to a new branch 'feature-user-profile'

Switch Back to the Previous Branch (-):

Just like cd - in terminal shells, you can instantly hop back to whatever branch you were on previously:

1git switch -

2. The Traditional Command: git checkout

Because millions of tutorials, StackOverflow answers, and CI/CD pipelines use git checkout, every developer must know its syntax:

1# Switch to an existing branch
2git checkout main
3 
4# Create and immediately switch to a new branch (-b flag)
5git checkout -b feature-dark-mode
Note
[!NOTE] git switch -c <name> is modern, clearer, and equivalent to git checkout -b <name>. Both commands accomplish the exact same outcome under the hood.

3. What Happens in Your Working Directory When You Switch?

When you run git switch <branch>:

  1. 1
    Git moves HEAD to point to the new branch pointer.
  2. 2
    Git rewrites the files in your Working Directory to match the snapshot of the commit pointed to by that branch!
  3. 3
    Files created in the other branch disappear from your disk, and files present in the target branch materialize instantly.
1Working on 'feature-auth' branch:
2Working Directory contains: [ auth.js, login.html, index.html ]
3 
4Execute: git switch main
5Git updates Working Directory:
6Working Directory now contains: [ index.html ] (auth.js and login.html disappear from disk!)

4. Preventing Switch Conflicts: Uncommitted Changes

What happens if you modified styles.css on main, forgot to commit, and try to git switch feature-payment?

  • If the modified file does not conflict with changes on the target branch, Git carries the modified file over to the new branch with you.
  • If the target branch has different changes to styles.css, Git will abort and protect your work:
1error: Your local changes to the following files would be overwritten by checkout:
2 styles.css
3Please commit your changes or stash them before you switch branches.
4Aborting

Solution:

Either commit your changes or use git stash to shelve them temporarily before switching!


Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
Which modern Git command was introduced specifically to navigate and create branches without overloading file-restoring behavior?
Question 2
What is the single-line command to create a new branch named bugfix-12 and switch to it immediately using git switch?
Question 3
What is the equivalent legacy command for git switch -c feature-api?
Question 4
How do you quickly return to your previously active branch without typing its full name?
Question 5
What happens to files on your disk in the working directory when you switch from branch A to branch B?
Next Lesson

Merging Branches: Fast-Forward vs Three-Way Merges (git merge)

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Git Branching Mechanics: Pointers, HEAD, and git branchMerging Branches: Fast-Forward vs Three-Way Merges (git merge)

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master Git & GitHub Basics with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext