Git通过grep/regex将行添加到索引
我有一个巨大的补丁,我想分为多个逻辑git提交。大量更改只是更改变量名或函数调用,这样就可以很容易地用grep找到它们。如果我可以在索引中添加任何与正则表达式匹配的更改,然后在git gui中进行清理,这将节省我大量的手工工作。是否有一种好方法可以使用git中的正则表达式或grep的某些输出(例如行号)逐行更新索引Git通过grep/regex将行添加到索引,regex,git,git-add,Regex,Git,Git Add,我有一个巨大的补丁,我想分为多个逻辑git提交。大量更改只是更改变量名或函数调用,这样就可以很容易地用grep找到它们。如果我可以在索引中添加任何与正则表达式匹配的更改,然后在git gui中进行清理,这将节省我大量的手工工作。是否有一种好方法可以使用git中的正则表达式或grep的某些输出(例如行号)逐行更新索引 我找到了,但我不确定如何从regex类型的搜索中构建临时文件。xargs是您需要的。试试这个: grep -irl 'regex_term_to_find' * | xargs -I
我找到了,但我不确定如何从regex类型的搜索中构建临时文件。xargs是您需要的。试试这个:
grep -irl 'regex_term_to_find' * | xargs -I FILE git add FILE
直到管道|
是用于搜索所有文件的标准grep命令*
。选项包括:
-不区分大小写i
-通过目录递归r
-仅列出文件名l
xargs
部分FILE
中,是grep命令传递的每个参数/匹配所使用的变量的名称。然后在适当的地方使用变量输入所需的命令。有一个可用于实现此目的的命令grepdiff
# check that the regex search correctly matches the changes you want.
git diff -U0 | grepdiff 'regex search' --output-matching=hunk
# then apply the changes to the index
git diff -U0 | grepdiff 'regex search' --output-matching=hunk | git apply --cached --unidiff-zero
我在diff上使用
-U0
,以避免得到不相关的更改。您可能需要调整此值以适应您的情况。更简单地说,您可以使用git add-p
并使用/
选项在您的diff中搜索要添加的修补程序。它不是完全自动化的,但比我发现的其他选择更容易。您可以先运行:
git status | \grep "your_pattern"
如果输出符合预期,则将文件添加到索引:
git add $(git status | \grep "your_pattern")
我现在正在使用Windows上的Git Bash,我遇到了一个类似的问题:我不需要从“不准备提交的文件列表”中添加一些文件: 为了用git添加相同的文件列表,我尝试了本dorum中描述的一个想法,如下所示:
$ git add $(git status | \grep "your_pattern")
但它对我不起作用(记住:GitBashoverWindows10)
至少,我直接尝试了一下,效果很好:
$ git add *ok
$ git status
On branch Bug_#292400_buggy
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: the/path/to/the/file1.ok
modified: the/path/to/the/file2.ok
modified: the/path/to/the/file3.ok
modified: the/path/to/the/file4.ok
....................................
modified: the/path/to/the/file666.ok
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: the/path/to/the/file333.NO
modified: the/path/to/the/file334.NO
$git添加*确定
$git状态
在branch Bug#292400_四轮马车上
要提交的更改:
(使用“git重置磁头…”取消分级)
修改:将/path/to/the/file1.ok
修改:将/path/to/the/file2.ok
修改:将/path/to/the/file3.ok
修改:将/path/to/the/file4.ok
....................................
修改:将/path/to/the/file666.ok
未为提交而暂存的更改:
(使用“git add…”更新将提交的内容)
(使用“git签出--…”放弃工作目录中的更改)
修改:将/path/to/the/file333.NO
修改后的/path/to/the/file334.NO
准备好提交了,所以。一些示例可能会大大有助于澄清您试图实现的目标。谢谢,但我只想添加文件的一部分,而不是整个文件。@FazJaxton,因此您需要添加一个补丁。在
git add
之后使用-p
参数,但是我不确定它在xargs中如何工作,因为补丁过程是由您完成的(即选择应该添加什么和不应该添加什么)。这对我来说非常有效,但是,作为一个注释,hunk
如果修改了与正则表达式不匹配的相邻行,则无法提供足够的粒度。我发现一个好方法是只运行第二行,然后检查结果git status-v
用于暂存的内容,而git diff
用于查看所有未暂存的内容。然后,如果一切看起来都正确,则提交。
$ git add $(git status | \grep "your_pattern")
$ git add *ok
$ git status
On branch Bug_#292400_buggy
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: the/path/to/the/file1.ok
modified: the/path/to/the/file2.ok
modified: the/path/to/the/file3.ok
modified: the/path/to/the/file4.ok
....................................
modified: the/path/to/the/file666.ok
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: the/path/to/the/file333.NO
modified: the/path/to/the/file334.NO