268

Suppose two set of changes are made in a project versioned by git. One set is staged and the other is not.

I would like to recheck staged changes by running my project at this state (before commit). 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 be stored somewhere for further work.

This is sounds very much like git stash command. But git stash would put both unstaged and staged changes away from my project. And I can't find something like git stash uncached.

4
  • 3
    As of today with my git 2.21, there's still not a good answer to this. All answers below are either incorrect (-k option) or cumbersome to use. Jun 30, 2019 at 19:08
  • 14
    I have a dream: git stash --unstaged (a.k.a. git stash -U).
    – Géry Ogam
    Jan 31, 2022 at 18:18
  • @Maggyero feature request?
    – Rodney
    Jan 28 at 7:42
  • @Maggyero Maybe they will. Given that the opposite --staged option or shorter git stash -S was added since 2022. Till then, the accepted answer works well.
    – joeljpa
    Feb 9 at 6:28

7 Answers 7

276

Update 2:
I'm not sure why people are complaining about this answer, it seems to be working perfectly with me, for the untracted files you can add the -u flag

The full command becomes git stash --keep-index -u

And here's a snippet from the git-stash help

If the --keep-index option is used, all changes already added to the index are left intact.

If the --include-untracked option is used, all untracked files are also stashed and then cleaned up with git clean, leaving the working directory in a very clean state. If the --all option is used instead then the ignored files are stashed and cleaned in addition to the untracked files.

And this is a gif of how it looks:

enter image description here

Update:

Even though this is the selected answer, a lot have pointed out that the [answer below](https://stackoverflow.com/a/34681302/292408) is the correct one, I recommend checking it out.

I tested my answer again today (31/1/2020) against git version 2.24.0, and I still believe that it's correct, I added a small note above about the untracked files. If you think it's not working please also mention your git version.

Old answer:
If the --keep-index option is used, all changes already added to the index are left intact:

git stash --keep-index

From the documentation of git-stash:

Testing partial commits

You can use git stash save --keep-index when you want to make two or more commits out of the changes in the work tree, and you want to test each change before committing:

# ... hack hack hack ...
$ git add --patch foo            # add just first part to the index
$ git stash save --keep-index    # save all other changes to the stash
$ edit/build/test first part
$ git commit -m 'First part'     # commit fully tested change
$ git stash pop                  # prepare to work on all other changes
# ... repeat above five steps until one commit remains ...
$ edit/build/test remaining parts
$ git commit foo -m 'Remaining parts'

But, if you just want to visually check the staged changes only, you can try difftool:

git difftool --cached
17
  • 5
    see also git stash [-p|--patch] which feels like interactive stashing. From man git stash "With --patch, you can interactively select hunks from the diff between HEAD and the working tree to be stashed."
    – here
    Aug 1, 2014 at 5:42
  • 58
    Note that this answer will also stash the changes you have staged.
    – Ben Flynn
    Jan 7, 2015 at 16:06
  • 3
    This answer isn't really useful as it will result in confusion. This answer is better stackoverflow.com/a/34681302/292408. May 29, 2018 at 20:42
  • 4
    This answer works fine for me: the only things in the resulting stash are the unstaged changes. 🤷‍♂️
    – z0r
    Apr 3, 2020 at 23:13
  • 6
    This answer works. The question was about staged vs. unstaged and does not mention anything about untracked files.
    – schminnie
    Jun 11, 2020 at 7:55
216

The accepted answer also stashes staged changes as a few have pointed out. Here's a way to do it without getting your staged changes in the stash.

The idea is to do a temporary commit of your staged changes, then stash the unstaged changes, then un-commit the temp commit:

# temp commit of your staged changes:
$ git commit --message "WIP"

# -u option so you also stash untracked files
$ git stash -u

# now un-commit your WIP commit:
$ git reset --soft HEAD^

At this point, you'll have a stash of your unstaged changes and will only have your staged changes present in your working copy.

10
  • 51
    This is really the correct answer IMO. The --keep-index option in the current accepted answer still stashes what's in the index, it just also keeps it in the index. So then it's duplicated, and hilarity ensues. Sep 16, 2016 at 19:50
  • 6
    @KenWilliams <del>hilarity</del> <ins>tragedy</ins>
    – tuespetre
    Sep 13, 2017 at 15:07
  • 2
    The git add . step might want to be improved by git add --all as that should grab the files in a directory above the current working directory too. May 29, 2018 at 20:34
  • 3
    This is the best answer so far, as the --keep-index option in the accepted answer is misleading. This should be the accepted answer. May 29, 2018 at 20:34
  • 6
    @PengheGeng you can run git commit with --no-verify to disable the commit hooks for this particular commit Jul 23, 2019 at 10:14
41

I found the marked answer did not work for me since I needed something which truly stashed only my unstaged changes. The marked answer, git stash --keep-index, stashes both the staged and unstaged changes. The --keep-index part merely leaves the index intact on the working copy as well. That works for OP, but only because he asked a slightly different question than he actually wanted the answer for.

The only true way I've found to stash unstaged changes is to not use the stash at all:

git diff >unstaged.diff
git apply -R unstaged.diff

You can also use git restore . or git checkout -- . instead of git apply -R unstaged.diff.

Work work work...

git apply unstaged.diff
rm unstaged.diff
10
  • 3
    This should be the accepted answer! It is the only one in multiple stackoverflow threads that does what it claims and not relying on making temp commits!
    – user643011
    Mar 18, 2017 at 14:43
  • 2
    @user643011: Temp commits are not a bad thing in git. They cost nothing and do not harm anyone.
    – Fritz
    Sep 5, 2017 at 9:19
  • 6
    @Fritz: No temp commits are not possible in some scenarios. It might fail if you have a pre-commit hook that checks the current working code. If your staged changes are good but your unstaged changes are not, this approach will fail to commit staged changes. Jul 18, 2018 at 22:02
  • 2
    This doesn't include untracked files. You need to use "git ls-files" to find & include those in the diff patch
    – ACyclic
    Oct 21, 2018 at 15:18
  • 2
    This definitely should be the accepted answer, better than temp commits when you are scripting a pre-commit hook. Jun 13, 2020 at 6:44
15

As of Git 2.35 (Q1 2022), you can now stash staged changes via git stash --staged.

Thus, to stash unstaged (and untracked) changes:

git stash --staged          # stashes staged, leaving unstaged
git stash -u                # stashes unstaged & untracked
git stash pop "stash@{1}"   # pops staged back (but they're now unstaged)
git add -A                  # restages the previously staged changes

HOWEVER, this would FAIL if the staged & unstaged changes affects the same lines, as git stash --staged would "partially" fail in this situation by stashing the staged changes, but leave behind both staged & unstaged.
In this case, the below method (using git commit) might be better.


If you wanna add an git alias for it, add this to your .gitconfig:

[alias]
    stash-unstaged = "!cd "${GIT_PREFIX:-.}"; f() { \
        git stash --quiet --staged;                 \
        git stash -u \"$@\";                        \
        git stash pop --quiet \"stash@{1}\";        \
        git add -A;                                 \
    }; f"

Usage:

git stash-unstaged -m "Stash Message"


For Git versions <2.35,

You can stash unstaged (and untracked) by exploiting git commit to separate the staged and unstaged changes:

git commit -m "TEMP"        # commits staged, leaving unstaged
git stash -u                # stashes unstaged & untracked
git reset --soft HEAD^      # undo commit, bringing staged back

Unlike the previous method (using git stash --staged), this will still work even when staged & unstaged changes affects the same lines.
Tho, you MIGHT need to resolve merge conflicts when u pop the stash.


FYI, to stash staged changes, simply stash the unstaged first:

# Stash unstaged as shown previously
git commit -m "TEMP"
git stash -u
git reset --soft HEAD^

git stash -u                # stashes remaining staged
git stash pop "stash@{1}"   # pops unstaged back

Alias for these 2:

[alias]
    stash-unstaged = "!cd "${GIT_PREFIX:-.}"; f() { \
        git commit --quiet -m \"TEMP\";             \
        git stash -u \"$@\";                        \
        git reset --quiet --soft HEAD^              \
    }; f"
    
    stash-staged = "!cd "${GIT_PREFIX:-.}"; f() {   \
        git stash-unstaged --quiet;                 \
        git stash -u \"$@\";                        \
        git stash pop --quiet \"stash@{1}\";        \
    }; f"
12

Since the various answers here so far seem to have their own intricacies/limitations, I wanted to provide a few more alternatives that cover all of the specific edge cases that I personally have needed.

tl;dr

List staged (without deleted) files:

git diff --staged --diff-filter=d --name-only

List unstaged (without deleted) files:

git diff --diff-filter=d --name-only

List unstaged/untracked files:

git ls-files --modified --others --exclude-standard

Stashing only staged files (originally from this StackOverflow answer, but tweaked slightly):

git stash push --include-untracked -- $(git diff --staged --diff-filter=d --name-only)

Stashing only unstaged (not untracked) files:

git stash push --keep-index -- $(git diff --diff-filter=d --name-only)

Stashing unstaged and untracked files:

git stash push --keep-index --include-untracked -- $(git ls-files --modified --others --exclude-standard)

Stashing staged/unstaged files, while also keeping the staged files in your index as well:

git stash push --keep-index

Stashing staged/unstaged/untracked files, while also keeping the staged files in your index as well:

git stash push --include-untracked --keep-index

Full Explanation

git stash push allows us to provide a <pathspec>, and only stash the files that match it:

git stash push -- <pathspec>

push [-p|--patch] [-k|--[no-]keep-index] [-u|--include-untracked] [-a|--all] [-q|--quiet] [-m|--message <message>] [--pathspec-from-file=<file> [--pathspec-file-nul]] [--] [<pathspec>…​]

Save your local modifications to a new stash entry and roll them back to HEAD (in the working tree and in the index). The part is optional and gives the description along with the stashed state.

<pathspec>…​

This option is only valid for push command.

The new stash entry records the modified states only for the files that match the pathspec. The index entries and working tree files are then rolled back to the state in HEAD only for these files, too, leaving files that do not match the pathspec intact.

-u, --include-untracked, --no-include-untracked

When used with the push and save commands, all untracked files are also stashed and then cleaned up with git clean.


git diff allows us to list the currently unstaged files with --name-only:

git diff --name-only

git diff [<options>] [--] [<path>…​]

This form is to view the changes you made relative to the index (staging area for the next commit).

--name-only

Show only names of changed files.

--diff-filter=\[(A|C|D|M|R|T|U|X|B)…​\[*\]\]

Select only files that are Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R), have their type (i.e. regular file, symlink, submodule, …​) changed (T), are Unmerged (U), are Unknown (X), or have had their pairing Broken (B). Any combination of the filter characters (including none) can be used.

Also, these upper-case letters can be downcased to exclude. E.g. --diff-filter=ad excludes added and deleted paths.


git ls-files allows us to list both the --modified files AND the untracked (--others) files:

git ls-files --modified --others --exclude-standard

git-ls-files - Show information about files in the index and the working tree

-m, --modified

Show modified files in the output

-o, --others

Show other (i.e. untracked) files in the output

--exclude-standard

Add the standard Git exclusions: .git/info/exclude, .gitignore in each directory, and the user’s global exclusion file.

3
  • 1
    Some of the higher-voted answers are simpler but none are this comprehensive. You deserve more upvotes!
    – Coderer
    Jan 25, 2022 at 9:28
  • Your solution does not seem to allow stashing unstaged changes (per file), only unstaged files, does it?
    – Géry Ogam
    Jan 31, 2022 at 18:25
  • 2
    Perfect Answer. thanks @Glen Mar 30, 2022 at 12:47
7

Git: Stash unstaged changes

This will stash all modifications that you did not git add:

git stash -k

Note that newly created (and non-added) files will remain in your working directory unless you also use the -u switch.

git stash -k -u 

Also, your working directory must be clean (i.e. all changes need to be added) when you git stash pop later on.

http://makandracards.com/makandra/853-git-stash-unstaged-changes

2
  • 21
    This is equivalent to git stash --keep-index. Staged files are included in the stash. Mar 9, 2015 at 7:41
  • This is answer is misleading, git stash --keep-index stashes all the changes from the HEAD state in the working tree, i.e. both staged and unstaged changes. The difference with bare git stash is that it also puts the working tree in index state (instead of in HEAD state), which creates a conflict when you git stash pop since the latter expect the working tree to be in the HEAD state.
    – Géry Ogam
    Jan 31, 2022 at 18:31
-1

Here is a way to make this very simple:

  1. Add the alias definitions below to your .zshrc or .bashrc or .bash_profile
  2. Now, anytime you are in this situation, just type gss and you will have 2 stashes - 1 with all the changes, another with only the staged changes

So, you can now apply the staged changes, see if those work, commit them if you want. Then, later you can also bring in the unstaged changes by applying the 'all WIP' stash and try those out.

alias gsts='git stash save'
alias gsv="git stash save --keep-index"

# How to Git Stash preserving staged/unstaged situation.
# 1. gsv will stash all and reset (remove) the unstaged, leaving only staged in staged state.
# 2. gsts will make a stash with your "good" (staged) files
alias gss='gsv all WIP && gsts staged WIP'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.