224

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?

SQB
  • 3,770
  • 2
  • 26
  • 45
Michael Dorst
  • 7,389
  • 11
  • 40
  • 65
  • 1
    Same question: https://stackoverflow.com/q/6315459/350384 – Mariusz Pawelski Mar 27 '20 at 17:10
  • Does this answer your question? [Git command to save a stash without modifying working tree?](https://stackoverflow.com/questions/6315459/git-command-to-save-a-stash-without-modifying-working-tree) – Mariusz Pawelski Mar 27 '20 at 17:28
  • 1
    @MariuszPawelski No, not really. That question is more specific than mine. The answer to my question was simply "no". Thanks for the link though, it may be helpful to some people, or even myself at a later time. – Michael Dorst Mar 29 '20 at 06:58
  • To be clear, my question is different because I have no requirement that the files remain untouched. I was merely looking for alternatives to `git stash && git stash apply`. You'll notice that the answers to that question are quite different from mine. – Michael Dorst Mar 29 '20 at 07:04
  • ah, right, you question is a bit less specific. But I put that question because its answers also fulfill your requirement. And that way this question appear as "Linked" in sidebar, so it might be helpful to someone. – Mariusz Pawelski Mar 29 '20 at 11:10

5 Answers5

223

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.

Cirelli94
  • 1,433
  • 16
  • 23
  • 9
    this is by far the most straightforward explanation of --keep-index I've seen. I didn't get quite the meaning by the way it was worded on the docs. – 40detectives Mar 17 '20 at 18:33
82

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.)

zrajm
  • 1,267
  • 1
  • 12
  • 21
madhead
  • 28,732
  • 15
  • 145
  • 186
  • 3
    also, what is the difference between `git stash; git stash apply` and `git stash && git stash apply`? – Michael Dorst Jul 24 '13 at 20:15
  • 17
    I believe the difference is that [`&&` runs second command only if first returned zero status code](http://linux-training.be/files/books/html/fun/ch11s04.html). – madhead Jul 24 '13 at 20:18
  • 3
    @anthropomorphic `git config --global alias.sta "!git stash && git stash apply"` should do it. –  Jul 24 '13 at 20:37
  • How can I modify this alias to use `git stash save` with an argument and _then_ do `git stash apply`? – spinningarrow Oct 09 '14 at 14:40
  • @spinningarrow try "!git stash $1 && git stash apply" – madhead Oct 09 '14 at 20:52
  • Why is there a bang (!) before the 'git' command? Bash tells me "-bash: !git: command not found. – Jay Sidri May 05 '17 at 01:48
  • 1
    @JaySidri, bang means that it is actually external command, not a git argument itself. [As per docs](https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases): "As you can tell, Git simply replaces the new command with whatever you alias it for. However, maybe you want to run an external command, rather than a Git subcommand. In that case, you start the command with a ! character." – madhead May 10 '17 at 12:20
  • not a huge fan of this since if you've already dealt with a conflict this will force you to deal with it again. – gabeio May 02 '18 at 17:08
  • As far as my requirement, this is the most straightforward method since `--keep-index` required staging changes. – Shriraj Hegde Oct 31 '21 at 10:36
13

A small enhancement in the answer which in practical may likely to use.

$ git add modified-file.txt  
(OR $ git add .    ---- for all modified file)
$ git stash save --keep-index "Your Comment"
Premchandra Singh
  • 14,026
  • 4
  • 29
  • 37
12

You can use git stash create to create a stash commit, and then save it to the stash using git stash store:

git stash store $(git stash create) -m "Stash commit message"

This can be saved to a git alias to make it more convenient:

git config --global alias.stash-keep '!git stash store $(git stash create)'

git stash-keep -m "Stash commit message"

Note that this does not do everything that git stash push does. For instance, it does not append the branch name to the commit, e.g. "stash@{0}: On myBranch: Stash commit message".

M. Justin
  • 9,474
  • 3
  • 68
  • 98
  • 1
    Love this one!! One correction though: `man git-stash` says the `-m ` has to come before the commit hash. Except something change in newest git. – tanius Jun 13 '20 at 17:33
  • This answer actually does exactly what the OP was asking! – jasongregori Jan 12 '21 at 17:35
  • I had trouble getting this alias to work, I eventually went with `save = "!f() { git stash store $(git stash create); }; f"` which worked. – jasongregori Jan 12 '21 at 17:54
4

There's a trick may help you, not stash' thing but FWIW:

git add -A
git commit -m "this is what's called stashing"       (create new stash commit)
git tag stash                               (mark the commit with 'stash' tag)
git reset HEAD~        (Now go back to where you've left with your working dir intact)

And so now you have a commit tagged stash at your disposal, it's not possible to do a git stash pop anyway but you can do things like creating patch or resetting files etc. from there, your working dir files are also left intact BTW.

KenIchi
  • 973
  • 8
  • 18