A Git branch allows you to create a separate line of development from an existing commit. Creating branches is useful when you want to develop a new feature, fix a bug, or experiment with changes without directly modifying the main development branch.
git branch.
The command git switch -c can create and switch to a
new branch in one step.
Creating a branch means creating a new branch reference that starts from the current commit.
A → B → C
↑
main
If you create a new branch at commit C, both branches initially point to C.
The basic command for creating a branch is:
git branch branch-name
For example:
git branch feature-login
Suppose you want to develop a login feature. You can create:
git branch feature-login
This creates the branch but keeps you on your current branch.
A separate branch can be created for fixing a bug:
git branch fix-login-error
The branch name describes the purpose of the work.
After creating a branch, use:
git branch
Example:
* main
feature-login
The asterisk shows that you are still on main.
Modern Git provides a convenient command to create and switch to a branch at the same time:
git switch -c feature-login
The -c option means create a new branch.
| Command | Action |
|---|---|
git branch feature-login |
Creates the branch only |
git switch -c feature-login |
Creates and switches to the branch |
When you create a branch, it starts from the commit currently checked out.
A → B → C
↑
main
git branch feature
A → B → C
↑
main
↑
feature
You can create a branch starting from a specific commit:
git branch feature-login 8f32abc
Here, 8f32abc represents the commit from which the
branch should start.
A branch can also be created from another branch:
git branch feature-login develop
The new branch starts from the commit currently referenced by
develop.
A common workflow is:
git switch -c feature-login
After this command, Git creates the branch and changes the current
branch to feature-login.
After switching to the new branch, check the current branch:
git branch --show-current
Example output:
feature-login
You can also use:
git status
Example:
On branch feature-login
nothing to commit, working tree clean
git switch main
git switch -c feature-dashboard
You can now work on the dashboard feature without making the feature commits directly on the main branch.
git switch main
git switch -c fix-navbar
You can now make and commit the navbar fix on the new branch.
After switching to the new branch, work normally:
git add .
git commit -m "Add dashboard feature"
The new commit advances the branch you are currently on.
D → E
/
A → B → C
\
F → G
After branches are created, they can contain different commits and develop independently.
You can create multiple branches in the same repository.
git branch feature-login
git branch feature-dashboard
git branch fix-navbar
List them using:
git branch
Good branch names should communicate the purpose of the work.
feature-login
feature-payment
fix-navbar
fix-login-error
update-homepage
Clear names make repositories easier to understand.
Suppose your history is:
A → B → C → D
You can create a branch from commit B:
git branch old-version B
The new branch will point to commit B.
You can explicitly create a branch from the current HEAD:
git branch feature-login HEAD
In normal usage, the commit argument can be omitted because a new branch is created from the current commit by default.
A local branch can be created from a remote-tracking branch:
git branch feature-login origin/main
This creates a local branch starting at the commit referenced by
origin/main.
A branch is a reference to a commit. Creating a branch does not create a separate physical copy of the entire project directory.
git branch feature-login
Git stores repository objects efficiently and branches simply point to commits.
git add .
git commit -m "Add homepage"
git switch -c feature-contact
The new branch starts from the commit containing the homepage changes.
A common development workflow is to create a feature branch before starting the feature:
git switch main
git pull
git switch -c feature-contact
This keeps the new work separate from the main branch.
git branch does not switch branches.git branch
git branch feature-login
git switch feature-login
git switch -c feature-login
git branch feature-login 8f32abc
git branch feature-login develop
git branch --show-current
git status
# Check current branch
git branch
# Switch to main
git switch main
# Create and switch to feature branch
git switch -c feature-login
# Check current branch
git branch --show-current
# Make changes
git add .
# Commit changes
git commit -m "Add login feature"
git init
git add .
git commit -m "Initial project"
git branch feature-about
git branch
git switch feature-about
git add .
git commit -m "Add about page"
git status
Here, feature-about is created from the initial project
commit and then used for developing the About page.
Creating branches is one of the most important Git workflows. A branch gives you a separate development path from a starting commit.
git branch feature-namegit switch -c feature-nameUse
git branchwhen you only want to create a branch. Usegit switch -cwhen you want to create and switch to the branch immediately.
git branch branch-name creates a new branch.git switch -c branch-name creates and switches to a branch.git branch --show-current displays the current branch.Question: Which command creates a new branch and switches to it?