How to Create a Branch from Another Branch in Git

In Git, branching is a powerful way to work on features, fixes, or experiments without affecting the main codebase. Often, you may need to create a new branch based not from main, but from an existing feature or development branch. This is especially useful when you’re building on someone else’s work or continuing a related task.

In this guide, you’ll learn how to create a new branch from another branch in Git, step by step.


✅ Why Create a Branch from Another Branch?

Creating a branch from a non-main branch is useful when:

  • You’re working on a sub-task of a larger feature.
  • You want to continue a teammate’s work.
  • You need to test or debug from a specific state of the codebase.
  • The changes you need are not yet merged into main.

🛠️ Step-by-Step: Create a Branch from Another Branch

1. List All Branches (Optional)

To see existing branches:

git branch -a

2. Switch to the Source Branch

Navigate to the branch you want to branch from:

git checkout existing-branch

Example:

git checkout feature/payment-integration

Make sure the branch is up to date:

git pull origin existing-branch

3. Create and Switch to the New Branch

Now, create a new branch based on the current one:

git checkout -b new-branch-name

Example:

git checkout -b feature/payment-validation

This creates a new branch based on the current feature/payment-integration branch and switches you to it.


4. Push the New Branch to Remote

Once you’re ready to share your branch:

git push -u origin new-branch-name

Example:

git push -u origin feature/payment-validation

The -u flag sets the upstream so future git push or git pull commands apply to this branch by default.


🔁 Summary Workflow

git checkout source-branch
git pull origin source-branch
git checkout -b new-branch
git push -u origin new-branch

🧠 Pro Tips

  • Use descriptive names for your branches, such as bug/fix-login, feature/signup-form, or test/api-endpoints.
  • Regularly merge the base branch into your new branch to keep it up to date.
  • Always pull before branching to avoid outdated code.

🏁 Conclusion

Creating a branch from another branch in Git gives you flexibility and control over how you manage your development workflow. Whether you’re continuing a feature or collaborating on a shared task, this technique helps you stay organized and avoid conflicts.

Sharing Is Caring:

Leave a Comment