How can I find the first branch latch?

Assume the following graph:

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

I would like to find G to be able to interactively rebase for squash commits (but still keep one commit in the thread branches), which will be considered and merged later. I don’t want to just reinstall the whole branch, because I want to save the information that there is a branch, and it was merged.

(I know that I can look at the history and just use the SHA checksum to commit, but I'm looking for a way to do this without manually digging up information or counting the number of commits and using ~ from HEAD with this number).

Edit: Clarify what I want to achieve:

I want to avoid this:

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

And instead, we get something like this:

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

In other words, I want to deflate commits with an interactive permutation in the topic branch into one, but still save the branch and use regular merging to integrate changes from the topic branch into the master.

+7
source share
3 answers

There are no "branch" commits in the git section. There are only commits available from the branch. And this information is not a very good indicator of which branch originally belonged to the team. Therefore, I see two ways to interpret your question:

  • I want to find the first commit after the merge base of branch and master , which is on the branch side.
  • I want to find the first commit available from branch , but not from master .

I'm going to suggest that you meant the second. The answer to this is returned by this command:

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

Note: All of this will end badly if you separate the topic threads.


I am a little confused about what you want to achieve. If you want the whole branch to be represented by a single message, 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 

Please note that this will start squash at the first merger base. Perhaps this is not what you want if you combined master in mybranch . You can change this to start with the first commit on mybranch, but not on master (it may have unexpected and unexpected results in complex stories), replacing the second command as follows:

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

You will get B with git merge-base

 git merge-base FI 

Or easier with

 git merge-base my_branch my_other_branch 

Then you can reinstall the result

 git rebase -i B 

And just save the first commit G, crushing all the others.

+4
source

git merge-base will give you B. Do you want all direct children from B to be also ancestors of I. This is due to How to find the next commit in git? and Link to child commit element in 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 end of the topic-branch along with the parents and show those where the parent is the root of the merge-base .

Since a topic branch can contain merges, there can be more than one “first” commit to branches, topologically.

+1
source

All Articles