How To Switch Between Git Branches From The Linux CLI

If you are using Git for version control then it is likely at some point you will have a project with multiple branches of code. The benefit of this is allowing various developers to work on different projects while providing an easier way to merge the code when the developers tasks are completed. One developer may be working strictly on big fixes that could need to be checked in daily while another developer may be working on a project that may take months to complete and git provides a system to merge these code branches together. Below is a quick reference for switching between Git code branches.

List All Git Project Branches:

bash

  1. [web@dev project]$ git branch -a
  2. * master
  3.   master-2111-sc-backoff
  4.   prod
  5.   origin/HEAD
  6.   origin/master
  7.   origin/master-1980-captcha
  8.   origin/master-2111-sc-backoff
  9.   origin/master-2333-cg-popup-validation
  10.   origin/master-2220-system-timeout-group
  11.   origin/master-2223-user-intake
  12.   origin/master-c-health-demo
  13.   origin/merged-2244-user-intake
  14.   origin/merged-2248-dialup-status-bundle
  15.   origin/prod
  16.   origin/rails-2.1.2
  17.   origin/rails-2.3.2.1
  18. [web@dev project]$

As you can see above there are numerous branches of code available in this projects repository which is actually stored on GitHub. Notice in the above list of Git branches the current active branch is “master” which is marked with a “*”. Typically when you have a project where there are numerous branches such as the above example there will be various developers that have some form of development environment set up so they can easily switch between the Git branches. If you need to switch between Git branches you simply checkout the branch you want to work on using the command below.

Switch Between Git Branches:

bash

  1. [web@dev project]$ git checkout prod
  2. Switched to branch "prod"
  3. Your branch is behind 'origin/prod' by 445 commits, and can be fast-forwarded.

After switching branches you should update the project by issuing a “git pull” or similar to make sure you are working on the latest code. After checking out the “prod” branch you can see in the below example that it now is the active code branch when listing all of the available Git branches.

Git Branch Listing That Displays New Active prod Branch:

bash

  1. [web@dev project]$ git branch -a
  2.   master
  3.   master-2111-sc-backoff
  4. * prod
  5.   origin/HEAD
  6.   origin/master
  7.   origin/master-1980-captcha
  8.   origin/master-2111-sc-backoff
  9.   origin/master-2333-cg-popup-validation
  10.   origin/master-2220-system-timeout-group
  11.   origin/master-2223-user-intake
  12.   origin/master-c-health-demo
  13.   origin/merged-2244-user-intake
  14.   origin/merged-2248-dialup-status-bundle
  15.   origin/prod
  16.   origin/rails-2.1.2
  17.   origin/rails-2.3.2.1

That is all there is to switching between Git branches. If local files have been modified there may be some merging required when updating the local file base.

Pro Git (Paperback)


List Price: $34.99 USD
New From: $13.95 USD In Stock
Used from: $5.36 USD In Stock

Version Control with Git: Powerful tools and techniques for collaborative software development (Paperback)


List Price: $34.99
New From: $12.04 USD In Stock
Used from: $3.35 USD In Stock

Share