Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git stash uncached: how to put away all unstaged changes?

Tags:

git

git-stash

People also ask

How do I remove unstaged changes?

For all unstaged files in current working directory use: git restore . That together with git switch replaces the overloaded git checkout (see here), and thus removes the argument disambiguation. If a file has both staged and unstaged changes, only the unstaged changes shown in git diff are reverted.

How do I stash all unstaged changes in git?

If you have both staged and unstaged changes in your project, you can perform a stash on just the unstaged ones by using the -k flag. The staged changes will be left intact ready for a commit.

Does stash remove staged changes?

The answer to the question as asked ("does stash convert staged files to unstaged") is both yes and no. If you've applied with git stash apply (vs git stash pop ), you're in great shape because the stash is still present.

Does git stash save all changes?

Git stash saves the uncommitted changes locally, allowing you to make changes, switch branches, and perform other Git operations. You can then reapply the stashed changes when you need them. A stash is locally scoped and is not pushed to the remote by git push .


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

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.


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

git checkout -- . will also work instead of apply -R.

Work work work...

git apply unstaged.diff
rm unstaged.diff

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