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
.
Update 2:
I'm not sure why people are complaining about this answer, it seems to be working perfectly with me.
To stash only unstaged changes:
git stash --keep-index
# or shorter:
git stash -k
For untracked 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:
Update:
Even though this is the selected answer, a lot have pointed out that the answer below 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