在 git 中,有没有办法在不应用存储的情况下显示未跟踪的隐藏文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12681529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In git, is there a way to show untracked stashed files without applying the stash?
提问by Max Nanasy
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?
如果我运行git stash -u
,我可以隐藏未跟踪的文件。但是,所述未跟踪的文件根本不显示git stash show stash@{0}
. 有没有办法在不应用存储的情况下显示未跟踪的隐藏文件?
回答by Will Palmer
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 doing git log --graph stash@{0}
)
未跟踪的文件存储在存储提交的第三个父级中。(这实际上并未记录在案,但从引入 -u 功能的提交中很明显,787513...,以及短语的其余文档的git-stash
方式...或者只是通过这样做git log --graph stash@{0}
)
You can view just the "untracked" portion of the stash via:
您可以通过以下方式查看存储的“未跟踪”部分:
git show stash@{0}^3
or, just the "untracked" tree itself, via:
或者,只是“未跟踪”树本身,通过:
git show stash@{0}^3:
or, a particular "untracked" file in the tree, via:
或者,树中的特定“未跟踪”文件,通过:
git show stash@{0}^3:<path/to/file>
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 as stash@{0}:
, does not include any changes from the third, "unstaged" parent.
不幸的是,没有好的方法可以总结所有已暂存+未暂存+未跟踪与“当前”状态之间的差异。即:git show stash@{0}
不能包含未跟踪的文件。这是因为存储提交本身的树对象(称为 )stash@{0}:
不包括来自第三个“未暂存”父项的任何更改。
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".
这是由于重新应用隐藏的方式:跟踪的文件可以轻松地作为补丁应用,而未跟踪的文件在理论上只能作为“整个文件”应用。
回答by Steve
You can list all stash commits with the following command:
您可以使用以下命令列出所有存储提交:
git rev-list -g stash
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:
由于隐藏文件表示为 HEAD、索引和未跟踪文件的无父级“根”提交的 3 向合并提交,因此可以通过将上述输出管道传输到以下内容来列出未跟踪文件隐藏文件:
git rev-list -g stash | git rev-list --stdin --max-parents=0
Useful applications of the above:
以上的有用应用:
Show only untracked, stashed files
仅显示未跟踪的、隐藏的文件
git rev-list -g stash | git rev-list --stdin --max-parents=0 | xargs git show --stat
Of course, remove the --stat
to see the contents of the files.
当然,删除--stat
以查看文件的内容。
Find a specific file
查找特定文件
git rev-list -g stash | xargs -n1 git ls-tree -r | sort -u | grep <pattern>
Grep untracked files
Grep 未跟踪文件
git rev-list -g stash | git rev-list --stdin --max-parents=0 | xargs git grep <pattern>
List all contents of all stashes
列出所有藏匿处的所有内容
git rev-list -g stash | git rev-list --stdin | xargs git show --stat
回答by wisbucky
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.
这些命令读取最后一个(最近的)存储。对于较早的 stash,增加“stash@”后面的数字,例如stash@{2}
从最后一个 stash 开始的第二个。
The reason this works is that git stash
creates a merge commit for each stash, which can be referenced as stash@{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.
之所以这样的作品是git stash
创建一个合并提交每个藏匿,它可以作为引用stash@{0}
,stash@{1}
等等。这样做的第一个父提交是藏匿的时间HEAD,第二父包含更改跟踪文件,以及第三(可能不存在)对未跟踪文件的更改。
This is partly explained in the manpage under "Discussion".
回答by Randall
To see all the files in the stash (both tracked and untracked), I added this alias to my config:
要查看存储中的所有文件(已跟踪和未跟踪),我将此别名添加到我的配置中:
showstash = "!if test -z ; then set -- 0; fi; git show --stat stash@{} && git show --stat stash@{}^3 2>/dev/null || echo No untracked files -"
It takes a single argumentof 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.
如果没有传递,该if...fi
部分会将 bash 参数$1更改为 0。
回答by weshouman
A workaround: Staging files before stashing them will make git stash show -p
work as expected.
解决方法:在存储文件之前暂存文件git stash show -p
将按预期工作。
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.
注意:这种方式也提供了添加交互部分的能力,这里是.
注意:确保您之前没有上演过的工作,否则您将无法区分它。
这可能有用。