gpt4 book ai didi

git - 获取本地更改文件和不同分支之间的 git 差异

转载 作者:太空狗 更新时间:2023-10-29 14:36:25 28 4
gpt4 key购买 nike

我对本地文件做了一些修改,但还没有提交。我想对当前版本与来自不同分支的版本进行 git diff,但是当我这样做时:

git diff master otherbranch myfile.txt

看起来它显示的是当前分支中 myfile.txt 的最后一次提交与当前版本之间的差异,而不是其他分支与当前版本之间的差异。

最佳答案

你非常接近!

git diff master otherbranch myfile.txt

这与 git diff master otherbranch -- myfile.txt 的“含义”相同,正如我们在 the git diff documentation 中看到的那样, 具有一般形式:

git diff <commit> <commit> -- <path>

This is to view the changes between two arbitrary <commit>.

想要的就在上面:

This form is to view the changes you have in your working tree relative to the named <commit>. You can use HEAD to compare it with the latest commit, or a branch name to compare with the tip of a different branch.

在手册页中进一步查找一行,您将看到:

git diff [--options] <commit> [--] [<path>...]

这将是:

git diff otherbranch -- myfile.txt

使用 -- 的原因是为了防止你需要 diff 一个文件,例如 --name-only-w 或类似的。养成一直使用它的习惯是个好主意,这对 git diff 来说并不重要,但有一天如果你不小心创建了一个名为 -rf 的文件, 1 然后运行git rm -- -rf,你会发现这是一个很好的习惯。 :-)

阅读和研究手册页是个好主意,因为 很多 git diff 的形式——命名没有提交,命名 1 个提交,或者命名2 次提交;使用或省略 --staged——所有这些都有不同的区别。我花了大约一年的时间才知道要使用哪个,而无需仔细检查手册。


1确实,这种情况有时会发生,尤其是当同事决定恶作剧并运行 : > ./-rf 时。另一种处理方法是使用 ./-rf,就像恶作剧一样,因为 ./-rf 不是以 - 开头的.但这只是一个好习惯。

关于git - 获取本地更改文件和不同分支之间的 git 差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42822095/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com