gpt4 book ai didi

git - 查看特定行的git历史

转载 作者:太空狗 更新时间:2023-10-29 13:29:50 29 4
gpt4 key购买 nike

我想查看与文件中特定行相关的提交历史。我知道我们可以获得文件的历史记录。是否有任何命令可用于仅对与某一特定行相关的提交进行排序?

最佳答案

bomz 给出了正确的选项,但语法错误。固定线路看起来像这样:

git log -L '/the line from your file/,+1:path/to/your/file.txt'

-L 参数的含义是“在 path/to/your/中找到正则表达式 /the line from your file/ 的第一次出现file.txt 并显示关于从这一点开始的一行范围的日志(意思是,只有这一行,但你可以说 +5 代替)。

需要注意的是,如果该行包含在正则表达式中具有特殊含义的字符,您需要对它们进行转义。

但是,使用行号可能会简单得多,如下所示:

git log -L15,+1:'path/to/your/file.txt'

(对于文件 path/to/your/file.txt 的第 15 行)

在这两种情况下,+1 都可以替换为更大的数字以获得更多行,或者替换为正则表达式以匹配所选范围的末尾。

来自 the docs 的详细说明:

-L <start>,<end>:<file>
-L :<funcname>:<file>

Trace the evolution of the line range given by "<start>,<end>" (or the function name regex <funcname>) within the <file>. You may not give any pathspec limiters. This is currently limited to a walk starting from a single revision, i.e., you may only give zero or one positive revision arguments. You can specify this option more than once.

<start> and <end> can take one of these forms:

  • number

    If <start> or <end> is a number, it specifies an absolute line number (lines count from 1).

  • /regex/

    This form will use the first line matching the given POSIX regex. If <start> is a regex, it will search from the end of the previous -L range, if any, otherwise from the start of file. If <start> is “^/regex/”, it will search from the start of file. If <end> is a regex, it will search starting at the line given by <start>.

  • +offset or -offset

    This is only valid for <end> and will specify a number of lines before or after the line given by <start>.

If “:<funcname>” is given in place of <start> and <end>, it is a regular expression that denotes the range from the first funcname line that matches <funcname>, up to the next funcname line. “:<funcname>” searches from the end of the previous -L range, if any, otherwise from the start of file. “^:<funcname>” searches from the start of file.

关于git - 查看特定行的git历史,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50469927/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com