简体   繁体   English

如何找到分支的第一个提交?

[英]How can I find the first commit of a branch?

Assume the following graph: 假设如下图:

A -- B -- C -- D -- E -- F
      \
       G -- H -- I

I would like to find G in order to be able to an interactive rebase to squash the commits (but still keep one commit on the topic branch) which will be reviewed and merged later. 我想找到G ,以便能够通过交互式rebase来压缩提交(但仍然在主题分支上保留一个提交),稍后将对其进行检查和合并。 I don't want to just rebase the whole branch, because I want to keep the information that there was a branch and it was merged. 我不想仅仅修改整个分支,因为我想保留有分支的信息并将其合并。

(I know that I can look at the history and just use the SHA checksum of the commit, but I'm looking for a way to do it without manually digging up the information or counting how many commits were made and use ~ from HEAD with that number). (我知道我可以查看历史记录,只使用提交的SHA校验和,但我正在寻找一种方法来实现它,而无需手动挖掘信息或计算提交了多少提交并使用〜来自HEAD那个数字)。

Edit: clarification of what I want to achieve: 编辑:澄清我想要实现的目标:

I want to avoid this: 我想避免这个:

A -- B -- C -- D -- E -- F -- J
      \                      /
       G -- H -- I -- -- -- -

And have something like this instead: 而是有类似的东西:

A -- B -- C -- D -- E -- F -- J
      \                      /
       G' -- -- -- -- -- -- -

In other words, I want to squash the commits with an interactive rebase on the topic branch into one, but still keep the branch and use a regular merge to integrate the changes from the topic branch into the master. 换句话说,我想将主题分支上的交互式rebase压缩为一个,但仍保留分支并使用常规合并将主题分支中的更改集成到主分支中。

There are no commits “on a branch” in git. git中没有“在分支上”的提交。 There are only commits reachable from a branch. 只有从分支可以访问的提交。 And that information is not a very good indicator of what branch that commit originally belonged to. 而且这些信息并不是一个非常好的指标,表明最初提交的分支属于哪个分支。 So I see two ways to interpret your question: 所以我看到两种方式来解释你的问题:

  • I want to find the first commit after the merge-base of branch and master that is on the side of branch. 我想找到branch一侧的分支和master的合并基础之后的第一个提交。
  • I want to find the first commit that is reachable from branch , but not from master . 我想找到第一个可以从branch访问的提交,但不能从master

I am gonna assume you meant the second one. 我假设你的意思是第二个。 The answer to that is returned by this command: 这个命令返回的答案是:

 git rev-list ^master mybranch | tail -n 1

Note: This is all gonna fail horribly if you branch off topic branches. 注意:如果你分支主题分支,这将是非常可怕的失败。


I am a little bit confused as to what you want to achieve. 关于你想要达到的目标,我有点困惑。 If you want a whole branch to be represented by one commit, but still get a merge, You should do this: 如果您希望整个分支由一个提交表示,但仍然获得合并,您应该这样做:

git checkout mybranch
git reset --soft `git merge-base mybranch master`
git commit -m 'All of Mybranch'
git checkout master
git merge --no-ff mybranch

Note that this will start the squash at the first merge-base. 请注意,这将在第一个合并基础上开始压缩。 This might not be what you want if you merged master into mybranch before. 如果您之前将master合并到mybranch ,这可能不是您想要的。 You can modify this to start at the first commit on mybranch, but not on master (might have wierd and unexpected results in complex histories) by replacing the second command with this: 您可以将此修改为从mybranch上的第一次提交开始,而不是在master上(可能在复杂的历史记录中可能有奇怪且意外的结果)通过将第二个命令替换为:

 git reset --soft `git rev-list ^master mybranch | tail -n 1`^

You will get B with git merge-base 你将获得B与git merge-base

git merge-base F I

Or more simply with 或者更简单地说

git merge-base my_branch my_other_branch

You can then rebase with the result 然后,您可以使用结果进行rebase

git rebase -i B

And just keep the first commit G, while squashing all the others. 只需保留第一个提交G,同时压缩所有其他提交。

git merge-base will give you B. You want all direct children of B that are also ancestors of I. That's related to How do I find the next commit in git? git merge-base会给你B.你想要B的所有直接孩子也是我的祖先。这与我如何在git中找到下一个提交有关? and Referencing the child of a commit in Git . 在Git中引用提交的子代 Try something like: 尝试类似的东西:

root=$(git merge-base master topic-branch)

git rev-list --parents ^$root topic-branch | grep " $root" | cut -f1 -d' '

This will list all the commits from the root to the tip of the topic-branch along with their parents and show the ones where the parent is the merge-base root. 这将列出从topic-branch的根到顶端的所有提交及其父节点,并显示父节点是基于merge-base根节点。

Because a topic branch may contain merges there may be more than one "first" commit in a branch, topologically. 因为主题分支可能包含合并,所以在拓扑中可能在分支中存在多个“第一”提交。

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

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