2

git diff命令(带有--no-index标志)非常适合区分两个文件,无论这两个文件是否包含在 Git 存储库中。

例子

$ git diff --no-index -- filea fileb
// shows diff between filea and fileb

问题

git add如果它们不是 Git 存储库的一部分,是否也可以使用 Git 选择性地将内容从一个文件“合并”(即)到另一个文件?基本上像 agit add -p但不是从工作目录到索引,而是从filebfilea.

我觉得所有基本构建块(管道命令)都应该可用。可能涉及format-patch和/或apply

$ <command> filea fileb
// wanted: interactively choose patches/hunks from fileb and apply them to filea
4

2 回答 2

2

git diff --no-index is pretty much just an ordinary diff, if you want to apply change hunks across arbitrary files, use an ordinary merge tool like vimdiff -- which is even harder to distinguish from vimdiff used as a Git mergetool because it's the same exact thing.

git add is for adding content to the repo, you're not working on two paths from the work tree, you're working on a single path from the work tree and whatever content is indexed at that path. In particular, you don't get the option to have two different names.

But Git itself finds its work tree (and repo, and objects, and index…) only if you don't explicitly tell it where they are. If you've got a snapshot of filea somewhere, say at /path/to/filea, you can

git --work-tree /path/to add -p filea`

and git will obligingly do a patch-add from the filea at that path to the filea content registered in the index.

于 2020-02-15T23:25:42.403 回答
1

git add -p命令是——或者至少1——一个大的 Perl 脚本。要查看它,请找到“核心”目录:

git --exec-path

这将打印类似的东西/usr/lib/git-coreor /usr/local/libexec/git-core。这是各种 Git 内部二进制文件和脚本所在的位置。

在打印出的任何目录中,查找git-add--interactive. 这是或曾经是git add -p代码。

这个 Perl 程序运行 Git diff 和 apply 命令,并且应该相对容易修改以使其执行您想要的操作。请注意,这用于所有git add -pgit reset -pgit checkout -p,这就是它有近 2000 行长的原因。


Git 项目人员正在重写所有这些,以使您无法修改。:-) 呃,也就是说,让它用 C 写得快——你仍然有源代码,仍然可以随意修改它,但是作为一个 C 程序,它会更难处理。

于 2020-02-15T22:36:39.167 回答