简体   繁体   English

Git:按分支过滤提交历史

[英]Git: Filtering commit history by branch

I'm still new to Git so I'm not sure how to do this, although I've seen it done with some software. 我对Git还是很陌生,所以虽然我已经看到它是使用某些软件完成的,但我不确定如何做到这一点。 How can you get a list of commits that were made on a given branch? 如何获得在给定分支上进行的提交的列表?

The more specific reason I would like to know this is so that I can find all files that were added to the repo via a specified branch as the input. 我想知道的更具体的原因是,我可以找到通过指定分支作为输入添加到仓库的所有文件。 Thanks. 谢谢。

A simplistic approach for a list of commits on a given branch, excluding merges into it: 给定分支上的提交列表的简单方法,不包括合并到该分支中:

git log --first-parent <branch>

The above command will include all commits that led to the commit at the creation of the branch. 上面的命令将包括所有在创建分支时导致提交的提交。

Because, you must remember, git does not really track where branches have started. 因为,必须记住,git不会真正跟踪分支的起始位置。 A branch is just a moving label, pointing to a commit, and its log normally shows a list of ALL commits that have contributed to the history of this commit. 分支只是一个移动标签,指向一个提交,并且其日志通常显示一个列表,列出了对该提交的历史做出了贡献的所有提交。 Including all merges. 包括所有合并。

In the original repo where the branch was created, you can extract the history from branch-start from the reflog: 在创建分支的原始存储库中,您可以从reflog中从branch-start提取历史记录:

git reflog show <branch>

But good luck on a server, where the branch was first pushed after multiple commits. 但是祝您好运,在服务器上,在多次提交之后分支首先被推送的服务器。

Actually, I wonder, does anyone have a reliable solution for finding the starting point of a branch? 我想知道,实际上,有人可以找到分支的起点吗? Graph theory experts wanted. 图论专家想要。

Checking out git log --help , there is the --branches=<pattern> option which you can use. 检出git log --help ,可以使用--branches=<pattern>选项。

So you can do git log --branches=<branch-name> --name-only 所以你可以做git log --branches=<branch-name> --name-only

This will list all the commits along with the files that were changed in each one. 这将列出所有提交以及在每个提交中更改的文件。

You can use git merge-base <branch1> <branch2> to find the SHA that the branches diverged from. 您可以使用git merge-base <branch1> <branch2>查找分支偏离的SHA。

Then using git log --branches=<branch-name> --name-only <SHA>..HEAD you can see all the changes only on the branch. 然后使用git log --branches=<branch-name> --name-only <SHA>..HEAD您只能在分支上看到所有更改。

You don't need to checkout the branch in order to see its log. 您无需签出分支即可查看其日志。 In fact, you don't necessarily need a branch at all, all you need is a ref. 实际上,您根本不需要分支,只需要一个引用。 Either way, let's say your branch (or commit hash/ref) is called mybranch . 无论哪种方式,假设您的分支(或提交哈希/引用)称为mybranch Simply run this command to get a list of all the files which were once a part of your git history: 只需运行以下命令即可获取曾经属于git历史记录的所有文件的列表:

git log --name-only --format="% " mybranch | grep -v '^$' | sort | uniq

Where --name-only will show you the file names, --format="% " will remove all commit messages and hashes, mybranch marks the starting point to go backwards from (until the first commit). --name-only将显示文件名,-- --format="% "将删除所有提交消息和哈希值,而mybranch的起点(直到第一次提交)。 Then you remove all empty lines with grep , and you have to sort the names in order to remove duplicates with uniq . 然后,使用grep删除所有空行,并且必须sort名称进行sort ,以便使用uniq删除重复项。 The generated list will include files which were deleted or moved within the repository. 生成的列表将包括已在存储库中删除或移动的文件。

If you need to see only the current files (will not include deleted ones), use the following command: 如果只需要查看当前文件(不包括已删除的文件),请使用以下命令:

git ls-tree -r --full-tree --name-only mybranch

To see a list of commits (without the files - your first question), simply run 要查看提交列表(不包含文件,这是您的第一个问题),只需运行

git log REF

Or, if you want single line format, use the --format=oneline flag. 或者,如果要单行格式,请使用--format=oneline标志。 Also, try out the --stat flag. 另外,尝试使用--stat标志。 It's not as greppable as the above examples, but may prove itself useful for you one day. 它不像上面的示例那样令人难忘,但有一天可能会证明对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM