有没有办法让git给你一个提交日志,只提交触及文件中特定行的提交? git
像git blame
同样,可是git blame
会告诉你触及特定行的最后提交。 github
我真的想获得一个相似的日志,而不是文件中任何地方的提交列表,而只是提交到特定行的提交。 web
这将为每一个有意义的修订调用git blame
来显示文件$FILE
行$LINE
: 正则表达式
git log --format=format:%H $FILE | xargs -L 1 git blame $FILE -L $LINE,$LINE
像往常同样,责备显示每行开头的修订号。 你能够追加 vim
| sort | uniq -c
获取聚合结果,相似于更改此行的提交列表。 (不彻底是,若是代码只是被移动了,这可能会为该行的不一样内容显示两次相同的提交ID。对于更详细的分析,您必须对相邻提交的git blame
结果进行滞后比较。任何人?) bash
这是一个定义git别名的解决方案,所以您能够像这样使用它: ide
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中定义别名,或者只需运行如下命令 spa
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函数: firefox
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:发现哪些提交触及了一系列行 。
从Git 1.8.4开始 , git log
有-L
来查看一系列行的演变。
例如,假设你看看git blame
的输出。 这里-L 150,+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)
一个很是简单的方法是使用vim-fugitive 。 只需在vim中打开文件,使用V
选择您感兴趣的行,而后输入
:Glog
如今,您可使用:cnext
和:cprev
来查看修改该行的文件的全部修订版。 在任什么时候候,输入:Gblame
以查看sha,做者和日期信息。
尝试使用Git 1.8.4中实现的如下命令。
git log -u -L <upperLimit>,<lowerLimit>:<path_to_filename>
因此,你的状况upperLimit
& lowerLimit
是感动line_number
更多信息 - https://www.techpurohit.com/list-some-useful-git-commands