3

我想修改一个(文本)文件的索引,而不必更改工作树文件状态。这可能吗?

4

3 回答 3

1

是的,您可以在 any 的 git 级别上使用 --work-tree 选项(这实际上不是真的。它应该适用于任何但有边缘情况)命令:

git show HEAD:path/to/your/file.txt > /some/other/place/file.txt
# modify the file in /some/other/place/file.txt
git --work-tree=/some/other/place add /some/other/place/file.txt
于 2011-12-20T18:42:17.103 回答
1

是的,您可以使用git update-index.

git update-index --cacheinfo 100644 <sha1-of-blob> path/in/repo

--add如果路径是分支新文件,您还需要使用。

如果您要暂存的文件是 git 存储库中尚不存在的 blob,那么您可以在 git 存储库中存储一个新的 blob git hash-object,例如:

blobid=$(command_that_creates_output | git hash-object -w --stdin)

或者

blobid=$(git hash-object -w /path/not/necessarily/in/repository)

然后,您可以按上述方式暂存 blob。

git update-index --cacheinfo 100644 blobid path/in/repo
于 2011-12-21T16:23:07.533 回答
1

“在不更改工作目录的情况下更改索引中的文件”的另一种做法是仅将补丁应用于索引。这通常是 GUI git 客户端仅暂存给定文件中选定行的方式。

您首先(如果需要)清除该文件索引中的更改:

git reset path/to/file

然后为它提取完整的补丁

git diff path/to/file > /path/to/tmpfile

编辑补丁文件以仅包含您要应用的更改,并仅应用编辑后的补丁:

git apply --cached /path/to/tmpfile

看:

git help apply
于 2012-03-28T09:51:50.187 回答