Create Git branch with current changes
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When working with Git, a distributed version control system, managing changes across different features or parts of your application efficiently is crucial. Sometimes, you start working on a new feature or a bug fix and realize that you need to switch context or save the current state of work without committing it directly to the master branch. This scenario is where creating a new branch with the current changes comes into play.
Understanding Git Branches
A Git branch represents an independent line of development in a project, enabling you to diversify development without affecting the stable codebase. The main branch is usually called master or main. When you create a new branch, Git effectively duplicates the state of the main branch (or any other branch you branch from) at that moment, allowing you to continue your work isolated from changes made by others or in other branches.
Reasons to Create a New Branch With Current Changes
- Context switching: If you need to prioritize another feature or fix another bug urgently.
- Experimentation: When trying out new features that might not make it into the final project.
- Code review: Clean separation of new features or bug fixes for easier review process.
Step-By-Step Guide to Creating a Git Branch With Current Changes
Here is how you can create a new branch and carry over your current changes without committing them first.
Step 1: Stash Your Changes (if needed)
If you have uncommitted changes that you do not want to commit yet, you should stash them:
This command temporarily shelves changes you've made to your working directory and reverts it to match the HEAD commit.
Step 2: Create and Switch to the New Branch
The above command does two things: it creates a new branch called new-feature-branch and checks it out, so you are now working on this new branch.
Step 3: Apply Your Stashed Changes
To bring back your stashed changes onto the new branch, use:
This command restores the previously stashed changes and deletes the stash. If you want to keep the stash, you can use git stash apply instead.
Example: Integrating Changes into a Branch
Let's consider an example where you are working on some improvements in your local main branch and realize that it should go into a separate feature branch.
- Stash current changes:
git stash save "Feature improvements" - Create and switch to a new branch:
git checkout -b improve-feature - Apply stashed changes:
git stash pop
Now, your new branch improve-feature has the improvements you were working on, isolated from main.
Summary Table
| Command | Description |
git stash | Temporarily shelves changes in the working directory. |
git checkout | Switches branches or restores working tree files. |
git checkout -b | Creates a new branch and checks it out at the same time. |
Conclusion
Creating a new Git branch with current changes is a powerful way to manage multifaceted development workflows, allowing for safe experimentation, context switching, and feature-specific work. By understanding and utilizing the branching and stashing features of Git, you enhance your ability to maintain a clean and robust codebase.

