Open In App

How to Create Git Branch With Current Changes?

Last Updated : 21 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Creating a new branch in Git while preserving your current changes is a common task that allows you to work on new features, bug fixes, or other tasks without affecting the main codebase. This article will guide you through one approach to achieve this, providing a detailed description, syntax, and examples for each method.

Stashing Changes

Stashing allows you to temporarily save your changes in a "stash" and revert your working directory to a clean state. You can then create a new branch and apply the stashed changes to that branch.

Syntax

Step 1: Stash the changes

git stash

Step 2: Create a new branch

git checkout -b new-branch-name

Step 3: Apply the stashed changes

git stash apply

Step 4: Delete the stash (optional)

git stash drop

Example:

Step 1: Save current changes

git stash
Screenshot-2024-05-22-193328
Git Stash

Step 2: Create and switch to a new branch

git checkout -b new-feature-branch
Screenshot-2024-06-05-190250
Switch to new Branch

Step 3: Reapply the stashed changes

git stash apply
Screenshot-2024-06-05-190341
Apply the stash

Step 4: Delete the stash if no longer needed

git stash drop
Screenshot-2024-06-05-190424
Delete Stash

Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg