How to Apply Stashes in Git
This article will cover the different ways to apply stashes in Git. It will show the steps to apply a specific Git stash.
Apply Stashes in Git
We use the git stash list
to show our Git stash if you are unaware.
pc@JOHN MINGW64 ~/Git (main)
$ git stash list
stash@{0}: WIP on main: 78129a6 Revert "$git status"
stash@{1}: WIP on main: 195e5c3 $git status
You can see we have two stashes in our Git branch.
Running a git stash apply
will point to the top of your stack. As shown below, you need to add an argument if you want to apply a specific stash.
$ git stash apply stash@{stash_index}
In our case, we want to apply the index 1
stash. We run the command below.
$ git stash apply stash@{1}
Removing text.txt.txt
CONFLICT (modify/delete): Tutorial.txt deleted in Updated upstream and modified in Stashed changes. Version Stashed changes of Tutorial.txt left in tree.
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: text.txt.txt
modified: text.txt.txt.bak
Unmerged paths:
(use "git restore --staged <file>..." to unstage)
(use "git add/rm <file>..." as appropriate to mark resolution)
deleted by us: Tutorial.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
.bash_history.bak
Delftscopetech/
New folder/
file.patch
However, when applied, this command does not delete the stash from your list. You should run a git stash pop
command.
There is a disparity between git stash apply
and git stash pop
is the latter applies changes and deletes the stash from your list.
Let us look at an example.
pc@JOHN MINGW64 ~/Git/Delftscopetech (main)
$ git stash pop stash@{0}
Removing README.md
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: README.md
no changes added to commit (use "git add" and/or "git commit -a")
Dropped stash@{0} (1531151482186b97f47e9b852ac29ddd194bd099)
We can see Git apply our stash and delete it from our output above.
An alternative is running the git stash drop stash@{Index}
. To clear all stashes, run the git stash clear
command.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn