In the previous article, you learned what a Git branch is and why branches are one of Git's most powerful features.
Now it's time to start using them.
Imagine you are developing a Student Management System. The current version of your project is stable, but you need to add a new feature called Student Attendance.
Instead of modifying the stable code directly, you decide to create a separate branch.
How do you create a branch?
How do you switch to it?
How do you see all available branches?
How do you delete a branch after it has been merged?
Git provides simple commands to perform all these tasks:
git branchgit switch
In this article, you will learn:
How to list branches
How to create branches
How to switch branches
How to create and switch in one step
How to rename branches
How to delete branches
The difference between
git switchandgit checkoutCommon mistakes
Best practices
By the end of this article, you'll be able to manage branches confidently in your own projects.
Listing All Branches
To see all local branches in your repository, use:
git branch
Example output:
* main
The asterisk (*) indicates the branch you are currently working on.
In this example, you are on the main branch.
Understanding the Output
Suppose your repository contains three branches.
Running:
git branch
may display:
* main
feature-login
feature-payment
Here:
mainis your current branch.feature-loginexists but is inactive.feature-paymentalso exists but is inactive.
Only one branch can be checked out at a time.
Creating a New Branch
To create a new branch:
git branch feature-attendance
Git creates the branch.
However, something important happens...
You remain on your current branch.
Run:
git branch
Output:
* main
feature-attendance
Notice that the asterisk is still next to main.
The new branch exists, but you are not using it yet.
Visual Representation
Before creating the branch:
main
↓
Commit A
↓
Commit B
After:
git branch feature-attendance
The repository looks like:
main
│
▼
Commit B
feature-attendance
│
▼
Commit B
Both branches point to the same commit.
Switching to Another Branch
To start working on the new branch:
git switch feature-attendance
Git displays:
Switched to branch 'feature-attendance'
Now:
git branch
Output:
main
* feature-attendance
The asterisk has moved.
You are now working on the new branch.
Understanding HEAD
When you switch branches, Git moves the HEAD pointer.
Initially:
HEAD
↓
main
After switching:
HEAD
↓
feature-attendance
Every new commit will now be added to feature-attendance.
Creating and Switching in One Command
Instead of:
git branch feature-login
git switch feature-login
You can use:
git switch -c feature-login
The -c option means:
Create a new branch and immediately switch to it.
This is one of the most commonly used commands in modern Git workflows.
Working on Different Branches
Suppose:
main
contains:
System.out.println("Version 1");
You switch:
git switch feature-login
Now you modify the file:
System.out.println("Version 2");
Commit the change.
When you switch back:
git switch main
You'll see the original version again because main has not received the new commit yet.
This isolation is what makes branches so useful.
Listing Branches with More Information
Use:
git branch -v
Example output:
* main a3f29b1 Initial project
feature-login d8e45c7 Add login page
This shows:
Branch name
Latest commit hash (short form)
Latest commit message
Showing Merged Branches
To see branches that have already been merged into the current branch:
git branch --merged
Example:
* main
feature-login
These branches are generally safe to delete if you no longer need them.
Showing Unmerged Branches
Use:
git branch --no-merged
Example:
feature-payment
feature-dashboard
These branches still contain work that has not been merged into the current branch.
Renaming a Branch
Suppose you're currently on:
feature-login
Rename it:
git branch -m feature-authentication
Now:
git branch
Output:
* feature-authentication
main
Renaming Another Branch
If you're not currently on the branch you want to rename:
git branch -m old-name new-name
Example:
git branch -m feature-ui feature-interface
Deleting a Branch
Suppose feature-login has already been merged into main.
Delete it:
git branch -d feature-login
Git removes the branch reference.
The commits remain in the repository because they have already been merged.
Safe Delete vs Force Delete
Safe deletion:
git branch -d feature-login
Git checks whether the branch has been merged.
If it has not, Git refuses to delete it.
Force deletion:
git branch -D feature-login
The uppercase -D forces the deletion even if the branch has not been merged.
Use this carefully because unmerged work may become harder to recover.
Can You Delete the Current Branch?
No.
Suppose you're currently on:
feature-login
Running:
git branch -d feature-login
will fail.
Switch to another branch first:
git switch main
Then delete:
git branch -d feature-login
git switch vs git checkout
Older versions of Git used:
git checkout feature-login
to switch branches.
Today, Git recommends:
git switch feature-login
Why?
Because git checkout had multiple responsibilities:
Switching branches
Restoring files
This confused many beginners.
Git introduced:
git switch→ For switching branchesgit restore→ For restoring files
These commands are clearer and easier to understand.
Real-World Example
Suppose you work on an Online Shopping website.
Current branch:
main
Create:
git switch -c feature-cart
Develop the shopping cart.
Commit your work.
Switch back:
git switch main
Your stable version remains unchanged until you intentionally merge the new feature.
Common Beginner Mistakes
Mistake 1
Creating a branch but forgetting to switch to it.
Remember:
git branch feature-login
does not change your current branch.
Mistake 2
Using -D without understanding the consequences.
Always try:
git branch -d
before using:
git branch -D
Mistake 3
Making new feature changes directly on main.
Create a feature branch instead.
Mistake 4
Deleting branches before ensuring their work has been merged or backed up.
Best Practices
Keep
mainstable.Create a separate branch for each feature or bug fix.
Use meaningful branch names such as:
feature-loginfeature-paymentbugfix-authentication
Delete branches after they have been merged.
Use
git branch -vto review branch status regularly.
Useful Branch Commands
| Command | Purpose |
|---|---|
git branch | List local branches |
git branch <name> | Create a new branch |
git switch <name> | Switch to a branch |
git switch -c <name> | Create and switch to a new branch |
git branch -v | Show branches with latest commit |
git branch --merged | List merged branches |
git branch --no-merged | List unmerged branches |
git branch -m <new-name> | Rename the current branch |
git branch -m <old> <new> | Rename another branch |
git branch -d <name> | Delete a merged branch |
git branch -D <name> | Force delete a branch |
Summary
Managing branches is an essential Git skill. The git branch command lets you create, list, rename, and delete branches, while git switch provides a modern and straightforward way to move between them.
By creating a separate branch for each feature or bug fix, you can work safely without affecting the stable version of your project. Once your work is complete, the branch can be merged and, if no longer needed, deleted to keep the repository organized.
Comments
Post a Comment