Switching Git Branches
Traducciones al EspañolEstamos 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.
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
If you have not already done so, create a Linode account and Compute Instance. See our Getting Started with Linode and Creating a Compute Instance guides.
Follow our Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access.
This guide assumes you have Git installed, are familiar with the command-line interface (CLI) and Git, and have a project in Git.
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:
Enter
git checkout quick-hotfix
at the command prompt.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:
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
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:
Enter
git checkout --track origin/edit-button-feature-request
at the command prompt.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