我如何 git 将本地更改区分到不同的分支?

How can I git diff local changes to a different branch?

我有一个存储库签出到一些(未知)标签(当前不在任何分支上。)并且一些本地更改未签入索引。当我 运行 git diff 文件时,它给了我一些更改,我不确定它们是哪个提交。我想让差异到达给定分支的顶部。是否有 git diff 命令?类似于:

git diff myLocalFile myRemoteBranch

您可以通过在命令末尾使用 -- 来指定带有 diff 的特定文件。

此外,您可以使用 HEAD 指针来指定您当前所在的位置,而不必指定您所在的标签。

示例:

git diff HEAD..myRemoteBranch -- relative/path/to/myLocalFile

或者,更简单地说:

git diff myRemoteBranch -- relative/path/to/myLocalFile

此外,如果您真的想与 "remote" 分支进行比较,您应该使用 remote 引用该分支,例如:

git fetch # retrieve latest from server
git diff origin/myRemoteBranch -- relative/path/to/myLocalFile

Git 内置了一个工具:

git diff-index myRemoteBranch myLocalFile

它默认以“原始”格式输出,使用-p获取补丁就像git diff默认一样:

git diff-index -p myRemoteBranch myLocalFile