Creating a new Git branch from an existing one is one of the most fundamental and powerful operations you will perform as a developer. It allows you to diverge from a stable codebase, experiment with new features, or fix bugs without affecting the main project. In practice, whether you are building a minor UI tweak or developing a major feature, understanding how to create git branch from another branch efficiently is crucial for your version control workflow. This guide will walk you through the various methods, explain the underlying mechanics, and share best practices to keep your repository clean and organized Not complicated — just consistent..
Understanding Git Branching Basics
Before diving into the commands, Make sure you understand what a Git branch actually is. Every time you make a change and save it, Git creates a new commit, and the branch pointer simply moves forward to that new commit. In Git, a branch is not a heavy, full copy of your code; rather, it is a lightweight movable pointer to a specific commit. It matters. This design makes branching incredibly fast and resource-efficient.
When you create a branch from another branch, you are essentially creating a new pointer that initially points to the exact same commit as the source branch. This means your new branch inherits all the commits, files, and history of the branch you are branching from, allowing you to build upon existing work smoothly It's one of those things that adds up..
Methods to Create a Git Branch from Another Branch
There are several ways to accomplish this task, depending on your preference and whether you want to switch to the new branch immediately or just create it locally. Here are the most common methods:
Method 1: The One-Liner Way (Creating and Switching)
The most efficient way to create a new branch and switch to it in a single command is using the -b or -c flag. This is highly recommended for developers who want to jump straight into their new work environment.
To create and switch from the main branch to a new feature-login branch
To create and switch from the main branch to a new feature-login branch, you would run:
git switch -c feature-login main
or, using the older checkout command:
git checkout -b feature-login main
Both commands achieve the same result: they create a new branch named feature-login that starts from the tip of main and immediately switch your working directory into it. The -c flag in git switch and -b in git checkout are short for “create”. If you omit the target branch, Git will base the new branch on the currently checked‑out branch instead.
Method 2: Create Without Switching
Sometimes you want to keep your current branch untouched and simply generate a new one for later use. The classic git branch command does exactly that:
git branch feature-login main
This creates a lightweight pointer called feature-login that points to the same commit as main. You remain on your current branch, and you can later switch to the new branch with:
git switch feature-login
or
git checkout feature-login
If you need to push the new branch to a remote repository, you can do so in one step:
git push origin feature-login:main
(Adjust the refspec if you want the remote branch to have a different name.)
Method 3: Using git switch and git checkout for Modern Workflows
Git introduced git switch in version 2.Consider this: 23 as a more intuitive companion to git branch. It focuses solely on changing the active branch, while git checkout still handles file‑level restoration.
| Goal | Command | Notes |
|---|---|---|
| Create and switch | git switch -c new-branch |
Uses the current HEAD as the source. Consider this: |
| Create from an explicit commit | git switch -c new-branch HEAD~2 |
Starts from a specific ancestor. That's why |
| Switch without creating | git switch existing-branch |
No -c flag needed. |
| Delete a merged branch | git branch -d old-branch |
Use -D for force deletion. |
If you prefer the brevity of git checkout, the same operations are available with -b (create) and without it (just switch) That alone is useful..
Best Practices for Branch Management
-
Adopt a Naming Convention
- Use prefixes like
feat/,fix/,docs/,chore/, etc., to make branch purposes instantly recognizable. - Avoid branches named
temp,hack, orWIPunless they are truly temporary.
- Use prefixes like
-
Keep Branches Short‑Lived
- Aim to merge feature branches within a few days. Long‑lived branches increase merge conflicts and drift from the main codebase.
- For larger, longer projects, consider using a
developorreleasebranch alongsidemain.
-
Use Topic Branches for Isolated Work
- Topic branches are perfect for single features, bug fixes, or experimental code. They keep the history clean and make code review easier.
-
Regularly Pull from Upstream
- Before you start work, fetch and merge (or rebase) the latest changes from the remote
main(or your organization’s default branch). This reduces integration pain later.
- Before you start work, fetch and merge (or rebase) the latest changes from the remote
-
Communicate Your Branches
- Push feature branches to the remote repository early (
git push origin feature-login). This creates a backup and allows collaborators to review or assist.
- Push feature branches to the remote repository early (
-
Clean Up After Merging
- Once a branch is merged, delete it both locally and remotely:
git branch -d feature-login # local delete (if merged) git push origin --delete feature-login
- Once a branch is merged, delete it both locally and remotely:
-
put to work Pull Requests (PRs)
- Modern hosting services (GitHub, GitLab, Bitbucket) turn remote branches into PRs, enabling code review, CI/CD testing, and documented changes.
-
Avoid Force‑Pushing in Shared History
- Force‑push (
git push --force) rewrites history and can disrupt teammates. Use it only for brand‑new branches or when you have explicit permission.
- Force‑push (
Advanced Techniques
Rebasing vs. Merging
- Rebasing moves your branch’s commits to sit on top of the target branch, producing a linear history. Use it for feature branches that are not yet shared widely.
git rebase