简体   繁体   English

git log只返回对master分支的提交?

[英]git log to return only the commits made to the master branch?

I've done a bit of searching and found: 我做了一些搜索,发现:

git log myBranchName

as a possible solution. 作为可能的解决方案。 But what happens when my branch is the master branch? 但是当我的分支是主分支时会发生什么? when I run: 当我跑:

git log master

It seems to return everything commited to any branch. 它似乎返回所有提交给任何分支的东西。 Based on what I've read, it lists all of the commits related to the master branch. 根据我读过的内容,它列出了与主分支相关的所有提交。 Knowing that, how I can call up the commit history of the master branch only? 知道了,我怎么才能调出主分支的提交历史记录?

I think this is what you want 我想这就是你想要的

git log --first-parent master

To quote the manual 引用手册

Follow only the first parent commit upon seeing a merge commit. 在看到合并提交时,仅遵循第一个父提交。 This option can give a better overview when viewing the evolution of a particular topic branch, because merges into a topic branch tend to be only about adjusting to updated upstream from time to time, and this option allows you to ignore the individual commits brought in to your history by such a merge. 在查看特定主题分支的演变时,此选项可以提供更好的概述,因为合并到主题分支往往只是关于不时调整到更新的上游,并且此选项允许您忽略引入的单个提交通过这样的合并你的历史。

Due to Git's branching model, commits do not belong to a single or multiple branches. 由于Git的分支模型,提交不属于单个或多个分支。 Branches are pointers to single commit objects within the whole commit graph. 分支是整个提交图中指向单个提交对象的指针。 So when you say a commit is “on a branch X” in X, you usually mean that it is reachable when starting at the commit the branch X points to. 因此,当您在X中表示提交是“在分支X上”时,通常意味着在分支X指向的提交处开始时可以访问它。

For git log , the default behaviour is equal to git log HEAD where HEAD refers to the commit the current branch currently points at. 对于git log ,默认行为等于git log HEAD ,其中HEAD指的是当前分支当前指向的提交。 So if you are on the master branch, it is equal to git log master , showing all commits that are reachable when starting at the most recent commit. 因此,如果您在主分支上,它等于git log master ,显示从最近提交开始时可以访问的所有提交。

Unfortunately what you are referring to as a commit made to a certain branch is not clearly defined in Git. 不幸的是,你所指的是某个分支的提交在Git中没有明确定义。 If I make a commit on master, and then create a new branch that points to the same commit (eg using git branch newbranch ), then that branch is literally identical to the master branch except for the name. 如果我在master上进行提交,然后创建一个指向同一提交的新分支(例如使用git branch newbranch ),那么除了名称之外,该分支与master分支完全相同。 So every property “made on branch master” would now also imply “made on branch newbranch” . 所以每一个“在分支大师 身上制造 ”的财产现在也意味着“在分支新分支上制造” As such you cannot have this property in Git. 因此,您不能在Git中拥有此属性。

Even parkydr's solution, which shows all commits which were made only on a single side of merges is not a failproof solution. 即使是parkydr的解决方案,它显示了仅在合并的一侧进行的所有提交,也不是一个防故障解决方案。 Ideally it would hide all those commits which were made on a separate non-master branch and which were then merged back into master. 理想情况下,它会隐藏在单独的非主分支上进行的所有提交,然后将这些提交合并回主服务器。 As such you would only get commits that are either made to the master-line directly or which are merge commits merging in some other commits. 因此,您只能获得直接对主线进行的提交,或者在其他一些提交中进行合并提交的提交。 However there are two things that will prevent this from working: 但是有两件事会妨碍它的运作:

  1. Fast-forward merges: When you branch off from master and create some commits, while creating no new ones on master directly, then a git merge somebranch on master will fast-forward the commits, resulting in the master branch pointing to the same commit as somebranch . 快进合并:当你从master分支并创建一些提交时,直接在master上创建没有新的提交,那么git merge somebranch上的git merge somebranch将快进提交,导致master分支指向同一个提交somebranch As such you “lose” the information that those commits were originally created on a separate branch. 因此,您“丢失”这些提交最初在单独分支上创建的信息。 You can force Git to always create merge commits though, using git merge --no-ff but this won't help you afterwards. 您可以强制Git始终使用git merge --no-ff创建合并提交,但这对后来无济于事。
  2. The merge order is not guaranteed: When you are on master and merge a branch in, then the previous master commit will always be the first parent. 无法保证合并顺序:当您使用master并合并分支时,之前的主提交将始终是第一个父提交。 So you would get your desired behaviour. 所以你会得到你想要的行为。 However it is perfectly possible to be on said branch, and merge master in instead, resulting in the master commit being the second parent. 然而,完全有可能在所述分支上,而是合并master,导致主提交是第二个父。 Then master could be fast-forwarded (or reset) to the new commit resulting in a “reversed” view. 然后可以将master快速转发(或重置)到新提交,从而产生“反转”视图。

So, the bottom line is that you cannot safely get such a history. 所以,最重要的是,你无法安全地获得这样的历史。 You're better off getting used to how Git's flexible branching model works. 你最好习惯Git灵活的分支模型是如何工作的。

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

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