Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the first commit of a branch?

Tags:

git

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. 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).

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.

like image 539
Tamás Szelei Avatar asked Apr 11 '13 11:04

Tamás Szelei


People also ask

How do I find my first commit?

Click on the "Insights" tab of the repository that you want to see the oldest commit, followed by the "Network" sub-tab on the left menu bar. When the page is fully loaded (i.e. you can see lots of lines joining and all), press Shift + ← to go all the way to the first commit.

How do I see the commit history of a branch?

git log --oneline is a great way to view commit history by displaying the first seven characters of the SHA-1 hash and commit message of the commits on the current branch. git log --oneline --graph presents commit history in a ASCII graph displaying the different branches in the repository and their commits.

How can I see my first commit in github?

Usage. Go to any particular repo's landing page (e.g. like the one you're on) and click the bookmarklet, which will take you to the first page (initial commit). By default, it tracks the master branch, but if you change the branch on the landing page, it will go to that branch's first commit.

How do I find a specific commit in git?

Looking up changes for a specific commit If you have the hash for a commit, you can use the git show command to display the changes for that single commit. The output is identical to each individual commit when using git log -p .


1 Answers

There are no commits “on a branch” in 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.
  • I want to find the first commit that is reachable from branch, but not from 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. 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:

 git reset --soft `git rev-list ^master mybranch | tail -n 1`^
like image 50
Chronial Avatar answered Oct 20 '22 20:10

Chronial