How to Stash Changes of the Files by Name in Git

Azhar Bashir Khan Feb 02, 2024
How to Stash Changes of the Files by Name in Git

This tutorial will introduce how to stash changes of the files by name in Git.

In Git, we may want to save the changes for a while and work on the version of the files before these changes took place.

We can use the git stash push command to stash the changes to save them for later use.

Again, later, we can use the git stash pop command to get these changes back.

Sometimes, we may want to save the stash entry with a name for ease of use. We may wish to use the name of the stash entry to check for it in the stash list and use the name to retrieve the changes.

We will now illustrate this with an example.

Using git stash push to Stash Changes of Files by Name in Git

Suppose we have a file named sample.txt in the branch main in the Git repository. We may have made some changes to the file locally.

We can check the status of the repository as follows.

$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   sample.txt

Thus, we can see that the file sample.txt has some changes that need to be committed.

Now, we decide that instead of committing those changes, we may want to stash those changes with a name for easy retrieval later.

The syntax of the git stash push command to stash changes with a name is git stash push -m <stash_name>.

We will now stash the changes of the file sample.txt with a name as follows.

$ git stash push -m "my_stash"
Saved working directory and index state On master: my_stash

We can see the given stash name in the stash list.

$ git stash list
stash@{0}: On master: my_stash

Thus, in the stash listing, we can see the stash entry with our given stash name viz. my_stash.

We will now again check the working tree changes, as follows.

$ git status
On branch main
nothing to commit, working tree clean

As we have done stashing, Git shows no new changes.

When needed, we can retrieve the changes from the stash store using the name given to the stash entry we just created.

We need to use the git stash apply command to retrieve the changes back into the working tree.

The syntax of the command git stash apply to retrieve the stash entry by name and apply the changes to the working tree is git stash apply stash^{/<stash_name>}.

Please note, we are using a regular expression with the stash name to get the desired stash entry.

Thus, we need to do as follows to retrieve the stash entry of the name my_stash.

$ git stash apply stash^{/my_stash}
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   sample.txt

We can now see the changes applied to the working tree, which we have retrieved from the stash, as follows.

$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   sample.txt

Unlike the git stash pop command, the git stash apply command does not remove the stashed state (i.e.) stash entry from the stash list. It only applies the given stash entry on top of the current working tree state.

Thus, we can still view the stash entry in the stash list.

$ git stash list
stash@{0}: On master: my_stash

We can remove the stash entry from the stash list.

We need to run the command as follows.

$ git stash clear

Caution: Please use this command with care because it removes all the stash entries.

To only remove a specific entry, as in our case, we need to do as follows.

$ git stash pop stash@{0}

Thus, in this case, the stash entry will now be removed from the stash list.

Related Article - Git Stash

Related Article - Git Push