How to stash a single file in Git

Sometimes you may be working on your Git repo and discover that you have made changes in several files and need to stash changes to only some of those modified files. How do you accomplish that? Well, there’s a handy command that goes as follows:

git stash -p

This will go through each modified file and give you an option to stash the hunk by entering ‘y’, not stash the hunk by entering ‘n’, or perform other actions. A full list, found on Stack Overflow is shown below:

   y - stash this hunk
   n - do not stash this hunk
   q - quit; do not stash this hunk or any of the remaining ones
   a - stash this hunk and all later hunks in the file
   d - do not stash this hunk or any of the later hunks in the file
   g - select a hunk to go to
   / - search for a hunk matching the given regex
   j - leave this hunk undecided, see next undecided hunk
   J - leave this hunk undecided, see next hunk
   k - leave this hunk undecided, see previous undecided hunk
   K - leave this hunk undecided, see previous hunk
   s - split the current hunk into smaller hunks
   e - manually edit the current hunk
   ? - print help

Leave a Reply

Your email address will not be published. Required fields are marked *