How Do I Stash Only One File Out of Multiple Files That Have Changed?

Better Stack Team
Updated on June 24, 2024

To stash only one file out of multiple files that have changed, you can use the git stash push command with the -p or --patch option. This option allows you to interactively select which changes you want to stash. Here's how you can do it:

 
git stash push -m "Your stash message" -p <path/to/file>

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

After running this command, Git will prompt you with a series of options for each change in the specified file. You can choose which changes you want to stash by typing y for yes, n for no, d to not stash the remaining changes in the file, or q to quit the interactive mode.

Once you've selected the changes you want to stash, Git will create a stash with only the selected changes from the specified file.

Note:

  • If you're unsure about the changes and want to see a diff before making a decision, you can type ? for help, and Git will display a diff for the current change.
  • This method allows you to stash changes selectively from a single file while keeping other changes in the working directory untouched.
  • After stashing the changes, you can later apply them using git stash apply or git stash pop.
Got an article suggestion? Let us know
Explore more
Git
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