BLOGS

git switch branch – Easily checkout git branches with this new command

Category
Programming
Author
IRSHAD S
Date
09/23/2019

git switch branch

Git 2.23 came up with the new ‘ git switch ’ command, which is not a new feature but an additional command to ‘switch/change branch feature which is already available in the overloaded git checkout command.

Currently, the all-encompassing command – git checkout does many things. It can be used to switch branches and also to restore the working tree files.

To separate out the functionalities, the GIT community introduced the new git switch branch command which is an attempt to start to scale back to the responsibilities without breaking backward compatibility.

SYNTAX: git switch <branch-name>
EXAMPLES

git list branches

Before going directly to the examples, let’s show all the branches that exist in the repo.

git branch

Output:

*master
 feature
 develop

git switch to existing branch

Let’s suppose there is an existing branch called develop and we want to switch from master to develop branch. To do that execute:

git switch develop

git switch to master

To switch branch to master 

git switch master

git switch to another branch

Let’s try once more. Suppose there is another branch called feature and we want to switch to it.

git switch feature

git switch branch to master

This time to switch back to master, we can use another shorthand that switches to the previous branch before we switched.

git switch -

git switch to new branch

Let’s suppose we want to create a new branch which doesn’t exist and also at the same time we want to switch to it. For that

git switch -c new-branch
which is the shorthand of the following two commands:
  1. creating a new branch using branch command.
  2. git branch new-branch
  3. then, switching to the newly created branch
  4. git switch new-branch

git switch to remote branch

To switch to a remote branch,

git switch -c <branch> --track <remote>/<branch>

Note:- Execute a git fetch command before, to fetch the latest remote updates (including the remote branches). 

git switch vs checkout

So what has changed? What’s the difference between  switch and checkout commands? Nothing special. git switch has almost copied and separated out the switching functionality from the git checkout command.

git switch branch vs git Checkout branch
git switch vs checkout
Summary
The new ‘experimental’ git switch branch command is meant to provide a better interface by having a clear separation, which helps to alleviate the developers’ confusion when using git checkout.
One such example is git checkout <filename> which reverses the modifications of an unstaged file and git checkout <branchname> which switches branch.The dilemma is more when the filename and branchname are the same.

The new commands, git restore (takes care of operations that change file), and the git switch (takes care of operations that change branches) shares out the responsibilities of git checkout in a more intuitive way.

Have you started to use the new git switch command? Share and let  your friends know.

You May Also Like To Read

Share on facebook
Share on twitter
Share on whatsapp
Share on linkedin
Share on facebook
Share on twitter
Share on whatsapp
Share on linkedin