DEV Community

Andrey for JetRockets

Posted on • Edited on • Originally published at jetrockets.pro

6 1

Git switch command

A switch command has been added in the new version of git
Let's look at examples:

# switched to <branch>
git switch <branch>

# creates a new <branch>
git switch -c <branch>

# switched to commit 
git switch -d <commit> 

# creates and switches to branch from remote. 
# need to use if branch exists in multiple remotes 
git switch -c <branch> --track <remote>/<branch> 

# switch to a branch even if the index or working tree is different from HEAD
# this is used to throw away local changes
git switch --discard-changes <branch>

# alias for  --discard-changes
git switch -f <branch> 

# switch back to the previous branch before we switched
git switch - 

Command available on version Git 2.23.0 or higher

Top comments (4)

Collapse
 
jessekphillips profile image
Jesse Phillips

OK, then if I do

git switch -d <commit> -c <branch>

It should work as expected, right?

Collapse
 
m0rozov profile image
Andrey

No, options -d cannot be used with a -c

Collapse
 
jessekphillips profile image
Jesse Phillips

Sad, hard to beat

git checkout -b <branch> <commit>

Thread Thread
 
m0rozov profile image
Andrey

U can use

git switch -c <branch> <commit>

Image of Quadratic

AI, code, and data connections in a familiar spreadsheet UI

Simplify data analysis by connecting directly to your database or API, writing code, and using the latest LLMs.

Try Quadratic free

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay