I have a repository with branches master and A and lots of merge activity between the two. How can I find the commit in my repository when branch A was created based on mast
The following command will reveal the SHA1 of Commit A
git merge-base --fork-point A
I seem to be getting some joy with
git rev-list branch...master
The last line you get is the first commit on the branch, so then it's a matter of getting the parent of that. So
git rev-list -1 `git rev-list branch...master | tail -1`^
Seems to work for me and doesn't need diffs and so on (which is helpful as we don't have that version of diff)
Correction: This doesn't work if you are on the master branch, but I'm doing this in a script so that's less of an issue
You can examine the reflog of branch A to find from which commit it was created, as well as the full history of which commits that branch pointed to. Reflogs are in .git/logs
.
A simple way to just make it easier to see the branching point in git log --graph
is to use the option --first-parent.
For example, take the repo from the accepted answer:
$ git log --all --oneline --decorate --graph
* a9546a2 (HEAD -> master, origin/master, origin/HEAD) merge from topic back to master
|\
| * 648ca35 (origin/topic) merging master onto topic
| |\
| * | 132ee2a first commit on topic branch
* | | e7c863d commit on master after master was merged to topic
| |/
|/|
* | 37ad159 post-branch commit on master
|/
* 6aafd7f second commit on master before branching
* 4112403 initial commit on master
Now add --first-parent
:
$ git log --all --oneline --decorate --graph --first-parent
* a9546a2 (HEAD -> master, origin/master, origin/HEAD) merge from topic back to master
| * 648ca35 (origin/topic) merging master onto topic
| * 132ee2a first commit on topic branch
* | e7c863d commit on master after master was merged to topic
* | 37ad159 post-branch commit on master
|/
* 6aafd7f second commit on master before branching
* 4112403 initial commit on master
That makes it easier!
Note if the repo has lots of branches you're going to want to specify the 2 branches you're comparing instead of using --all
:
$ git log --decorate --oneline --graph --first-parent master origin/topic