git stash and git stash apply without clearing working directory momentarily

Viewed 46

Many times I need to take a snapshot of the current working directory as a backup before making experimental changes. Here is what I do.

git stash
git stash apply

But I used some applications that listen for disk changes and they cause unwanted behavior. I want to do the same without cleaning the local working directory in the first step.

It can be a line of code or a multi-line string as I will just create a function for it.

Bonus: Nice if it preserves the current staging/non-staging area too.

1 Answers

Try :

git stash store $(git stash create)

Under the hood git stash [push] does :

  1. create a commit which stores the content of the index + worktree,
  2. store that commit in a ref named refs/stash (previous stashes are actually the reflog of this ref),
  3. clean up the files on disk.

If you look at the docs, there are other subcommands to git stash :

  • create creates a commit as in step 1, and outputs its sha on stdout
  • store does step 2

(step 3 is git reset --hard HEAD)

Related