If I run git stash -u
, I can stash untracked files. However, said untracked files don't show up at all with git stash show stash@{0}
. Is there any way to show untracked stashed files without applying the stash?
相关问题
- Why does recursive submodule update from github fa
- Extended message for commit via Visual Studio Code
- Emacs shell: save commit message
- Can I organize Git submodules in a flat hierarchy?
- Upload file > 25 MB on Github
相关文章
- 请教Git如何克隆本地库?
- GitHub:Enterprise post-receive hook
- Git Clone Fails: Server Certificate Verification F
- SSIS solution on GIT?
- Is there a version control system abstraction for
- ssh: Could not resolve hostname git: Name or servi
- Cannot commit changes with gitextensions
- git: retry if http request failed
Untracked files are stored in the third parent of a stash commit. (This isn't actually documented, but is pretty obvious from The commit which introduced the -u feature, 787513..., and the way the rest of the documentation for
git-stash
phrases things... or just by doinggit log --graph stash@{0}
)You can view just the "untracked" portion of the stash via:
or, just the "untracked" tree itself, via:
or, a particular "untracked" file in the tree, via:
There is, unfortunately, no good way to get a summary of the differences between all staged+unstaged+untracked vs "current" state. ie:
git show stash@{0}
cannot be made to include the untracked files. This is because the tree object of the stash commit itself, referred to asstash@{0}:
, does not include any changes from the third, "unstaged" parent.This is due to the way stashes are re-applied: tracked files can be easily applied as patches, whereas untracked files can only be applied, in theory, as "whole files".
A workaround: Staging files before stashing them will make
git stash show -p
work as expected.git add .
git stash save
Note: This way gives the power adding interactive portions too, here is how.
Caution: Ensure you don't have previously staged work, or you won't be able to distinguish it.
This may be of use.
You can list all stash commits with the following command:
Since stashes are represented as a 3-way merge commit of HEAD, the index, and a parent-less "root" commit of untracked files, untracked file stashes can be listed by piping the above output into the following:
Useful applications of the above:
Show only untracked, stashed files
Of course, remove the
--stat
to see the contents of the files.Find a specific file
Grep untracked files
List all contents of all stashes
To list the untracked files in the stash:
git ls-tree -r stash@{0}^3 --name-only
To show a complete diff of all untracked files (with content):
git show stash@{0}^3
These commands read the last (most recent) stash. For earlier stashes, increment the number behind the "stash@", for example
stash@{2}
for the second from the last stash.The reason this works is that
git stash
creates a merge commit for each stash, which can be referenced asstash@{0}
,stash@{1}
etc. The first parent of this commit is the HEAD at the time of the stash, the second parent contains the changes to tracked files, and the third (which may not exist) the changes to untracked files.This is partly explained in the manpage under "Discussion".
To see all the files in the stash (both tracked and untracked), I added this alias to my config:
showstash = "!if test -z $1; then set -- 0; fi; git show --stat stash@{$1} && git show --stat stash@{$1}^3 2>/dev/null || echo No untracked files -"
It takes a single argument of which stash you want to view. Note it will still present it in two back-to-back lists.
The
if...fi
section changes the bash argument $1 to 0 if none was passed.