Stashing only un-staged changes in Git

git stash no local changes to save
git stash pop specific
git stash apply specific file
git stash only saved changes
git add staged to stash
vscode stash staged changes
git stash only index
how to push only staged changes

I'd like to do the following work flow:

  1. Add changes to the stage.
  2. Stash all the other changes that were not staged.
  3. Do some stuff with the things in stage (i.e. build, run tests, etc)
  4. Apply the stash.

Is there a way to do step 2?

Example

 echo "123" > foo
 git add foo # Assumes this is a git directory
 echo "456" >> foo
 git stash
 cat foo # Should yield 123

git stash save has an option --keep-index that does exactly what you need.

So, run git stash save --keep-index.

Git: Stash unstaged changes, What is a simple way to put away all unstaged changes and leave only staged? So I need unstaged changes to disappear from my project, but to  when you reapply the first stash and get all changed back while you might be only interested in your unstages changes use git stash apply --index option. This will try to keep your un(staged) state. This will try to keep your un(staged) state.

This may be done in 3 steps: save staged changes, stash everything else, restore index with staged changes. Which is basically:

git commit -m 'Save index'
git stash push -u -m 'Unstaged changes and untracked files'
git reset --soft HEAD^

This will do exactly what you want.

Git stash uncached: how to put away all unstaged changes?, 1 Answer. Stage all your files you need to stash. Run. This command will create a stash with all of your changes (staged and unstaged), but will leave the staged changes in your working directory (still in state staged). Run. Now your "good stash" has only staged files. Now if you need unstaged files before stash, The git stash command takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy.

git stash save --keep-index

Also, Re:

Why not commit your changes after staging them? – Shin

A: Because you should always checkin tested code :) That means, you need to run the tests with only the changes you are about to commit

All this apart from the fact that of course, as an experienced programmer, you have the innate urge to test and review just those changes -- only partly kidding

Stashing only staged changes in git - is it possible?, (both staged and unstaged), saves them away for later use, and then reverts them from your working copy. git stash --keep-index This command will create a stash with all of your changes (staged and unstaged), but will leave the staged changes in your working directory (still in state staged).

With git version 2.7.4 you may do:

git stash save --patch

The git will ask you to add or not your changes into stash. And you then just answer y or n

You can restore working directory as you always do that:

git stash pop

or, if you want to keep saved changes in stash:

git stash apply

How do you stash an untracked file?, repo, so git pull will work without any problems. When you effectively stash your unstaged changes using $ git stash save --keep-index. you might wish to give the stash a message, so that when you to do a git stash list it's more obvious what you have stashed before, especially if you follow that stash operation by further saves. For example $ git stash save --keep-index "changes not yet staged"

Extending previous answers, I sometimes have a complex set of changes staged, but wish to commit a separate change first. For example, I might have spotted a bug or otherwise incorrect code that I'd like to fix ahead of my staged changes. One possible route to take is this:

first stash everything, but leave the staged changes intact

$ git stash save --keep-index [--include-untracked]

now stash the staged changes separately too

$ git stash save

make changes for fix; and test; commit them:

$ git add [--interactive] [--patch]

$ git commit -m"fix..."

now restore the previously staged changes:

$ git stash pop

resolve any conflicts, and note that if there were conflicts, git will have applied but not dropped that top stash entry.

(... Then commit the staged changes, and restore the stash of all the other changes, and continue ...)

git stash, The git stash command takes your uncommitted changes (both staged and to stash just a single file, a collection of files, or individual changes from within files. Sometimes I only want to stash part of my set of files. It'd be great to be able to stash a set of files, or just the unstaged ones or something. I've seen other git clients like SourceTree do this as well.

Stashing only staged changes in git - is it possible?, to unstage) modified: index.html Changes not staged for commit: (use "git add You can reapply the one you just stashed by using the command shown in the  Git: Stash unstaged changes. This will stash all modifications that you did not git add: Note that newly created (and non-added) files will remain in your working directory unless you also use the -u switch. Also, your working directory must be clean (i.e. all changes need to be added) when you git stash pop later on.

Git Pull While Ignoring Local Changes?, to unstage) modified: index.html Changes not staged for commit: (use "git add <​file>. By default, git stash will stash only modified and staged tracked files. A script to `git stash` only the currently staged changes. - stashstaged.sh

Stashing and Cleaning, Also see the git-stash(1) manpage. z ( magit-stash ) Create a stash of unstaged changes in the working tree. Untracked Create a snapshot of the index only. The answer to this issue is the git stash command. Stashing takes the dirty state of your working directory — that is, your modified tracked files and staged changes — and saves it on a stack of unfinished changes that you can reapply at any time (even on a different branch).

Comments
  • Why not commit your changes after staging them?
  • IIRC --keepindex does exactly that
  • Because if, say, the build fails I don't want to have a commit of this. I know I can delete the commit but I'd like to do this without a commit if possible.
  • Sehe, thanks. I can confirm this works. Gee, I looked at the manual at linux.die.net/man/1/git-stash which is out of date. man git stash is much better.
  • it's --keep-index, fwiw.
  • True. I keep using save with git stash. Maybe it is the programmer in me insisting on honoring the symmetry with apply/pop. :)
  • Note: this still stashes all your changes; the only difference from regular git stash save is that it leaves the already-staged changes in your working copy as well. In the workflow above this would work fine since you're just applying the stash on top of a local copy that already has half of the stash's changes (which git is smart enough to ignore). But if you edit the code before re-applying the stash, you could potentially see merge conflicts when you go to apply. Fyi.
  • @ytpete That has bitten me so many times. I really wish there was a way for git to only stash the things you are not keeping... I often commit stuff, then do a full git stash, knowing that I can git commit --ammend if there are problems in what I committed.
  • --amend (rather than --ammend)
  • This solution does not work for me because of the problems described by peterflynn. It is not a good answer to the question since it still stashes the staged changes. Anybody got a better solution?
  • Note: -u also stashes untracked files.