How to Stash Specific Files in Git

Azhar Bashir Khan Feb 02, 2024
How to Stash Specific Files in Git

This tutorial will see about stashing changes of only specific files in Git.

In Git, when we make some changes in the working tree, we may have some changes that may be staged in the local repository or not yet staged.

We may now want to save these changes for a while and work on a version of the files before these changes take place.

Thus, for such purposes, we can use the git stash push command to stash the changes (i.e.) for later use.

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

We may sometimes require to save or stash changes done to specific files only, instead of all the files that are changed in the working tree of the repository.

We will now illustrate this with an example.

Using git stash push to Stash Changes of Specific Files in Git

Suppose we have a file named example.txt in the working tree of the repository. We have made some changes to the file, and now we want to stash those changes for a while in Git.

We may also have changes in the other files in the working tree, but we do not want to stash them.

The syntax of the command to stash specific files in Git is git stash push <file>.

Thus, we do as follows to stash the changes done only to the file example.txt.

$ git stash push example.txt
Saved working directory and index state On main: example.txt
HEAD is now at 8b3560b minor changes

Thus, we have stashed the changes only of the file example.txt.

We can now list the stash entries and see our stash entry, as follows.

$ git stash list
stash@{0}: On main: example.txt

We can also add a specific message while stashing as follows.

$ git stash push -m "my example stash" example.txt
Saved working directory and index state On main: my example stash example.txt
HEAD is now at 8b3560b minor changes

We can again list it as follows.

$ git stash list
stash@{0}: On main: my example stash example.txt

Apart from this, we can also interactively stash specific changes of the desired files in Git.

For this, we need to use the option --patch or -p along with the git stash push command.

Thus, to do this in our example, we do as follows.

$ git stash push --patch
diff --git a/example.txt
index 7ab5ca4..a281fc6 100644
--- a/example.txt	
+++ b/example.txt	
@@ -4,9 +4,11 @@
 
 some text
+new text
 
some other text
some text again
+second new text


Stash this hunk [y,n,q,a,d,/,s,e,?]? y

Saved working directory and index state WIP on main: 8b3560b minor changes

The git stash command iterates through all the changes in the working tree of the repository and prompts to choose the changes to stash.

Here, we have entered option y besides the question, Stash this hunk [y,n,q,a,d,/,s,e,?]? to stash the example.txt file changes.

We can enter the option n to not stash the file’s current hunk (the change).

For more information on git stash and available options, see this site viz. git stash - Saving Changes.

Related Article - Git Stash