ara*_*nid 17
如果您可以说服编辑器编写要暂存的文件版本,则可以使用plumbing-level git命令将其添加到正确名称下的索引中.你需要绕过"git add",它总是将工作树中的路径X与索引中的路径X相关联(据我所知).
一旦你有了你想要写入一些临时文件$ tempfile的内容,运行git hash-object -w $tempfile- 这会将对象写入.git/objects并输出blob id.然后使用git update-index --cacheinfo 100644 $blobid $path此路径$ path 将该blob id提供给索引.
这是一个示例,它在我的仓库中对一个名为"post_load"的脚本进行更改,而不会覆盖文件本身(也证明您可以不使用临时文件):
git update-index --cacheinfo 100755 $(perl -lne 'print unless (/^#/)' post_load \
| git hash-object -w --stdin) post_load
Run Code Online (Sandbox Code Playgroud)
您没有提到您计划从哪个编辑器执行此操作,因此很难建议您如何集成它.正如我所提到的,你需要以某种方式呈现git与文件,因为你希望它被暂存(记住,git不处理存储更改).如果您可以编写一个宏来将文件保存为"$ file.tmp",那么使用类似上面的内容git update-index --cacheinfo $the_mode $(git hash-object -w $file.tmp) $file(获取$ the_mode作为练习:p),删除$ file.tmp并将编辑器缓冲区恢复$ file基本上可以满足您的要求.
例如,以下脚本有三个参数:M N path.它将在"path"更新文件的索引内容,以便将行M到N(包括)替换为来自stdin的内容:
#!/bin/sh
start_line=$1
end_line=$2
path=$3
mode=$(git ls-files -s $path | awk '{print $1}')
blob_id=$(
(
head -n $(expr $start_line - 1) $path
cat
tail -n +$(expr $end_line + 1) $path
) | git hash-object -w --stdin
)
exec git update-index --cacheinfo $mode $blob_id $path
Run Code Online (Sandbox Code Playgroud)
例如,echo "HELLO WORLD" | ./stage-part 8 10 post_load将仅用"HELLO WORLD"替换8-10中的三条线.