查找包含对给定文件的更改的 Git 分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6258440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find a Git branch containing changes to a given file
提问by Dustin
I have 57 local branches. I know I made a change to a certain file in one of them, but I'm not sure which one. Is there some kind of command I can run to find which branches contain changes to a certain file?
我有 57 个本地分支机构。我知道我对其中一个文件中的某个文件进行了更改,但我不确定是哪个文件。是否可以运行某种命令来查找哪些分支包含对某个文件的更改?
回答by Seth Robertson
Find all branches which contain a change to FILENAME (even if before the (non-recorded) branch point)
查找包含对 FILENAME 更改的所有分支(即使在(未记录的)分支点之前)
FILENAME="<filename>"
git log --all --format=%H $FILENAME | while read f; do git branch --contains $f; done | sort -u
Manually inspect:
手动检查:
gitk --all --date-order -- $FILENAME
Find all changes to FILENAME not merged to master:
查找未合并到 master 的 FILENAME 的所有更改:
git for-each-ref --format="%(refname:short)" refs/heads | grep -v master | while read br; do git cherry master $br | while read x h; do if [ "`git log -n 1 --format=%H $h -- $FILENAME`" = "$h" ]; then echo $br; fi; done; done | sort -u
回答by Adam Dymitruk
All you need is
所有你需要的是
git log --all -- path/to/file/filename
If you want to know the branch right away you can also use:
如果您想立即了解分支,您还可以使用:
git log --all --format=%5 -- path/to/file/filename | xargs -I{} -n 1 echo {} found in && git branch --contains {}
Further, if you had any renames, you may want to include --follow
for the Git log command.
此外,如果您有任何重命名,您可能希望包含--follow
Git 日志命令。
回答by Simpleton
Looks like this is still a problem without an appropriate solution. I don't have enough credits to comment, so here's my little contribution.
看起来这仍然是一个没有适当解决方案的问题。我没有足够的积分来评论,所以这是我的一点贡献。
Seth Robertson's 1st solution kinda worked for me, but only gave me local branches, among which where many false positives, probably because of merges from the stable branch.
Seth Robertson 的第一个解决方案对我有用,但只给了我本地分支,其中许多误报,可能是因为来自稳定分支的合并。
Adam Dymitruk's 2nd solution didn't work for me at all. For starters, what's --format=%5? It isn't recognized by git, I couldn't find anything about it and I couldn't get it to work with other format options.
Adam Dymitruk 的第二个解决方案根本不适合我。首先,什么是 --format=%5?git 无法识别它,我找不到有关它的任何信息,也无法使其与其他格式选项一起使用。
But his 1st solution combined with the --source option and with a simple grep proved helpful:
但是他的第一个解决方案结合了 --source 选项和一个简单的 grep 被证明是有帮助的:
git log --all --source -- <filename> | grep -o "refs/.*" | sort -u
This gives me a number of remote tags and branches and one local branch, where I made the latest changes to the file. Not sure how complete this is.
这为我提供了许多远程标记和分支以及一个本地分支,我在其中对文件进行了最新更改。不知道这有多完整。
UPDATEas per @nealmcb 's request, sorting branches by most recent change:
根据@nealmcb 的请求更新,按最近的更改对分支进行排序:
First, you could change the grep to "refs/heads/.*", which will give you the local branches only. If there are only a few branches, you could examine the latest commit of each one like this:
首先,您可以将 grep 更改为“refs/heads/.*”,这只会为您提供本地分支。如果只有几个分支,您可以像这样检查每个分支的最新提交:
git log -1 <branch> -- <filename>
If there are more branches and you really want to automate this, you can combine the two commands using xargs, git log formatting and another sort into this one-liner:
如果有更多分支并且你真的想自动化这个,你可以使用 xargs、git log 格式化和另一种排序将这两个命令组合到这个单行中:
git log --all --source -- <filename> | grep -o "refs/heads/.*" | sort -u | xargs -I '{}' git log -1 --format=%aI%x20%S '{}' -- <filename> | sort -r
This will result in output like this:
这将导致如下输出:
2020-05-07T15:10:59+02:00 refs/heads/branch1
2020-05-05T16:11:52+02:00 refs/heads/branch3
2020-03-27T11:45:48+00:00 refs/heads/branch2
回答by whiteinge
The following is an inelegant brute-force method, but I expect it should work. Make sure you've stashed any uncommitted changes first as it will switch which branch you are currently on.
以下是一种不雅的蛮力方法,但我希望它应该有效。确保您首先隐藏了任何未提交的更改,因为它会切换您当前所在的分支。
for branch in $(git for-each-ref --format="%(refname:short)" refs/heads); do
git checkout $branch && git grep SOMETHING
done