Stash changes while keeping the changes in the working directory in Git
Is there a git stash
command that stashes your changes, but keeps them in the working directory too? So basically a git stash; git stash apply
in one step?
Solution 1:
For what it's worth, another way to do this is to stage the changes you want to keep, and then stash everything using --keep-index
:
$ git add modified-file.txt
$ git stash push --keep-index
The commands above will stash everything, but it will leave the files staged in your working directory.
From the official Linux Kernel Git documentation for git stash
or from git-scm:
If the
--keep-index
option is used, all changes already added to the index are left intact.
Solution 2:
git stash
and then git stash apply
(git stash && git stash apply
) will stash files and apply stash immediately after it. So after all you will have your changes in stash and in working dir.
You can create an alias if you want it in one piece. Just put something like this to ~/.gitconfig
:
[alias]
sta = "!git stash && git stash apply"
The drawback of this approach is that all files are stashed and recreated. This means that timestamps on the files in question will be changed. (Causing Emacs to complain when I try to save the file if opened it before I did the git sta
, and may cause unnecessary rebuilds if you're using make
or friends.)