
Branches in Git isolate development work so you can make changes without affecting your main codebase. Listing branches reveals all the available lines of development in your repository. Creating a new branch lets you experiment, fix bugs, or build features independently from stable code.
This article explains how the git branch command lists local and remote branches and how to create new branches effectively using Git.
If you're already familiar with Git and just need a quick reference, use the following commands to list and create branches:
git branchThe git branch command supports several useful flags for managing and inspecting branches:
Creating a new branch in Git allows you to isolate development work such as bug fixes or feature additions from the main codebase.
git: Invokes the Git command-line tool.branch: Manages local branches.<branch-name>: The name of the branch to create.List all local branches.
Output:
The asterisk indicates that you're currently on the main branch.
Create a new branch named feature-login.
Git creates the new branch from the current main branch tip.
List the branches again to verify.
Output:
The new feature-login branch appears alongside existing ones.
Use the git switch command (available in Git 2.23 and later) to change your current working branch. This updates the working directory to reflect the state of the selected branch.
git switch: Changes the current working branch.<branch-name>: The name of the target branch.Check the current branch.
Output:
The asterisk marks the current branch (main).
Switch to the hotfix-header branch.
Output:
Confirm the active branch.
Output:
The asterisk now points to hotfix-header.
Use the -c flag with git switch to create and switch to a new branch in one step.
This creates a new branch named development from the current branch and makes it the active branch.
Confirm the branch switch.
Output:
The asterisk now points to development.
To track and switch to a remote branch that doesn’t exist locally:
Fetch the remote branches.
List available remote branches.
Output:
Create a local tracking branch and switch to it.
Git checks out a new local branch named feature-oauth linked to origin/feature-oauth.
Confirm the switch.
Output:
An emergency branch is a temporary branch created to isolate urgent fixes, such as production outages, security patches, or critical bugs. This approach allows you to address the issue without disrupting ongoing work in main or feature branches.
-c: Creates a new branch before switching.<emergency-branch-name>: The name of the emergency branch to create.Create and switch to an emergency branch.
This command creates a new branch named emergency-fix and makes it the active branch.
After committing your changes, switch back to the main branch.
Merge the emergency branch into main.
Once merged, delete the emergency branch to keep your branch list clean.
For a detailed article on deleting branches, see How to Delete a Branch in Git.
In this article, you explored how to list and create Git branches using the git branch and git switch commands. You also learned how to switch between branches, handle remote tracking, and manage emergency fixes with temporary branches. These techniques help streamline your development workflow, isolate changes safely, and support collaborative Git practices. For advanced usage and configuration, refer to the official Git documentation.
0 Comments
Be the first to comment and share your perspective with the community.