Git Create Branch: Your Essential Guide to Workflow Management
To create a new branch in Git without switching to it, the command used is
git branch <branch-name>
. If my goal is to create a new branch and immediately switch to it, I prefer
git checkout -b <branch-name>
or, with newer Git versions,
git switch -c <branch-name>
.
Git branching is a core feature. It allows developers to diverge from the main line of development and continue working in isolation. This means you can work on new features, bug fixes, or experiments without impacting the stable version of your project. Branches in Git are lightweight pointers to commits. They are very efficient and ‘cheap’ to create, as Git itself is free and open-source software.
Understanding Git Branches
A Git branch is essentially a movable pointer to a specific commit in your project’s history. It is not a separate copy of your codebase or a distinct folder. Instead, it’s a 40-character file containing the SHA-1 hash of a commit, typically stored in the `.git/refs/heads/` directory. This design is why creating branches is so fast and requires minimal overhead. This is a significant advantage of Git compared to older version control systems.

When you create a new branch, Git records the current state of your repository at that commit. Any new commits you make while on that branch will extend its history without affecting other branches until you explicitly merge them. This isolation is crucial for maintaining a clean and stable main codebase.
How to Create a New Git Branch
Creating a new Git branch is a straightforward process, but the specific command you use depends on whether you want to switch to it immediately or keep working on your current branch.
Creating a Branch Without Switching to It
If you want to create a new branch but remain on your current branch (e.g., `main` or `develop`), use the `git branch` command followed by the desired branch name. This is useful when you want to prepare a branch for future work but aren’t ready to switch contexts yet.
-
- First, ensure you are on the branch you want to base your new branch on:
git checkout mainYou should see output like:
Switched to branch 'main' - Now, create the new branch:
git branch feature/new-loginThere will typically be no output if successful. To verify, run:
git branchThis will list all local branches, with an asterisk `` next to the currently active branch. You should see `feature/new-login` in the list, but ` main` should still indicate that the `main` branch is active.
- First, ensure you are on the branch you want to base your new branch on:
Creating a Branch and Switching to It Immediately
The most common workflow is to create a new branch and then immediately switch to it. This lets you start working on it. Git offers two primary commands for this:
Using `git checkout -b`
The `git checkout -b` command is a convenient shortcut. It both creates a new branch and switches your working directory to it.
- Ensure you are on the desired base branch (e.g., `main`):
git checkout mainYou should see:
Switched to branch 'main' - Create and switch to your new branch:
git checkout -b feature/user-profileYou should see output like:
Switched to a new branch 'feature/user-profile' - Verify your current branch:
git branchThe output should show ` feature/user-profile`, indicating you are now on the new branch.
Using `git switch -c` (Modern Git)
Git 2.23 introduced the `git switch` command. This separates the concerns of switching branches from restoring files. The `-c` flag for `git switch` specifically creates and switches to a new branch. It offers a clearer syntax for this operation.
- Ensure you are on the desired base branch:
git checkout mainYou should see:
Switched to branch 'main' - Create and switch to your new branch using `git switch -c`:
git switch -c bugfix/dashboard-errorYou should see output like:
Switched to a new branch 'bugfix/dashboard-error' - Verify your current branch:
git branchThe output should show ` bugfix/dashboard-error`.
We generally recommend `git switch -c` for creating and switching to new branches if you are using a recent Git version, as it is part of Git’s effort to make commands more intuitive and less overloaded.
Creating a Git Branch from Another Branch
By default, when you create a new branch using `git branch ` or `git checkout -b `, Git bases it on your currently checked-out branch. So, to create a branch from a specific existing branch other than your current one, you first need to check out that base branch.

- Switch to the branch you want to use as the base (e.g., `develop`):
git checkout developYou should see:
Switched to branch 'develop' - Now, create your new branch (and switch to it if desired):
git checkout -b feature/advanced-searchYou should see:
Switched to a new branch 'feature/advanced-search'Alternatively, if you want to create the branch but stay on `develop`:
git branch feature/advanced-searchNo output indicates success. Verify with `git branch`.
Creating a Branch from a Remote Branch
To create a local branch that tracks a remote branch, you typically use a similar approach, but Git often provides a shortcut for the first time you do it.
- First, ensure your local repository has the latest information from the remote:
git fetch originThis command downloads new commits and branches from the remote (e.g., `origin`) but doesn’t merge them into your local branches.
- To create a new local branch that tracks a remote branch (e.g., `origin/staging`), one can use:
git checkout -b local-staging origin/stagingThis creates a new local branch named `local-staging` and configures it to track `origin/staging`. You should see:
Switched to a new branch 'local-staging'Alternatively, if the local Git version is recent, a simpler command often works:
git checkout stagingIf a remote branch named `staging` exists and no local branch with that name exists, Git will automatically create a local `staging` branch that tracks `origin/staging`. You should see:
Branch 'staging' set up to track remote branch 'staging' from 'origin'. Switched to a new branch 'staging'
When to Create a Git Branch
We recommend creating a new Git branch whenever you start a distinct piece of work that you want to keep separate from your main development line. This practice is fundamental to efficient team collaboration and managing changes effectively. Here are common scenarios:
- New Feature Development: Always create a new branch (e.g., `feature/login-page`, `feature/payment-gateway`) for any new functionality. This keeps the `main` branch stable.
- Bug Fixes: For fixing bugs, create a dedicated branch (e.g., `bugfix/critical-error-123`). This allows for isolated testing and hotfixes without disrupting ongoing feature work.
- Experiments/Prototyping: If you’re trying out a new idea or experimenting with a different approach, a branch (e.g., `experiment/new-algorithm`) provides a safe sandbox.
- Release Preparation: Many teams use release branches (e.g., `release/v1.0.0`) to prepare for a software release. This allows for final testing and minor bug fixes without new features being introduced.
This strategy minimizes conflicts, enables concurrent development by multiple team members, and simplifies code review processes.
Creating a Branch from a Specific Commit or Tag
Sometimes, you might need to create a branch not from the tip of an existing branch, but from an older specific commit or a tag. A tag is a static pointer to a commit, often used to mark release points (e.g., `v1.0`).
From a Specific Commit
To create a branch from a specific commit, you need its SHA-1 hash (the unique identifier). You can find this hash using `git log`.
- Find the commit hash using `git log`. For example, to see the last 5 commits:
git log --oneline -5This might output something like:
a1b2c3d Add user authentication e4f5g6h Implement basic dashboard i7j8k9l Initial project setup ...Let’s say a branch is to be created from the commit `e4f5g6h`.
- Create the new branch from that commit:
git branch old-feature-base e4f5g6hThis creates a new branch named `old-feature-base` pointing to commit `e4f5g6h`. To switch to it, one would then run `git checkout old-feature-base` or `git switch old-feature-base`.
From a Tag
Creating a branch from a tag is similar, as a tag also points to a specific commit.
- List available tags:
git tagThis might output:
v1.0 v1.1 v2.0-beta - Create a new branch from a tag (e.g., `v1.0`):
git branch hotfix/v1.0-critical v1.0This creates a branch `hotfix/v1.0-critical` at the point marked by the `v1.0` tag. You can then `git checkout hotfix/v1.0-critical` to start working on it.
If Creating a Branch Still Fails
While `git branch` commands are generally solid, you might encounter issues. Here are a few common scenarios and their solutions:
- “fatal: A branch named ‘…’ already exists.”: This error means you’re trying to create a branch with a name that’s already in use. You need to choose a unique name or delete the existing branch if it’s no longer needed (`git branch -d `).
- “error: pathspec ‘…’ did not match any file(s) known to git.”: This usually happens when you try to create a branch from a non-existent commit hash or tag. Double-check the commit SHA or tag name for typos. Ensure you’ve run `git fetch` to get the latest remote references if you’re trying to branch off a remote-only commit or tag.
- “File name too long” error for branch names.: While Git itself is flexible, operating systems have limits on file path lengths. A Git branch is technically a file in `.git/refs/heads/`. If your branch name is excessively long, it might exceed these OS limits, especially on Windows. According to GitHub, branch names are typically limited to 250 bytes for ASCII characters. Or they are around 62 characters for 32-bit/4-byte Unicode characters. If you hit this, we recommend shortening your branch name.
- Detached HEAD state.: If you created a branch from a commit and then checked out that commit directly (e.g., `git checkout `) without creating a branch first, you’ll be in a “detached HEAD” state. This means new commits won’t be associated with a branch. To fix this, create a new branch from your current HEAD:
git checkout -b <new-branch-name>This will put you back on a named branch.
How to Confirm Your Branch Was Created and Switched
After creating or switching branches, it’s always good practice to confirm that you are on the intended branch. The primary command for this is `git branch`.

- Open your terminal or command prompt in your Git repository.
- Run the command:
git branchYou should see a list of all local branches. The branch you are currently on will be prefixed with an asterisk (``). For example, if you just created and switched to `feature/my-new-feature`, the output would include ` feature/my-new-feature`.
- You can also use `git status` to get a more detailed overview of your current branch and repository state:
git statusThe first line of the output will typically state `On branch `, confirming your active branch.
Related guides
Sources: Git
Frequently asked questions
Can I create a Git branch directly on GitHub?
Yes, you can create a Git branch directly on GitHub through its web interface. Navigate to your repository, click the branch dropdown menu (usually showing ‘main’ or ‘master’), type your new branch name, and press Enter or click ‘Create branch’.
What is the difference between `git branch` and `git checkout -b`?
`git branch ` creates a new branch but leaves you on your current branch. `git checkout -b ` creates a new branch and immediately switches your working directory to that new branch, making it ready for development.
Should I always create a new branch for every task?
We strongly recommend creating a new branch for every distinct task, feature, or bug fix. This practice isolates changes, simplifies code reviews, prevents conflicts on the main branch, and is a cornerstone of effective Git-based team workflows.