如何“git diff”工作树到储藏室?

发布于 2024-12-09 01:47:34 字数 45 浏览 0 评论 0原文

如何查看取消存储将对当前工作树造成的更改?我想知道在应用之前会进行哪些更改!

How can I see the changes un-stashing will make to the current working tree? I would like to know what changes will be made before applying them!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

养猫人 2024-12-16 01:47:34

查看最新的存储:

git stash show -p

查看任意存储:

git stash show -p stash@{1}

来自 git stash 联机帮助页:

默认情况下,该命令显示 diffstat,但它会接受任何
git diff 已知的格式(例如, git stash show -p stash@{1} 来查看
补丁形式的第二个最新藏品)。

See the most recent stash:

git stash show -p

See an arbitrary stash:

git stash show -p stash@{1}

From the git stash manpages:

By default, the command shows the diffstat, but it will accept any
format known to git diff (e.g., git stash show -p stash@{1} to view
the second most recent stash in patch form).

绳情 2024-12-16 01:47:34

要查看最新的存储:

git stash show -p

要查看任意存储:

git stash show -p stash@{1}

另外,我使用 git diff 将存储与任何分支进行比较。

您可以使用:

git diff stash@{0} master

查看与分支主服务器相比的所有更改。

或者您可以使用:

git diff --name-only stash@{0} master

轻松查找仅更改的文件名。

To see the most recent stash:

git stash show -p

To see an arbitrary stash:

git stash show -p stash@{1}

Also, I use git diff to compare the stash with any branch.

You can use:

git diff stash@{0} master

To see all changes compared to branch master.

Or You can use:

git diff --name-only stash@{0} master

To easy find only changed file names.

太阳哥哥 2024-12-16 01:47:34

如果您存储的更改所基于的分支同时发生了更改,则此命令可能很有用:

git diff stash@{0}^!

这会将存储与其所基于的提交进行比较。

If the branch that your stashed changes are based on has changed in the meantime, this command may be useful:

git diff stash@{0}^!

This compares the stash against the commit it is based on.

陌路黄昏 2024-12-16 01:47:34

根据您想要比较存储的内容(本地工作树/父提交/头提交),实际上有几个可用的命令,其中有很好的旧 git diff 和更具体的 git stash show

比较 stash 与 ↓git diffgit stash show
本地工作树git diff stash@{0}git stash show -l
父提交<代码>git diff stash@{0}^ stash@{0}git stash show -p
HEAD 提交git diff stash@{0} HEAD/

While git stash show 乍一看看起来更加用户友好,git diff 实际上更强大,因为它允许指定文件名以进行更集中的差异。我亲自在我的 zsh git 插件

Depending on what you want to compare the stash with (local working tree / parent commit / head commit), there are actually several commands available, amongst which the good old git diff, and the more specific git stash show:

Compare stash with ↓git diffgit stash show
Local working treegit diff stash@{0}git stash show -l
Parent commitgit diff stash@{0}^ stash@{0}git stash show -p
HEAD commitgit diff stash@{0} HEAD/

While git stash show looks more user friendly on first sight, git diff is actually more powerful in that it allows specifying filenames for a more focused diff. I've personally set up aliases for all of these commands in my zsh git plugin.

江南烟雨〆相思醉 2024-12-16 01:47:34

如果您的工作树是脏的,您可以通过首先提交脏工作树,然后将其与存储进行比较,将其与存储进行比较。之后,您可以使用脏工作树撤消提交(因为您可能不希望在提交日志中保留该脏提交)。

您还可以使用以下方法来相互比较两个存储(在这种情况下,您只需首先弹出其中一个存储)。

  • 提交你的脏工作树:

    <前><代码>git add .
    git commit -m“脏提交”

  • 将存储与该提交进行比较:

    git diff HEAD stash@{0}
    
  • 然后,您可以恢复提交,并将其放回工作目录中:

    git reset --soft HEAD~1
    git 重置 .
    

现在您已经将脏工作树与存储区进行了比较,并返回到最初的位置。

If your working tree is dirty, you can compare it to a stash by first committing the dirty working tree, and then comparing it to the stash. Afterwards, you may undo the commit with the dirty working tree (since you might not want to have that dirty commit in your commit log).

You can also use the following approach to compare two stashes with each other (in which case you just pop one of the stashes at first).

  • Commit your dirty working tree:

    git add .
    git commit -m "Dirty commit"
    
  • Diff the stash with that commit:

    git diff HEAD stash@{0}
    
  • Then, afterwards, you may revert the commit, and put it back in the working dir:

    git reset --soft HEAD~1
    git reset .
    

Now you've diffed the dirty working tree with your stash, and are back to where you were initially.

七月上 2024-12-16 01:47:34

@Magne 的答案是迄今为止唯一一个(很晚)回答该问题最灵活/有用的解释的答案,但是它比必要的要复杂一些。无需提交和重置,只需隐藏您的工作副本,进行比较,然后取消隐藏。

git stash save "temp"
git diff stash@{0} stash@{1}
git stash pop

通过暂时使工作文件夹更改成为存储堆栈的顶部 (stash@{0}),将原始顶部向下移动 (stash@{1}),可以显示存储堆栈顶部和工作文件夹之间的差异)然后在“新设置”位置使用原始顶部进行比较,以便您看到将其应用到当前工作顶部所产生的变化。

“但是如果我当前没有任何工作怎么办?”那么你就处于正常的无聊情况。只需使用 @Amber 的答案

git stash show

或 @czerasz 的答案

git diff stash@{0}

,或者承认存储和取消存储是快速且简单的,只需取消存储更改并检查它们即可。如果您现在不需要它们,请将它们扔掉(当前索引/工作文件夹发生变化)。完整来说就是

git stash apply
git diff
git reset
git checkout

@Magne's answer is the only one to (very late) date that answers the most flexible/useful interpretation of the question, but its a fair bit more complicated than necessary. Rather than committing and resetting, just stash your working copy, compare, then unstash.

git stash save "temp"
git diff stash@{0} stash@{1}
git stash pop

That shows you the differences between the top of the stash stack and your working folder by temporarily making your working folder changes become the top of the stash stack (stash@{0}), moving the original top down one (stash@{1}) then comparing using the original top in the 'new set' position so you see the changes that would result from applying it on top of your current work.

"But what if I don't have any current work?" Then you are in the normal boring case. Just use @Amber's answer

git stash show

or @czerasz's answer

git diff stash@{0}

or admit that stashing and unstashing is fast and easy anyway, just unstash the changes and inspect them. If you don't want them at the moment throw them (the current index/working folder changes) away. In full that's

git stash apply
git diff
git reset
git checkout
蝶…霜飞 2024-12-16 01:47:34

以防万一,要比较工作树和存储中的文件,请使用以下命令

git diff stash@{0} -- fileName (with path)

Just in case, to compare a file in the working tree and in the stash, use the below command

git diff stash@{0} -- fileName (with path)
清浅ˋ旧时光 2024-12-16 01:47:34

这对我的 git 版本 1.8.5.2 有效:

git diff stash HEAD

This works for me on git version 1.8.5.2:

git diff stash HEAD
风追烟花雨 2024-12-16 01:47:34

如果你有比较工具(比如超越比较)

git difftool stash HEAD

If you have tools for diff (like beyond compare)

git difftool stash HEAD
趴在窗边数星星i 2024-12-16 01:47:34

我相信 git diff..stash@{0} 是比较本地工作树和最新存储之间的更改的最直观方法。根据需要将 stash@{0} 替换为适用的存储编号。

请注意 git diff stash@{0} 可能会产生误导性结果。如果您的存储和当前分支的两个历史记录出现了分歧,则差异看起来就像您在存储中添加了所有新内容并删除了当前分支特有的所有内容。

答案基于 git 书

另外,请注意双点 .. 和三点 ... 指定不同的提交比较,我指的是这个答案的双点。 有关详细信息,请参阅 git 书籍

I believe git diff <current-branchname>..stash@{0} is the most intuitive way to compare the changes between the local working tree and the most recent stash. Replace stash@{0} with the applicable stash number as needed.

Beware that git diff stash@{0} may produce misleading results. If the two histories of your stash and current branch have diverged, the diff will look like you’re adding all the new stuff in your stash and removing everything unique to the current branch.

answer based on the git book

Also, note that double dot .. and triple dot ... specify different commit comparisons, and I am referring to the double dot for this answer. See the git book for details

弥繁 2024-12-16 01:47:34

我发现这里的答案都没有给出我的用例所需的内容:

做了一些更改,将它们隐藏起来,想检查发生了什么变化
不应用隐藏。

我发现工作树和存储之间的最好差异是:

git diff stash@{0}

或者简单地说:

git diff stash

但这显示了差异与我预期的方式相反:添加的行(通常为绿色)显示为已删除(通常为红色),反之亦然。

反转 diff 得到了我需要的东西:

git diff stash -R

此时,在存储之后运行此命令会得到与在存储之前运行 git diff 相同的结果。

I found that none of the answers here gave me quite what I needed for my use case:

Made some changes, stashed them, wanted to check what had changed
without applying the stash.

The best I found for a diff between the working tree and the stash was:

git diff stash@{0}

or simply:

git diff stash

But this showed the diff the wrong way round for how I expected it: Lines added (usually in green) were shown as removed (usually red), and vice versa.

Reversing the diff got me what I needed:

git diff stash -R

At this point you get identical results running this command after stashing as you would running git diff before the stash.

伴我心暖 2024-12-16 01:47:34

在不移动任何内容的情况下执行此操作的一种方法是利用 patch 可以读取 git diff 的事实(基本上是统一的差异)

git stash show -p | patch -p1 --verbose --dry-run

这将向您展示补丁通常会执行的操作的逐步预览。这样做的额外好处是,补丁也不会阻止自己将补丁写入工作树,如果由于某种原因你真的需要 git 停止修改前的提交,请继续并删除 --dry-运行并按照详细说明进行操作。

One way to do this without moving anything is to take advantage of the fact that patch can read git diff's (unified diffs basically)

git stash show -p | patch -p1 --verbose --dry-run

This will show you a step-by-step preview of what patch would ordinarily do. The added benefit to this is that patch won't prevent itself from writing the patch to the working tree either, if for some reason you just really need git to shut up about commiting-before-modifying, go ahead and remove --dry-run and follow the verbose instructions.

你对谁都笑 2024-12-16 01:47:34

她是存储列表

git stash list 
stash@{0}: WIP on feature/blabla: 830335224fa Name Commit
stash@{1}: WIP on feature/blabla2: 830335224fa Name Commit 2

所以获取存储编号并执行以下操作:

您可以这样做:

 git stash show -p stash@{1}

但是如果您想要一个差异(这与显示存储不同,这就是我写这个答案的原因。Diff考虑分支中的当前代码,show仅显示您将应用的内容

您可以使用:

git diff stash@{0}

git diff stash@{0} <branch name>

另一个有趣的事情是:

git stash apply
git stash apply stash@{10}

这会应用存储而不将其从列表中删除,你可以git checkout。删除这些更改,或者如果您愿意 git stash drop stash@{10} 从列表中删除存储。

从这里开始,我从不建议使用 git stash pop 并使用 git stash apply 和 git stash drop 的组合。错误的分支...有时很难恢复你的代码。

She the list of stash

git stash list 
stash@{0}: WIP on feature/blabla: 830335224fa Name Commit
stash@{1}: WIP on feature/blabla2: 830335224fa Name Commit 2

So get the stash number and do:

You can do:

 git stash show -p stash@{1}

But if you want a diff (this is different to show the stash, that's why I write this answer. Diff consider the current code in your branch and show just show what you will apply)

You can use:

git diff stash@{0}

or

git diff stash@{0} <branch name>

Another interesting thing to do is:

git stash apply
git stash apply stash@{10}

This applies the stash without removing it from the list, you can git checkout . to remove those change or if you are happy git stash drop stash@{10} to remove a stash from the list.

From here I never recommend to use git stash pop and use a combination of git stash apply and git stash drop If you apply a stash in the wrong branch... well sometimes is difficult to recover your code.

单身狗的梦 2024-12-16 01:47:34

结合我在这个帖子和这个帖子中学到的知识,当我想要看到“存储中的内容”,我首先运行:

git stash show stash@{0}

这将显示修改了哪些文件。然后,为了在 difftool 中获得良好的视觉差异,我这样做:

git difftool --dir-diff stash@{0} stash@{0}^

这将立即显示给定存储与其父存储的所有差异。

您可以在 ~/.gitconfig 中配置 diff 工具,例如使用 Meld

...
[diff]
    tool = meld

Combining what I learned in this thread and in this one, when I want to see "what is inside the stash", I first run:

git stash show stash@{0}

That will show what files were modified. Then, to get a nice visual diff in a difftool, I do:

git difftool --dir-diff stash@{0} stash@{0}^

This will display all the differences at once of the given stash against its parent.

You can configure the diff tool in ~/.gitconfig, e.g. with Meld:

...
[diff]
    tool = meld
赴月观长安 2024-12-16 01:47:34

FWIW
这对于所有其他答案来说可能有点多余,并且与已接受的答案非常相似;但也许它会对某人有所帮助。

git stash show --help 将为您提供您应该需要的一切;包括隐藏的演出信息。

显示[<隐藏>]

<块引用>

将存储中记录的更改显示为存储状态与其原始父级之间的差异。如果没有给出,则显示最新的。默认情况下,该命令显示 diffstat,但它将接受 git diff 已知的任何格式(例如, git stash show -p stash@{1} 以补丁形式查看第二个最新存储)。您可以使用 stash.showStat 和/或 stash.showPatch 配置变量来更改默认行为。

FWIW
This may be a bit redundant to all the other answers and is very similar to the accepted answer which is spot on; but maybe it will help someone out.

git stash show --help will give you all you should need; including stash show info.

show [<stash>]

Show the changes recorded in the stash as a diff between the stashed state and its original parent. When no is given, shows the latest one. By default, the command shows the diffstat, but it will accept any format known to git diff (e.g., git stash show -p stash@{1} to view the second most recent stash in patch form). You can use stash.showStat and/or stash.showPatch config variables to change the default behavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文