Git:通过行号将索引*的部分(更改)文件添加到索引* [英] Git: Add part of (changed) file to index *by line numbers*

查看:135
本文介绍了Git:通过行号将索引*的部分(更改)文件添加到索引*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有git命令在索引的行范围内添加差异?



我希望能够在我的编辑器中选择行,然后运行一个宏来将选择中的所有更改添加到索引中。 解决方案

如果你能说服你编辑器来编写您想要演出的文件的版本,您可以使用管道级别的git命令以正确的名称将其添加到索引中。你需要绕过git add,它始终将工作树中的路径X与索引中的路径X相关联(据我所知)。



将你想要的内容写入临时文件$ tempfile,运行 git hash-object -w $ tempfile - 这会将对象写入.git / objects并输出blob id。然后使用 git update-index --cacheinfo 100644 $ blobid $ path 将此blob id提供给索引,以将路径$ path与该对象关联。



下面是一个例子,在我的repo中对一个名为post_load的脚本进行了改变,而不覆盖文件本身(也证明你可以在没有临时文件的情况下):

  git update-index --cacheinfo 100755 $(perl -l''print unless(/ ^#/)'post_load \ 
| git hash -object -w --stdin)post_load

你没有提及你打算编辑哪个编辑器从这样做,所以很难告诉你如何整合这一点。正如我所提到的,您需要以某种方式将git与该文件一起呈现,因为您希望该文件被暂存(请记住,git不会处理存储更改)。如果你可以编写一个宏来保存文件为$ file.tmp,那么使用类似上面的(获得$ the_mode剩下的练习:p),删除$ file.tmp并将编辑器缓冲区还原回$文件,该文件基本上可以处理您要求的内容。

例如,以下脚本需要三个参数: MN路径。它将在path处更新文件的索引内容,以便将M到N(含)的行替换为stdin的内容:

 #!/ bin / sh 

start_line = $ 1
end_line = $ 2
path = $ 3

mode = $(git ls-文件-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 $模式$ blob_id $路径

例如 echoHELLO WORLD| ./stage-part 8 10 post_load 将从8-10替换为HELLO WORLD的三行。


Is there a git command to add differences within a range of line-numbers to the index?

I want to be able to select lines in my editor and run a macro to add any changes in the selection to the index.

解决方案

If you can persuade your editor to write a version of the file that you want to be staged, you can use the plumbing-level git commands to add it to the index under the right name. You need to bypass "git add" which will always associate path X in the working tree with path X in the index (as far as I know).

Once you have the content you want to stage written to some temporary file $tempfile, run git hash-object -w $tempfile - this will write the object to .git/objects and output the blob id. Then feed this blob id to the index using git update-index --cacheinfo 100644 $blobid $path to associate the path $path with that object.

Here's an example that stages a change to a script called "post_load" in my repo without overwriting the file itself (also demonstrating you can do without a temp file):

git update-index --cacheinfo 100755 $(perl -lne 'print unless (/^#/)' post_load \
                                      | git hash-object -w --stdin) post_load

You don't mention which editor you're planning to do this from, so it's hard to advise you on how to integrate this. As I mentioned, you need to somehow present git with the file as you want it to be staged (remember, git doesn't deal with storing changes). If you can write a macro to just save the file as "$file.tmp", then use something like the above to git update-index --cacheinfo $the_mode $(git hash-object -w $file.tmp) $file (obtaining $the_mode is left as an exercise :p), delete $file.tmp and revert the editor buffer back to $file that would do basically what you're asking for.

For example, the following script takes three arguments: M N path. It will update the index content for the file at "path" so that lines M through N (inclusive) are replaced with the content from 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

for example echo "HELLO WORLD" | ./stage-part 8 10 post_load will replace the three lines from 8-10 with just "HELLO WORLD".

这篇关于Git:通过行号将索引*的部分(更改)文件添加到索引*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆