Managing Branches in Git
Learn how to create, view, rename, and delete Git branches while keeping your project organized.
1. What Is Branch Management?
Branch management is the process of organizing different lines of development in a Git repository.
Developers use branches to work on new features, fix bugs, and test changes without directly modifying the main branch.
2. View Existing Branches
Use the following command to display local branches:
git branch
Example output:
feature-login
* main
payment-module
The * symbol indicates the
currently selected branch.
View Local and Remote Branches
git branch -a
3. Create a New Branch
To create a branch without switching to it:
git branch feature-login
This creates a branch named
feature-login.
git branch
does not automatically switch to it.
4. Create and Switch to a Branch
The recommended modern Git command is:
git switch -c feature-registration
Alternatively, you can use:
git checkout -b feature-registration
Both commands create a new branch and immediately switch to it.
5. Switch Between Existing Branches
Switch to the main branch:
git switch main
Switch to a feature branch:
git switch feature-login
Git may prevent switching if your uncommitted changes would be overwritten. Commit, stash, or safely discard those changes first.
6. Rename a Branch
Rename the current branch:
git branch -m new-branch-name
Rename another local branch:
git branch -m old-branch-name new-branch-name
7. Delete a Branch
Delete a local branch after its changes have been merged:
git branch -d feature-login
Force-delete a local branch:
git branch -D feature-login
-D option can delete a branch
even if its changes have not been merged.
Use it carefully.
You cannot delete the branch currently checked out in your working tree. Switch to another branch first.
8. Push a Branch to GitHub
After creating and committing changes on a feature branch, publish it to GitHub:
git push -u origin feature-login
The -u option sets the upstream
tracking relationship, making future
push and pull commands easier.
9. Practical Example
Suppose you are developing a Login Module for a Student Management System.
# Switch to main
git switch main
# Create a new feature branch
git switch -c feature-login
# Check the active branch
git branch
# Stage your changes
git add .
# Commit your work
git commit -m "Add student login module"
# Push the branch to GitHub
git push -u origin feature-login
You can now create a pull request on GitHub to review and merge the login feature.
10. Common Branch Commands
| Command | Purpose |
|---|---|
git branch |
List local branches |
git branch -a |
List local and remote-tracking branches |
git branch feature |
Create a branch |
git switch feature |
Switch to a branch |
git switch -c feature |
Create and switch to a branch |
git branch -m new-name |
Rename the current branch |
git branch -d feature |
Safely delete a merged branch |
git branch -D feature |
Force-delete a branch |
git push -u origin feature |
Publish a branch and set upstream |
11. Best Practices
- Use meaningful branch names such as
feature-login. - Keep the main branch stable.
- Create separate branches for different features.
- Commit changes regularly with clear messages.
- Review changes before merging branches.
- Delete old branches after confirming they are no longer needed.