Git Branch Switching
Introduction
When working with Git, one of its most powerful features is the ability to create and manage multiple branches. Branches allow you to work on different features, bug fixes, or experiments simultaneously without affecting your main codebase. However, creating branches is only half the story - you also need to know how to switch between them efficiently.
In this guide, we'll learn how to navigate between branches in Git, understand the implications of switching, and explore some advanced techniques for managing your workflow.
Understanding Git Branches
Before diving into branch switching, let's quickly review what branches are:
A branch in Git is simply a lightweight movable pointer to a commit. The default branch is called main
(or master
in older repositories). When you create a new branch, you're creating a new pointer to the current commit.
In the diagram above, we have:
- The
main
branch with 3 commits - A
feature
branch that diverges after the second commit - Two commits on the
feature
branch
Basic Branch Switching
Viewing Your Branches
Before switching branches, you might want to see what branches exist in your repository:
git branch
Output:
feature
* main
bugfix
The branch with an asterisk (*
) is your current branch.
Switching Branches with git checkout
The traditional command for switching branches is git checkout
:
git checkout feature
Output:
Switched to branch 'feature'
This command changes your working directory to reflect the state of the feature
branch.
Switching Branches with git switch
Git version 2.23 introduced a new command specifically for branch switching called git switch
. This offers a clearer syntax:
git switch feature
Output:
Switched to branch 'feature'
The switch
command is now the preferred way to change branches as it's more intuitive and separates the concerns of branch switching from other checkout
functionality.
Creating and Switching in One Step
With checkout
You can create a new branch and switch to it in a single command:
git checkout -b new-feature
Output:
Switched to a new branch 'new-feature'
With switch
Similarly, with git switch
:
git switch -c new-feature
Output:
Switched to a new branch 'new-feature'
Handling Uncommitted Changes When Switching
When you try to switch branches with uncommitted changes, Git will prevent you from switching if those changes would be overwritten by the switch. Let's explore different scenarios:
Scenario 1: Non-conflicting Changes
If your uncommitted changes don't conflict with the branch you're switching to, Git will allow the switch and carry your changes over:
# Make some changes to file.txt
git switch feature
Output:
Switched to branch 'feature'
Your changes to file.txt
are preserved and carried to the feature
branch.
Scenario 2: Conflicting Changes
If your changes would be overwritten by switching branches:
# Make changes to a file that differs between branches
git switch feature
Output:
error: Your local changes to the following files would be overwritten by checkout:
file.txt
Please commit your changes or stash them before you switch branches.
Aborting
In this case, you have several options:
-
Commit your changes to the current branch:
bashgit add file.txt
git commit -m "Complete work on feature X"
git switch feature -
Stash your changes to apply them later:
bashgit stash
git switch feature
# Later, if you want to apply the changes
git stash apply -
Discard your changes if they're not needed:
bashgit restore file.txt # or git checkout -- file.txt in older Git versions
git switch feature
Advanced Branch Switching Techniques
Temporary Branch Switching with git worktree
If you need to quickly check something in another branch without committing or stashing your current work, you can use Git worktrees:
git worktree add ../temp-main main
This creates a new working directory at ../temp-main
with the main
branch checked out, allowing you to browse that branch without disturbing your current work.
Branch Switching with Specific File Restoration
Sometimes you want to switch branches but keep specific files from your current branch:
git switch feature -- file.txt
This will switch to the feature
branch but restore file.txt
from your current branch.
Detached HEAD State
You can also checkout a specific commit instead of a branch, which puts you in a "detached HEAD" state:
git checkout a1b2c3d
Output:
Note: checking out 'a1b2c3d'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
This is useful for examining old versions of your codebase, but be careful as commits made in this state may be lost if you don't create a branch from them.
Real-World Application: Feature Branch Workflow
Let's walk through a common workflow that involves branch switching:
-
Start on the main branch and make sure it's up to date:
bashgit switch main
git pull -
Create and switch to a feature branch:
bashgit switch -c feature/user-authentication
-
Work on your feature, making commits as needed:
bash# Make changes
git add .
git commit -m "Add login form" -
While working on your feature, a critical bug is reported that needs immediate attention. Stash your current changes and switch to main:
bashgit stash
git switch main
git pull -
Create and switch to a bugfix branch:
bashgit switch -c hotfix/critical-bug
-
Fix the bug and commit:
bash# Fix bug
git add .
git commit -m "Fix critical bug in payment processing" -
Push the bugfix and create a pull request (or merge directly if appropriate):
bashgit push -u origin hotfix/critical-bug
-
Switch back to your feature branch and restore your stashed changes:
bashgit switch feature/user-authentication
git stash pop -
Continue working on your feature.
This workflow demonstrates how branch switching enables you to manage priorities effectively without losing work.
Common Issues and Solutions
Issue: "Cannot switch branch due to local modifications"
Solution: Commit, stash, or discard your changes as described earlier.
Issue: "Branch switching resulted in merge conflicts"
Solution: Resolve conflicts by editing the conflicted files, then:
git add <resolved-files>
git commit
Issue: "Lost track of which branch I'm on"
Solution: Check your current branch with:
git branch
or for more detailed information:
git status
Summary
Branch switching is a fundamental Git skill that allows you to navigate between different versions of your codebase. Here's what we covered:
- Basic branch switching with
git checkout
andgit switch
- Creating and switching to new branches in one step
- Handling uncommitted changes when switching branches
- Advanced techniques like using worktrees and detached HEAD states
- A real-world workflow example showing how branch switching fits into daily development
By mastering branch switching, you'll be able to work on multiple features simultaneously, handle urgent bugs without disrupting your main work, and explore your project's history effectively.
Additional Resources
- Practice branch switching with interactive tutorials like Learn Git Branching
- Read about Git's branching model in the Pro Git book
Exercises
- Create a new Git repository, add a few files, and commit them
- Create three different branches and make unique changes in each
- Practice switching between branches and observe how your working directory changes
- Try creating a situation with uncommitted changes, then practice stashing them to switch branches
- Create a branch from a specific commit in your history, make some changes, and then merge it back to your main branch
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)