DEV Community

Cover image for All About Switching Branch In Git 🚀
Garvit Motwani for World In Dev

Posted on

All About Switching Branch In Git 🚀

Whether you're an open-source contributor, project manager, or even a normal github user for personal projects maybe, you need to know how to Switch Branches In Git.

So let's get started!!

How to create a new branch

Normally you use git checkout with a -b flag and pass a name for the branch.

If you are on the branch called master, this will be the case for you:

$ (master) git checkout -b new-branch
Switched to a new branch 'new-branch'
$ (new-branch)
Enter fullscreen mode Exit fullscreen mode

How to switch to an existing branch

To switch to an existing branch, you can use git checkout but without the -b flag and pass the name of the branch you want to switch to, something like this:

(new-branch)$ git checkout master
Switched to branch 'master'
(master)$
Enter fullscreen mode Exit fullscreen mode

Bonus:

There is a shortcut to return the previous branch you were in, you can just use - after git checkout

(master)$ git checkout -
Switched to branch 'new-branch'
$ (new-branch)
Enter fullscreen mode Exit fullscreen mode

PLEASE FOLLOW, LIKE, SHARE, AND COMMENT

Follow me on Dev and Twitter

You should also check:

Subscribe to our newsletter

Top comments (0)