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.