git 可以用 stash 做工作副本的差异吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8058611/
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
Can git do a diff of the working copy with stash
提问by Laughing monster
How can I diff my current working copy against a stash?
如何将我当前的工作副本与存储区分开?
My use case: my working copy already contains a subset of the changes in my stash@{0}
, but I don't want to apply allthe changes in stash@{0}
. I want to do a diff to help determine which desirable changes in stash@{0}
are still missing from my working copy.
我用例:我的工作拷贝已经包含在我的变化的一个子集stash@{0}
,但我并不想申请所有的变化stash@{0}
。我想做一个差异来帮助确定stash@{0}
我的工作副本中仍然缺少哪些理想的更改。
回答by Andy
If it was your most recent stash, git diff stash@{0}
will do it. If not, you can use git stash list
to get the index of which stash you want to compare to.
如果这是你最近的藏品,git diff stash@{0}
就会这样做。如果没有,您可以使用git stash list
来获取要比较的存储的索引。
To see the difference between the actual working copy you would need to stash it first. You could then pop it back off.
要查看实际工作副本之间的差异,您需要先将其隐藏起来。然后你可以把它弹回去。
git add -A <- Add all the files
git commit -m "temp" <- commit them
git diff stash@{0}..HEAD <- diff the commit with your stash
git reset HEAD~ <- roll your commit back
回答by Magne
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 wish to undo the commit of your working tree (to keep your history clean).
如果您的工作树是脏的,您可以通过首先提交脏工作树,然后将其与存储进行比较,将其与存储进行比较。之后,您可能希望撤消您的工作树的提交(以保持您的历史记录干净)。
Commit your dirty working tree:
git add . git commit -m "Dirty commit"
Diff the stash with that commit:
git diff stash@{0}
Then, afterwards, you may revert the commit, and put it back in the working dir:
git reset --soft HEAD~1 git reset .
提交你肮脏的工作树:
git add . git commit -m "Dirty commit"
使用该提交区分存储:
git diff stash@{0}
然后,之后,您可以恢复提交,并将其放回工作目录:
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.
现在你已经用你的藏品区分了肮脏的工作树,并回到了你最初的地方。
回答by niksvp
I think OP wanted the answer to see what difference the stash contains other than the files present in the working copy.
我认为 OP 希望得到答案,看看除了工作副本中存在的文件之外,stash 还包含哪些不同之处。
The following command shows what stash contains.
以下命令显示了 stash 包含的内容。
git stash show -p stash@{0}
回答by Max Wallace
You can't do this without either commiting or stashing your working copy. Stashing is probably lower-risk:
如果不提交或隐藏您的工作副本,您就无法做到这一点。藏匿可能风险较低:
git add -A # Stage all changes in your working copy.
git stash save --keep-index # Stash your working copy and leave it intact
git diff stash@{0} stash@{1}
To get rid of the stash you just made for the sake of diffing, use git stash pop
or git stash drop stash@{0}
.
为了摆脱你只是为了差异而制作的藏匿处,使用git stash pop
或git stash drop stash@{0}
。
If you want to use a commit instead, see @Magne's answer.
如果您想改用提交,请参阅@Magne 的回答。
回答by MOHRE
You can use git stash show -l
. It shows what you want without extra stash or commit.
您可以使用git stash show -l
. 它无需额外的存储或提交即可显示您想要的内容。
回答by Petros Mitakos
You can use the Unix diffcommand in conjunction with two Git commands just like so:
您可以将 Unix diff命令与两个 Git 命令结合使用,如下所示:
diff <(git diff) <(git stash show -p stash@{0})
Of course you can change the stash@{0} to whatever stash you want. This command is actually comparing the actual working directory (git diff) with one of your stashes.
当然,您可以将 stash@{0} 更改为您想要的任何 stash。此命令实际上是将实际工作目录 (git diff) 与您的其中一个存储进行比较。
UPDATE
更新
You can also install colordiffand see a much much better result:
您还可以安装colordiff并看到更好的结果:
Installation:
安装:
- Ubuntu/Debian: sudo apt-get install colordiff
- OS X: brew install colordiff
- Ubuntu/Debian: sudo apt-get install colordiff
- OS X: brew install colordiff
and then just change command to :
然后只需将命令更改为:
colordiff <(git diff) <(git stash show -p stash@{0})
回答by alpha_989
To do a diff of a file or folder present in the working directory with that in a stash you can do:
要将工作目录中存在的文件或文件夹与存储中的文件或文件夹进行比较,您可以执行以下操作:
git diff stash@{0} -- /path/to/fileorfolder
git diff stash@{0} -- /path/to/fileorfolder
Here stash@{0}
just represents a commit HASH, so it works exactly as git diff
works. stash@{0}
is just a handler.
这里stash@{0}
只代表一个提交 HASH,所以它的工作原理完全一样git diff
。stash@{0}
只是一个处理程序。
回答by o.v
回答by Wayne Walker
Not sure what the OP wanted, but I use this to see what the actual diff set of the stash is:
不确定 OP 想要什么,但我用它来查看存储的实际差异集是什么:
git difftool stash@{0}^!
git difftool stash@{0}^!
or
或者
git diff stash@{0}^!
git diff stash@{0}^!
or
或者
git diff stash@{0}^! > patchfile
git diff stash@{0}^! > patchfile
回答by Emran Bader
Yes you can use git stash show -p
,
check this Git diff against a stashfor more information.
是的,您可以使用git stash show -p
,检查此 Git diff 与 stash以获取更多信息。