Switching Git Branches

Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $100 credit.
This credit will be applied to any valid services used during your first 60 days.

Git is a distributed version control system. It supports non-linear, distributed development, allowing multiple contributors to work on a project simultaneously within what are called branches. This guide walks you through the basics of what a branch is and how to switch between branches.

Before You Begin

  1. Familiarize yourself with our Getting Started guide and complete the steps for setting your Linode.

  2. This guide assumes you have Git installed, are familiar with the command-line interface (CLI) and Git, and have a project in Git.

  3. Update your Linode’s system:

    sudo apt-get update && sudo apt-get upgrade
    
  4. Update your local workstation’s system using the tools and package managers for your operating system.

What is a Branch in Git?

If you’re doing work on a website, application, or other Git project, you probably don’t want to push the code to production immediately. This is where creating a branch is necessary, as it marks what version of the code you branched from and allows you to work without breaking anything currently in production, or having you disturb other developers (or vice versa).

Why Would I Need to Switch Branches in Git?

Different branches have different purposes, so sometimes you need to switch from one to another. For example, you may be working in two branches yourself, or you may be pair programming. Git was designed to anticipate the need to switch between branches with git checkout (git switch was introduced to be more intuitive and decrease the number of functions in git checkout, but it is currently considered experimental and not covered in this article).

Switching to an Existing Git Branch

You may have multiple tasks you’re working on in separate branches. Let’s say one is a hotfix and one is a feature request. You realize you have something to add on the hotfix, so you need to switch to that branch in Git:

  1. Enter git checkout quick-hotfix at the command prompt.

  2. You should then get a response stating:

    Switched to branch 'quick-hotfix'

At that point, you can complete the hotfix.

Switching to a New Git Branch

Suppose you are updating your product’s API and creating the branch you need to switch to. To do so:

  1. Enter a command similar to the following at the command prompt. The -b flag is followed by the name of the new branch.

    git checkout -b update-api

  2. You should then get a response stating:

    Switched to a new branch 'update-api'

You can then commit work to that branch.

Switching to a Remote Git Branch

Using remote branches is one of the best ways to work with your colleagues. Let’s say you’re pair programming for the day and your coworker has a branch called “edit-button-feature-request” ready for the two of you. To switch to a remote branch in Git:

  1. Enter git checkout --track origin/edit-button-feature-request at the command prompt.

  2. You should then get a response stating:

    Branch edit-button-feature-request set up to track remote branch newsletter from origin.
    Switched to a new branch 'edit-button-feature-request'

At that point, a new local branch is created, allowing you to start on that feature request, and push to that branch as necessary.

Further Reading on Git Branches

Git branching is an extensive subject. For more on branching in Git, see “Git Branching - Branches in a Nutshell” on git-scm.com.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on


Your Feedback Is Important

Let us know if this guide made it easy to get the answer you needed.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.