Create a branch in Git from another branch

Learn create a branch in git from another branch with practical examples, diagrams, and best practices. Covers git, git-branch, git-workflow development techniques with visual explanations.

How to Create a New Git Branch from an Existing One

Hero image for Create a branch in Git from another branch

Learn the essential Git commands and best practices for creating new branches from any existing branch, a fundamental skill for collaborative development.

Git branching is a core feature that allows developers to diverge from the main line of development and work on new features, bug fixes, or experiments in isolation. This article will guide you through the process of creating a new branch from an existing one, explaining the underlying concepts and providing practical commands. Understanding this process is crucial for maintaining a clean and efficient workflow in any Git-managed project.

Understanding Git Branches

In Git, a branch is essentially a lightweight movable pointer to a commit. When you create a new branch, you're creating a new pointer to the current commit you're on. This allows you to make changes without affecting other branches. The 'master' or 'main' branch is typically the default branch in a repository, representing the stable version of your project. However, you can create branches from any existing branch, not just the main one.

graph TD
    A[Initial Commit] --> B[Feature A Commit 1]
    B --> C[Feature A Commit 2]
    C --> D(master/main branch)
    C --> E(feature/new-feature branch)
    E --> F[New Feature Commit 1]
    F --> G[New Feature Commit 2]

Visual representation of Git branching from a common commit.

Creating a Branch from Your Current Location

The most common scenario is creating a new branch from your current working branch. This is straightforward and involves two primary commands: git branch to create the branch and git checkout to switch to it. Alternatively, you can use a single command, git checkout -b, which combines both actions.

# Step 1: Create a new branch named 'feature/my-new-feature'
git branch feature/my-new-feature

# Step 2: Switch to the newly created branch
git checkout feature/my-new-feature

Creating and switching to a new branch using two separate commands.

# Create and switch to a new branch in one command
git checkout -b feature/my-new-feature

Using git checkout -b for a combined create and switch operation.

Creating a Branch from a Specific Existing Branch

Sometimes, you might be on one branch (e.g., feature/old-feature) but need to create a new branch from another existing branch (e.g., develop) without switching to develop first. Git allows you to specify the starting point for your new branch directly.

# Create 'feature/another-feature' from 'develop' while currently on 'feature/old-feature'
git branch feature/another-feature develop

# Now, switch to the newly created branch
git checkout feature/another-feature

Creating a branch from a specific existing branch without switching to it first.

# Combine creation and switching from a specific branch
git checkout -b feature/another-feature develop

Using git checkout -b <new-branch-name> <start-point> for efficiency.

Verifying Your Branches

After creating a new branch, it's good practice to verify that it exists and that you are currently on the correct branch. The git branch command is your friend here.

# List all local branches
git branch

# List all local and remote branches
git branch -a

Commands to list and verify your Git branches. The current branch will be highlighted (e.g., with an asterisk).

1. Choose Your Base Branch

Decide which existing branch you want to base your new work on (e.g., main, develop, or another feature branch).

2. Update Your Base Branch

Before creating a new branch, ensure your local copy of the base branch is up-to-date: git checkout <base-branch> then git pull.

3. Create and Switch

Use git checkout -b <new-branch-name> <base-branch> to create and switch to your new branch in one go. If you're already on the base branch, simply use git checkout -b <new-branch-name>.

4. Start Developing

You are now on your new branch, ready to make changes without affecting other parts of the project.