Now that you understand how to create a new branch using git branch <branch-name>
, the next logical step is learning how to start working on that branch. Creating a branch simply establishes a new pointer; it doesn't automatically move your development context to that new line of work. Your working directory still reflects the state of the branch you were previously on (often the main
branch by default).
To change your active working context to a different branch, you need to "check out" that branch. This action tells Git, "I want to work on this branch now." When you switch branches, Git performs two main tasks:
HEAD
to point to the tip of the branch you're switching to. HEAD
usually points to the name of the branch you are currently on.Think of it like having multiple project drafts laid out on different desks. Creating a branch is like setting up a new desk (git branch new-feature
). Switching branches is like physically moving over to work at that new desk (git checkout new-feature
or git switch new-feature
), and the papers (files) on the desk change to reflect the state of that draft.
git checkout
Historically, the primary command for switching branches has been git checkout
. Its usage is straightforward:
git checkout <branch-name>
For example, if you previously created a branch named fix-login-bug
using git branch fix-login-bug
, you would switch to it like this:
# Assuming you are currently on the 'main' branch
git checkout fix-login-bug
After running this command, Git will report:
Switched to branch 'fix-login-bug'
Now, any new commits you make will be added to the fix-login-bug
branch, advancing its history independently from the main
branch or any other branches. Your working directory files will reflect the state they were in at the last commit on fix-login-bug
(which, if you just created it, will be the same as the commit you branched off from).
git switch
(Modern Approach)The git checkout
command is quite versatile. Besides switching branches, it can also be used to restore files in the working directory from different commits or the index (staging area). This overlap in functionality sometimes caused confusion for newcomers.
To address this, newer versions of Git (starting from 2.23) introduced two more specific commands: git switch
for changing branches and git restore
for changing files. The git switch
command is dedicated solely to branch operations, making its purpose clearer.
To switch to our fix-login-bug
branch using git switch
, you would run:
# Assuming you are currently on the 'main' branch
git switch fix-login-bug
The result is identical to using git checkout fix-login-bug
:
Switched to branch 'fix-login-bug'
Your HEAD
now points to fix-login-bug
, and your working directory is updated accordingly.
Imagine your repository history looks like this initially, with HEAD
pointing to the main
branch:
HEAD
pointing tomain
, which points to commit C3.
Now, you create a new branch feature-x
from main
using git branch feature-x
. The HEAD
pointer doesn't move yet.
feature-x
created, both branches point to C3,HEAD
still onmain
.
When you run git switch feature-x
(or git checkout feature-x
), Git updates HEAD
:
HEAD
now points tofeature-x
. Your working directory reflects commit C3 (initially). Subsequent commits will advancefeature-x
.
git checkout
and git switch
have options to create a new branch and immediately switch to it in one step.
git checkout -b <new-branch-name>
git switch -c <new-branch-name>
This is a common shortcut, combining git branch <name>
and git switch <name>
.While git checkout
works perfectly fine for switching branches and is widely used in older tutorials and projects, using git switch
is generally recommended for clarity if your Git version supports it. It makes the intent of your command unambiguous: you are switching your working context to a different branch.
Being able to swiftly move between different lines of development is fundamental to leveraging Git's branching power. You can now start a new feature on one branch, switch to another to fix an urgent bug, and then switch back to your feature, keeping the work isolated until you are ready to combine it.
© 2025 ApX Machine Learning