有没有办法让git给你一个提交日志,只提交触及文件中的特定行?
就像git blame,但git blame会显示触及特定行的LAST commit。
我希望得到一个类似的日志,不是文件中任何地方的提交列表,而是触及特定行的提交。
有没有办法让git给你一个提交日志,只提交触及文件中的特定行?
就像git blame,但git blame会显示触及特定行的LAST commit。
我希望得到一个类似的日志,不是文件中任何地方的提交列表,而是触及特定行的提交。
当前回答
你可以通过使用picko -axe来获得一组提交。
git log -S'the line from your file' -- path/to/your/file.txt
这将为您提供影响该文件中该文本的所有提交。如果文件在某个时候被重命名,您可以添加——follow-parent。
如果你想在每次编辑时检查提交,你可以将结果管道到git show:
git log ... | xargs -n 1 git show
其他回答
下面是一个定义git别名的解决方案,所以你可以像这样使用它:
git rblame -M -n -L '/REGEX/,+1' FILE
输出示例:
00000000 18 (Not Committed Yet 2013-08-19 13:04:52 +0000 728) fooREGEXbar
15227b97 18 (User1 2013-07-11 18:51:26 +0000 728) fooREGEX
1748695d 23 (User2 2013-03-19 21:09:09 +0000 741) REGEXbar
您可以在.gitconfig中定义别名,也可以简单地运行以下命令
git config alias.rblame !sh -c 'while line=$(git blame "$@" $commit 2>/dev/null); do commit=${line:0:8}^; [ 00000000^ == $commit ] && commit=$(git rev-parse HEAD); echo $line; done' dumb_param
这是一个丑陋的一行程序,所以这里有一个去混淆的等效bash函数:
git-rblame () {
local commit line
while line=$(git blame "$@" $commit 2>/dev/null); do
commit="${line:0:8}^"
if [ "00000000^" == "$commit" ]; then
commit=$(git rev-parse HEAD)
fi
echo $line
done
}
pickaxe解决方案(git log——pickaxe-regex -S' regex ')只会给你添加/删除行,而不会对包含正则表达式的行进行其他更改。
这个解决方案的一个限制是git blame只返回第一个REGEX匹配,所以如果存在多个匹配,递归可能会“跳转”到另一行。一定要检查完整的历史输出以发现那些“跳转”,然后修复REGEX以忽略寄生行。
最后,这里有一个替代版本,在每次提交时运行git show来获得完整的diff:
git config alias.rblameshow !sh -c 'while line=$(git blame "$@" $commit 2>/dev/null); do commit=${line:0:8}^; [ 00000000^ == $commit ] && commit=$(git rev-parse HEAD); git show $commit; done' dumb_param
您可以混合使用git blame和git log命令来检索git blame命令中每次提交的摘要并附加它们。类似于以下bash + awk脚本。它将提交摘要作为代码注释内联添加。
git blame FILE_NAME | awk -F" " \
'{
commit = substr($0, 0, 8);
if (!a[commit]) {
query = "git log --oneline -n 1 " commit " --";
(query | getline a[commit]);
}
print $0 " // " substr(a[commit], 9);
}'
一句话:
git blame FILE_NAME | awk -F" " '{ commit = substr($0, 0, 8); if (!a[commit]) { query = "git log --oneline -n 1 " commit " --"; (query | getline a[commit]); } print $0 " // " substr(a[commit], 9); }'
请参见Git:发现哪些提交曾经接触过一系列行。
从Git 1.8.4开始,Git log有-L来查看行范围的演变。
例如,假设您查看git blame的输出。这里- l150,+11表示“只看150到150+11行”:
$ git blame -L 150,+11 -- git-web--browse.sh
a180055a git-web--browse.sh (Giuseppe Bilotta 2010-12-03 17:47:36 +0100 150) die "The browser $browser is not
a180055a git-web--browse.sh (Giuseppe Bilotta 2010-12-03 17:47:36 +0100 151) fi
5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 152) fi
5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 153)
5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 154) case "$browser" in
81f42f11 git-web--browse.sh (Giuseppe Bilotta 2010-12-03 17:47:38 +0100 155) firefox|iceweasel|seamonkey|iceape)
5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 156) # Check version because firefox < 2.0 do
5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 157) vers=$(expr "$($browser_path -version)"
5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 158) NEWTAB='-new-tab'
5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 159) test "$vers" -lt 2 && NEWTAB=''
a0685a4f git-web--browse.sh (Dmitry Potapov 2008-02-09 23:22:22 -0800 160) "$browser_path" $NEWTAB "$@" &
你想知道155行的历史。
然后,使用git log。这里,-L 155,155:git-web——browse.sh表示“跟踪文件git-web——browse.sh中155行到155行的演变”。
$ git log --pretty=short -u -L 155,155:git-web--browse.sh
commit 81f42f11496b9117273939c98d270af273c8a463
Author: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
web--browse: support opera, seamonkey and elinks
diff --git a/git-web--browse.sh b/git-web--browse.sh
--- a/git-web--browse.sh
+++ b/git-web--browse.sh
@@ -143,1 +143,1 @@
-firefox|iceweasel)
+firefox|iceweasel|seamonkey|iceape)
commit a180055a47c6793eaaba6289f623cff32644215b
Author: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
web--browse: coding style
diff --git a/git-web--browse.sh b/git-web--browse.sh
--- a/git-web--browse.sh
+++ b/git-web--browse.sh
@@ -142,1 +142,1 @@
- firefox|iceweasel)
+firefox|iceweasel)
commit 5884f1fe96b33d9666a78e660042b1e3e5f9f4d9
Author: Christian Couder <chriscool@tuxfamily.org>
Rename 'git-help--browse.sh' to 'git-web--browse.sh'.
diff --git a/git-web--browse.sh b/git-web--browse.sh
--- /dev/null
+++ b/git-web--browse.sh
@@ -0,0 +127,1 @@
+ firefox|iceweasel)
你可以通过使用picko -axe来获得一组提交。
git log -S'the line from your file' -- path/to/your/file.txt
这将为您提供影响该文件中该文本的所有提交。如果文件在某个时候被重命名,您可以添加——follow-parent。
如果你想在每次编辑时检查提交,你可以将结果管道到git show:
git log ... | xargs -n 1 git show
在我的例子中,行号随着时间的推移发生了很大的变化。 我也在git 1.8.3上,它不支持“git blame -L”中的正则表达式。 (RHEL7仍然有1.8.3)
myfile=haproxy.cfg
git rev-list HEAD -- $myfile | while read i
do
git diff -U0 ${i}^ $i $myfile | sed "s/^/$i /"
done | grep "<sometext>"
Oneliner:
myfile=<myfile> ; git rev-list HEAD -- $myfile | while read i; do git diff -U0 ${i}^ $i $myfile | sed "s/^/$i /"; done | grep "<sometext>"
当然,这可以被做成一个脚本或一个函数。