How Can I Git Stash a Specific File?

Better Stack Team
Updated on June 24, 2024

To stash a specific file in Git, you can use the git stash push command with the path to the file you want to stash. Here's how to do it:

 
git stash push -- <path-to-file>

Replace <path-to-file> with the path to the file you want to stash.

For example, if you want to stash changes to a file named example.txt, you would run:

 
git stash push -- example.txt

This command stashes the changes to the specified file, removing them from the working directory and staging area, but keeping them in the stash for later retrieval.

Note:

  • Stashing a specific file is useful when you only want to temporarily remove changes to that file from your working directory without stashing changes to other files.
  • You can stash multiple files by providing multiple <path-to-file> arguments.
  • To retrieve the stashed changes later, you can use git stash apply or git stash pop.
  • Stashed changes can also be referred to using a stash index, which you can find using git stash list.
Got an article suggestion? Let us know
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github