Back to Blog
Lesson 15 of the Git & GitHub: Git & GitHub from Zero course
GitAugust 2, 20263 min read

Creating Branches: How to Use Git Branch to Manage Workflows

Learn how to use git branch to create, list, and manage independent lines of development. Master the core branching workflow to organize your project today.

gitversion controlbranchingworkflowterminal
Organized whiteboard with colorful sticky notes used for planning and brainstorming.

Previously in this course, we learned how to navigate your commit history using navigating-commits-mastering-git-checkout-and-head. Now that you can move through time, it’s time to learn how to move through parallel universes.

In professional software development, you never work directly on your "production-ready" code. Instead, you use branching to isolate your changes. This lesson covers how to create new branches and list existing ones using git branch.

Understanding Branching from First Principles

Think of your repository as a timeline. By default, Git gives you one timeline called main.

When you branch, you are essentially taking a snapshot of the current state of your project and creating a new, independent path. Any commits you make on this new branch do not affect the main branch. This allows you to experiment, fix bugs, or build new features without risking the stability of your core project.

The git branch command is your primary tool for managing these timelines. It doesn't switch you to the branch—it simply creates the pointer or lists the ones you already have.

Creating and Listing Branches

To see what branches currently exist in your project, run:

Bash
git branch

This will output a list of your local branches. The branch marked with an asterisk (*) and usually highlighted in color is the one you are currently "on."

Creating a New Branch

To create a new branch, you provide a name to the command:

Bash
git branch feature-login-page

This creates a new pointer named feature-login-page that points to the exact commit you are currently standing on. Crucially, this command does not switch you to the new branch. Your terminal is still "pointing" to your previous branch (usually main).

Verification

After running the command above, check your list again:

Bash
git branch

You will see:

  • main
  • feature-login-page

The asterisk will still be next to main. You have created a new branch, but you haven't moved into it yet.

Hands-on Exercise

Let’s apply this to your running project. Open your terminal in your project directory and follow these steps:

  1. Check your current branch: Run git branch to confirm you are on main.
  2. Create a new branch: Name it add-readme-description by running git branch add-readme-description.
  3. List your branches: Verify that both main and add-readme-description appear in the list.
  4. Observe the active branch: Note that the asterisk is still beside main.

Common Pitfalls

  • Forgetting to Switch: Beginners often create a branch and immediately start typing code, forgetting they are still on main. Always check your status with git status or git branch before you start working. (We will cover how to switch branches in the next lesson).
  • Naming Conventions: While you can name a branch anything, avoid using spaces or special characters. Use hyphens or underscores (e.g., feature-header or bugfix/login-error) to keep your workflow clean and CLI-friendly.
  • Creating a Branch from the Wrong Commit: Remember that git branch creates a branch based on your current location. If you want to branch off a specific past commit, you must navigate there first using navigating-commits-mastering-git-checkout-and-head.

FAQ

Does git branch move me to the new branch? No. It only creates a new pointer. You need a separate command to switch your working directory to the new branch.

Can I have multiple branches with the same name? No, branch names must be unique within your local repository.

What happens if I delete a branch? If you delete a branch, the commits associated with it remain in the database (unless they aren't reachable by any other branch), but the pointer to that timeline is removed. We will discuss safe deletion in a later lesson.

Recap

Branching is the cornerstone of a collaborative, professional workflow. By using git branch, you create isolated environments where you can experiment safely. Remember: git branch creates the lane, but it doesn't put you in the driver's seat.

Up next: We will learn how to switch into these branches and start making changes using git switch.

Similar Posts