Create a branch in Git from another branch
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Git is a widely-used version control system that helps teams manage changes to source code over time. Its distributed nature allows developers to work simultaneously on different branches, which are essentially pointers to snapshots of the codebase at a particular point in time. An essential task in Git is creating a branch from another branch, enabling parallel development and experimentation without impacting the main codebase. In this article, we'll explore how to create a branch from another branch in Git, along with technical explanations and examples.
Understanding Branches in Git
A branch in Git represents a separate line of development, which allows for experimentation, feature development, or maintenance without affecting the main codebase. The default branch in Git is typically the main or master branch. When you create a new branch, Git creates a pointer to the current commit, and from that point onwards, you can work independently of other branches.
How Git Manages Branches
Internally, Git manages branches by creating a reference pointer (stored as a file in the .git/refs/heads/ directory) that points to the commit hash of the current snapshot. When you make a commit, Git advances the branch pointer, essentially appending to the branch history.
Creating a Branch from Another Branch
Creating a branch from a different branch in Git is straightforward and can be done using the command line. Here's the basic workflow:
Step 1: Navigate to the Existing Branch
Before you create a new branch, ensure you are on the branch you want to branch off from. Use the following commands:
Step 2: Create the New Branch
After switching to your source branch, create a new branch using the git checkout -b command:
Alternatively, you can create a new branch without switching by using:
This command creates the new branch without checking it out.
Step 3: Verify the Branch
List the branches to confirm that your new branch has been created and switched to, if applicable:
The current branch is indicated by an asterisk (*).
Example Use Case
Imagine you're working on a feature that requires testing a new library integration. Instead of working directly on the develop branch, which may affect ongoing development, you can create a new branch, feature/new-library, from develop for this purpose.
Execute the following:
Start making your changes and commits in the feature/new-library branch without impacting the develop branch.
Advantages of Branching
Branching in Git provides several advantages:
- Isolation: Work on new features or bug fixes without disturbing the main development line.
- Collaboration: Multiple developers can work on different branches simultaneously.
- Maintenance: Keep different lines of development for features, releases, or hotfixes.
- Experimentation: Create branches for quick trials without the fear of altering stable code.
Summary Table
Below is a summary table of key commands and concepts:
| Action | Command | Description |
| List branches | git branch | Displays all branches in the repository. |
| Switch branch | git checkout <branch-name> | Changes the active branch to <branch-name>. |
| Create and switch to branch | git checkout -b <new-branch-name> | Creates a new branch and switches to it. |
| Create branch without switching | git branch <new-branch-name> <source-branch-name> | Creates a new branch based on <source-branch-name>. |
| Verify current branch | git branch | Shows current branch and marks it with an asterisk (*). |
Additional Topics
Merging Branches
Once your work on the new branch is complete, you may want to merge the changes back into your main branch. This is typically done using git merge while on the main branch:
Best Practices for Branch Naming
Follow consistent naming conventions to make it easier to manage branches:
- Feature branches: Use
feature/<feature-name>. - Bugfix branches: Use
bugfix/<bug-description>. - Hotfix branches: Use
hotfix/<issue-description>.
Choose descriptive names that help quickly identify the branch's purpose.
Conclusion
Creating a branch from another in Git is a fundamental process in collaborative software development, enabling parallel work streams, code separation, and risk-free experimentation. By understanding and implementing best practices for working with branches, teams can improve their workflow efficiency and maintain the stability of the codebase.

