As we discussed in the chapter introduction, managing different tasks simultaneously, like developing a new feature while fixing a separate bug, requires a way to isolate these lines of work. Git provides this isolation through branches. Think of branches as movable bookmarks or pointers in your project's history. When you want to start a new line of development, you create a new branch.
The command to create a new branch is git branch
followed by the name you want to give the branch.
git branch <branch-name>
For example, if you are about to start working on a new website header, you might create a branch named update-header
:
git branch update-header
What happens when you run this command? Git creates a new pointer (named update-header
in this case) that points to the exact same commit you are currently on (where the HEAD
pointer is). It's important to understand that this command only creates the branch; it does not switch you to it. Your working directory files remain unchanged, and you are still on the same branch you were on before running the command.
Imagine your project history looks like this, where main
is your primary branch and HEAD
indicates your current position:
Your repository state before creating a new branch.
HEAD
points tomain
, which points to the latest commitC3
.
Now, if you run git branch update-header
, the state becomes:
After running
git branch update-header
. A new pointerupdate-header
is created, pointing to the same commitC3
asmain
.HEAD
still points tomain
.
Notice that a new pointer, update-header
, now exists and points to commit C3
. However, HEAD
still points to main
, indicating that main
is still your active branch.
While Git allows almost any name for a branch (except those containing spaces or certain special characters), it's good practice to use descriptive names. Common conventions include:
fix-login-bug
).feature/
, bugfix/
, or hotfix/
to categorize branches (e.g., feature/user-profiles
, bugfix/issue-451
).These conventions make it easier for you and your collaborators to understand the purpose of different branches just by looking at their names.
Creating a branch sets the stage for isolated development. The next step, which we'll cover in the following section, is actually switching to your new branch to start working on it.
© 2025 ApX Machine Learning