【问题标题】:Git add lines to index by grep/regexGit 通过 grep/regex 添加行到索引
【发布时间】:2013-02-20 11:39:18
【问题描述】:

我有一个巨大的补丁,我想将它分成多个逻辑 git 提交。大量更改只是更改变量名称或函数调用,以便可以使用 grep 轻松定位它们。如果我可以将任何与正则表达式匹配的更改添加到索引中,然后在 git gui 中进行清理,它将为我节省大量的手动工作。有没有一种好方法可以使用 git 中的正则表达式或从 grep 的某些输出(例如行号)逐行更新索引?

我找到了a similar question,但我不确定如何通过正则表达式搜索构建临时文件。

【问题讨论】:

  • 一些示例可能有助于阐明您想要实现的目标。

标签: regex git git-add


【解决方案1】:

patchutils 有一个命令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 以避免获得不相关的更改。您可能需要调整此值以适应您的情况。

【讨论】:

  • 这对我来说效果很好,但请注意,如果您修改了与正则表达式不匹配的相邻行,hunk 并不能真正提供足够的粒度。
  • 我发现一个好方法是只运行第二行然后检查结果。 git status -v 用于上演的内容,git diff 用于查看所有未上演的内容。如果一切看起来都正确,则提交。
【解决方案2】:

更简单地说,您可以使用git add -p 并利用/ 选项在您的差异中搜索要添加的补丁。它不是完全自动化的,但比我发现的其他替代方案更容易。

【讨论】:

    【解决方案3】:

    你可以先运行:

    git status | \grep "your_pattern"
    

    如果输出符合预期,则将文件添加到索引中:

    git add $(git status | \grep "your_pattern")
    

    【讨论】:

      【解决方案4】:

      我现在在 Windows 上使用 Git-Bash,我遇到了类似的问题:我不需要从“未暂存的提交文件列表”中添加一些文件:

       $ git status
       On branch Bug_#292400_buggy
       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 
          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
      

      首先,我检查了文件选择是否是我正在寻找的:

      $ git status | grep ok
                  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
      

      我尝试了这个 dorum 中描述的一个想法,以便使用 git 添加相同的文件列表,如下所示:

      $ git add $(git status | \grep "your_pattern")
      

      但这对我不起作用(记住:Windows10 上的 Git-Bash

      至少,我直接试过了,效果很好:

      $ 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
      

      准备提交,所以。

      【讨论】:

        【解决方案5】:

        xargs 是您正在寻找的。试试这个:

        grep -irl 'regex_term_to_find' * | xargs -I FILE git add FILE
        

        直到管道| 是您搜索所有文件* 的标准grep 命令。选项是:

        • i - 不区分大小写
        • r - 通过目录递归
        • l - 仅列出文件名

        在语句FILExargs 部分中,是用于grep 命令传递的每个参数/匹配的变量的名称。然后在适当的地方使用变量输入所需的命令。

        【讨论】:

        • 谢谢,但我只想添加文件的一部分,而不是整个文件。
        • @FazJaxton,为此您需要将其添加为补丁。在git add 之后使用-p 参数,但是我不确定它在xargs 中如何工作,因为补丁过程由您完成(即选择应该添加的内容和不应该添加的内容)。
        猜你喜欢
        • 2012-11-15
        • 2011-12-10
        • 1970-01-01
        • 1970-01-01
        • 2021-08-31
        • 2021-09-07
        • 2021-04-12
        • 1970-01-01
        • 2012-01-18
        相关资源
        最近更新 更多