How to switch back to the previous branch quickly in Git?

Milan Panchal
2 min readMay 13, 2024

Overview:

  • Where we are working on any project having multiple features then probably maintaining many branches in our repository.
  • Some times we do work on multiple branches simultaneously to perform different tasks. So that we can keep the code in respective branch.
  • The action of moving from one branch to other branch is called branch switching a.k.a checkout in git.

How to switch branches in git?

There are two ways to switch the branches in git:

  1. git checkoutUsed to switch between different branches in git.
  2. git switchAnother way of switching between the branches in git. The new command has been introduced in the version 2.23.0
# Switch to existing branch
$ git checkout <existing_branch>

# Create new branch and switch to the branch
$ git checkout -b <non_existing_branch>
# Switch to existing branch
$ git switch <existing_branch>

# Create new branch and switch to the branch
$ git switch -c <non_existing_branch>

How to switch back to the previous branch?

Sometimes we find ourself needing to switch between the same two branches in git again and again. Instead of typing the branch name manually each time, there’s a shortcut for that in the terminal.

$ git switch -

--

--

Milan Panchal

iOS Team Lead ǁ Swift ǁ Objective-C ǁ Lifelong Learner ǁ Blogger — Join Medium from the following link: https://medium.com/@milanpanchal24/membership