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:

[web@dev project]$ git branch -a
* master
  master-2111-sc-backoff
  prod
  origin/HEAD
  origin/master
  origin/master-1980-captcha
  origin/master-2111-sc-backoff
  origin/master-2333-cg-popup-validation
  origin/master-2220-system-timeout-group
  origin/master-2223-user-intake
  origin/master-c-health-demo
  origin/merged-2244-user-intake
  origin/merged-2248-dialup-status-bundle
  origin/prod
  origin/rails-2.1.2
  origin/rails-2.3.2.1
[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:

[web@dev project]$ git checkout prod
Switched to branch "prod"
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:

[web@dev project]$ git branch -a
  master
  master-2111-sc-backoff
* prod
  origin/HEAD
  origin/master
  origin/master-1980-captcha
  origin/master-2111-sc-backoff
  origin/master-2333-cg-popup-validation
  origin/master-2220-system-timeout-group
  origin/master-2223-user-intake
  origin/master-c-health-demo
  origin/merged-2244-user-intake
  origin/merged-2248-dialup-status-bundle
  origin/prod
  origin/rails-2.1.2
  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)

By (author): Scott Chacon


List Price: $34.99 USD
New From: $20.79 USD In Stock
Used from: $14.85 USD In Stock


List Price: $34.99 USD
New From: $7.00 USD In Stock
Used from: $0.53 USD In Stock

Share