This study will explain:
- How to Create/Make a New Branch From the Latest Stash?
- How to Create/Make a New Branch From an Earlier Stash?
How to Create/Make a New Branch From the Latest Stash?
To create a new branch from the latest stash:
- First, view the latest stash changes.
- Then, run the βgit stash branch <branch-name>β command to create a branch from the latest stash and apply its changes to it.
- Lastly, verify the newly created branch.
Step 1: View Available Stashes
First, type out the following command to display the list of available stashes in the repository:
It can be seen that the repository contains three stashes:
Step 2: View Latest Stash Changes
Then, view the latest stash changes with the help of given-provided command:
According to the below output the latest stash contains only one βnew.txtβ fileβs changes:
Step 3: Create New Branch From Latest Stash
Now, execute the βgit stash branchβ command along with the desired branch name to create it. Here, βalphaβ is our new branch name:
The below screenshot indicates that the βalphaβ branch has been created from the latest stash with all changes:
Step 4: Verify Branch
Finally, verify whether the new branch has been created or not by viewing all branches:
The below output displays the newly created βalphaβ branch:
How to Create/Make a New Branch From an Earlier Stash?
To create a new branch from the earlier stash, utilize the βgit stash branch <branch-name> stash@{reference-no}β command and specify the branch name and reference.
Step 1: Navigate to the βmasterβ Branch
First, move to the βmasterβ branch:
Step 2: List Available Stashes
Then, display the list of all stashes in the repository and choose the desired stash reference:
According to the below image, the current repository contains two stashes:
Step 3: Create New Branch From Earlier Stash
Utilize the βgit stash branchβ command and specify the new branch name and the stash reference to create a new branch from it. For instance, we have specified the βstash@{1}β stash reference to create a βfeatureβ branch:
In the below output, it can be observed that the new βfeatureβ branch has been created successfully including all its changes:
Step 4: Verify Branch
Finally, view all available branches to ensure whether the new branch has been created or not:
Thatβs all about creating a new branch from the stash in Git.
Conclusion
Git permits developers to create a new branch from stash. The βgit stash branch <branch-name>β command is utilized to create a branch from the latest stash. Moreover, users can create a new branch from an earlier stash using the βgit stash branch <branch-name> stash@{reference-no}β command. This study explained the methods to create or make a branch from a stash.