Stash changes to specific files

You can add the files with changes you want to keep, then stash the rest of the files and clear the stash:

git add file2.cpp file2.h file3.cpp
git stash --keep-index

At this point, you’ve stashed your unwanted changes. If you’d like to permanently get rid of them, run:

git stash drop

Now you have file2.cpp, file2.h, and file3.cpp staged for commit. If you then want to stash these files (and not commit them):

git reset
git stash

Now you’ll be at your previous commit, with only those three files stashed.

Update:

Git 2.13 and later includes a more direct way to stash specific files with git stash push, as VonC explains in his answer.

Leave a Comment