Creating branches is a normal part of everyday Git work. For example, if you are about to build a login feature, you might create a branch called:
feature/loginand do all login-related work there.
Viewing Existing Branches
Use:
git branchto list local branches in the repository.
You may see:
* main
feature/loginThe * shows which branch you are currently on. In this example, the current branch is main.
You can also run:
git statusand Git will usually show:
On branch mainCreating a New Branch
To create a branch, use:
git branch <branch-name>For example:
git branch feature/loginThis creates the branch, but it does not switch to it.
If you run:
git branchagain, you may see:
feature/login
* mainThe new branch exists, but you are still on main.
Switching Branches with git switch
To move to an existing branch, use:
git switch <branch-name>For example:
git switch feature/loginNow:
git branchmay show:
* feature/login
mainAny new commits you create will now belong to feature/login.
Branch route console
Create feature/login, switch to it, and confirm the HEAD position.
—
—
No commits yet
Create feature/login, switch to it, and confirm the HEAD position.Creating and Switching in One Step
In real work, you often want to create a branch and immediately start using it. You can do that with:
git switch -c <branch-name>For example:
git switch -c feature/paymentThe -c means create.
This is essentially the same as running:
git branch feature/paymentfollowed by:
git switch feature/paymentBecause it combines both operations, git switch -c is very convenient.
Naming Branches
Git does not force you to follow one branch naming style, but clear names make a repository much easier to understand.
For feature work, you might use:
feature/login
feature/payment
feature/user-profileFor bug fixes:
fix/login-error
fix/payment-timeoutSimpler names are also fine:
login-feature
payment-fixThe exact style is less important than making the purpose of the branch obvious.
KNOWLEDGE QUIZ
feature/login?
Be Careful with Uncommitted Changes
Suppose you are working on feature/login and have modified several files but have not committed them yet. If you try to switch branches, Git may carry those changes with you, or it may stop you if the switch would overwrite something.
A good beginner habit is to check:
git statusbefore switching branches.
If your current work is ready, you can commit it first:
git add .
git commit -m "Add login form"Then switch branches.
This keeps your work easier to understand and avoids unnecessary confusion.
A Simple Example
Suppose you are currently on main:
git branchOutput:
* mainCreate and switch to a branch for the login feature:
git switch -c feature/loginMake your changes, then commit them:
git add .
git commit -m "Add login page"That commit now belongs to feature/login.
If you switch back:
git switch mainthe login changes may no longer appear in your working files. The changes are not lost; they belong to the other branch.
Switch back again:
git switch feature/loginand the code returns.
This is one of the key ideas behind branches: different branches can represent different versions of the project at the same time.
Practice
You are on main and about to build search. Write the complete command sequence to inspect the current branch, create and switch to feature/search, and confirm your location again.