Listing and Identifying Local Branch States
In this lesson, you will learn how to verify which branches exist in your local repository and how to confirm which one is currently active.
As you begin building features in isolated branches, it is easy to lose track of where you are. Git provides a simple command to audit your environment so you can ensure your next commit lands exactly where it belongs.
The git branch Command
To list all the branches currently residing in your local repository, run the following command in your terminal:
git branch
When you execute this command, Git will display a list of all your local branches. This output serves two purposes:
- Identification: It shows you the names of every branch you have created or checked out locally.
- State Inspection: One of the lines in the list will be prefixed with an asterisk (
*) and usually highlighted in a different color. This indicates your active branch—the "current" branch where your next commit will be recorded.
Understanding the Output
If you were working on a feature and ran the command, your terminal might look like this:
main
* feature-login-page
fix-typo-in-readme
In this example, the asterisk shows that feature-login-page is your active branch. Any snapshots you capture using your commit workflow will be attached to this branch. The other branches (main and fix-typo-in-readme) exist in your local directory, but they are currently dormant.
By checking this list regularly, you avoid the common pitfall of accidentally adding new work to the wrong branch. If you notice you are on the wrong branch, you now have the clarity needed to switch back to the correct context before proceeding with your staging and committing process.
Key Takeaway: Use the git branch command to list all local branches and identify your active branch by locating the one marked with an asterisk.