How to stash a specific file in Git?

Since git 2.13, there is a command to save a specific path to the stash: git stash push <path>. For example:

git stash push -m welcome_cart app/views/cart/welcome.thtml


You can do that using git stash --patch (or git stash -p) — you’ll enter interactive mode where you’ll be presented with each hunk that was changed. Use n to skip the files that you don’t want to stash, y when you encounter the one that you want to stash, and q to quit and leave the remaining hunks unstashed. a will stash the shown hunk and the rest of the hunks in that file.

Not the most user-friendly approach, but it gets the work done if you really need it.

How to git stash a specific file?

For stashing a specific file:

git stash -- filename.txt

For stashing more than one files:

git stash -- filename1.txt filename2.txt

Answer #2:

I usually add to index changes I don’t want to stash and then stash with --keep-index option.

git add app/controllers/cart_controller.php
git stash --keep-index
git reset

The last step is optional, but usually, you want it. It removes changes from the index.

Warning As noted in the comments, git stash --keep-index pushes everything onto the stash, both staged and unstaged. The --keep-index just leaves the index alone after the stash is done. This can cause merge conflicts when you later pop the stash.

Answer #3:

The -m option simply adds a message to your stash, and is entirely optional. Thus, the command

git stash push [paths you wish to stash]

is perfectly valid. So for instance, if I want to only stash changes in the src/ directory, I can just run

git stash push src/

Answer #4:

If you are using visual studio code there is a simpler way to stash selected files.

  1. Make sure you have installed the GitLens extension in VSCode
  2. Go to the Source Control tab
  3. Select files that you want to stash
  4. Right-click on it, and you will see many options. Click on Stash Changes
enter image description here
  1. Now it will ask you to add some stash message. Add an understandable message and hit enter.
enter image description here

Voila! You’ve successfully stashed changes in specific files in Git.

How to stash a specific file in Git?

  1. stage the changes you do NOT want to stash.
  2. stash the remaining unstaged files with:
$ git stash save <give_it_a_name> --keep-index

The unstaged files are now stashed. See the stash list with your named stash:

$ git stash list

stash@{0}: On mybranch: WIP220412-1119am
stash@{1}: On mybranch: WIP220312-749am

To restore the stashed files:

$ git stash apply stash@{<index_of_saved_stash>}
$ git stash apply stash@{0}

The changes stashed in WIP220412-1119am are now restored. And the stash list remains as well, (instead of “git stash pop”, you can retain the list this way.)


Steps to stashing only one file

Assume you have six files and all of them have been changed. Now let’s we what steps should be taken to stash only one of them.

Viewing the changed files

Firstly, run git status to see the list of the changed files:

git status

Staging files

Execute the git add command to stage all the six files:

git add .

Unstaging the file

Next step is unstaging the file3 with the help of git reset:

git reset file3

Stashing the file

Stash file3 with the git stash command to get it back to its original committed stage:

git stash --keep-index

Another way of stashing only one file is executing the following:

git stash save -p "commit message"

This method makes it possible to select which hunks should be added to the stash. Read about the descriptions of each hunk here.


The git stash command shelves change made to the working copy so you can do another work, and then return and re-apply them. The command will stash the changes that have been added to your index (staged changes) and changes made to files currently tracked by Git (unstaged change. The --keep-index option left intact all the changes that are already added to the index.

Adding Changes

The git add command adds changes in the working directory to the index. It instructs Git to add updates to a certain file in the next commit. The primary role of this command is to promote changes in the working directory to the index. The index of Git gathers all the connected changes into highly focused snapshots. Then you can commit these changes to your project history.

Hope you learned something from this post.

Follow Programming Articles for more!

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →