Git: Create a Branch From Unstaged/uncommitted Changes on Master
Creating a branch from unstaged or uncommitted changes in Git involves a few steps. Here’s a detailed guide on how to do this:
Steps to Create a Branch from Unstaged/Uncommitted Changes
Stash Your Uncommitted Changes
First, you need to temporarily store your uncommitted changes using
git stash
. This will allow you to switch branches without losing your changes.git stash
- **`git stash`**: Saves your local modifications away and reverts the working directory to match the `HEAD` commit.
Create and Switch to a New Branch
Once your changes are stashed, create a new branch and switch to it:
git checkout -b <new-branch-name>
- **`<new-branch-name>`**: Replace this with the name of your new branch.
**Example**:
```
git checkout -b feature/new-branch
```
This creates a new branch called `feature/new-branch` and switches to it.
Apply Stashed Changes to the New Branch
Apply the stashed changes to your new branch:
git stash pop
- **`git stash pop`**: Applies the most recent stash and removes it from the stash list.
If there are conflicts during the stash pop, resolve them as needed.
Commit Your Changes
After applying the stashed changes, you can now add and commit them to the new branch:
git add . git commit -m "Describe your changes"
- **`git add .`**: Stages all modified and new files.
- **`git commit -m "Describe your changes"`**: Commits the staged changes with a message.
Alternative Approach: Create a Branch Directly From Uncommitted Changes
If you prefer not to use git stash
, you can create a new branch directly from your current state. However, note that this approach may not work if you have conflicts or if you need to cleanly separate your changes. Here’s how you can do it:
Create and Switch to a New Branch
git checkout -b <new-branch-name>
Example:
git checkout -b feature/new-branch
This creates and switches to the new branch.
Commit Your Changes
Add and commit your uncommitted changes on the new branch:
git add . git commit -m "Describe your changes"
This commits your current changes directly to the new branch.
Summary
Stash Uncommitted Changes:
git stash
Create and Switch to New Branch:
git checkout -b <new-branch-name>
Apply Stashed Changes:
git stash pop
Commit Changes:
git add . git commit -m "Describe your changes"
Alternatively, if you want to avoid stashing:
Create and Switch to New Branch:
git checkout -b <new-branch-name>
Commit Changes Directly:
git add . git commit -m "Describe your changes"
By following these steps, you can effectively create a branch from uncommitted changes, allowing you to manage and organize your work in Git more effectively.
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github