0

I have a branch, develop, to which I merged several days ago using commit hash 92dd09b.

For clarity, here is an image of my commit in Git Extensions: enter image description here

Given that hash, I need to find the prior commit on the develop branch prior to my merge. How would I go about doing that?

If I check out my commit hash, git log shows the previous commits on my branch, not on develop.

I looked at this this question, then I checked out my merge hash (92dd09b) and ran this command:
git log HEAD@{1} -1

But I get an error: fatal: ambiguous argument 'HEAD@': unknown revision or path not in the working tree.

I'm using Git for Windows.

bopapa_1979
  • 8,949
  • 10
  • 51
  • 76

2 Answers2

1

Based on comments, it seems as though I misread your question regarding what commit 92dd09b identifies. I've kept my original answer below to avoid making the comment conversations even more confusing. But based on my current understanding, you really have

                              ... x
                                   \
x -- x -- x -- A -- 92dd09b -- x -- M2 -- x <--(develop)
                   /
         ... x -- B <--(branch)

In that case, what you tried first (checkout 92dd09b, run git log) should show the commits from both branches; you could exclude the commits from the "merged in" branch by saying

git log --first-parent

Or, per phd's answer, you could say

git show 92dd09b~

(among other things).

Your comments indicate that this didn't get you to the right commit, though, which suggests that something is incorrectly observed.


So as I understand the question you had a develop branch

x -- x -- x -- A <--(develop)

and you had another branch

x -- x -- x -- A <--(develop)

   ... x -- 92dd09b <--(branch)

and you merged 92dd09b into develop

x -- x -- x -- A -- M1 <--(develop)
                   /
   ... x -- 92dd09b <--(branch)

and now maybe some more work has happened

                        ... x
                             \
x -- x -- x -- A -- M1 -- x -- M2 -- x <--(develop)
                   /
   ... x -- 92dd09b <--(branch)

So now you want to find A.

As a rule, commits make it easy to look "back in time"; less easy to look forward. If you start from 92dd09b, you have to "look forward" to get to M1, and then look back at A.

So it's not as easy as you might hope. But it can be done. You could start with

git log --merges --format=%p develop

This will give you parent lists for each merge commit in develop, one on each line. So then you can filter that in any number of ways.

git log --merges --format=%p develop |grep 92dd09b$ |cut -d" " -f1

should print the abbreviated hash of the "other" parent of M1, which should be A. (Assuming it's not an octopus merge, and assuming you merged 92dd09b into develop and not the other way around.)

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
  • This is close. My commit WAS the merge commit. It would be M1, I think. I have added an image of my commit in GitExtensions for reference, just in case I am not describing it properly. – bopapa_1979 May 22 '18 at 17:52
  • @EricBurcham : Well, now that is confusing. If you're saying that 92dd09b is the merge commit, then phd's answer should've returned the correct result. – Mark Adelsberger May 22 '18 at 17:57
  • @EricBurcham - And for that matter, `git log` would then show commits on *both* branches. You could refine that as `git log --first-parent` to exclude the other branch, and see *only* the commits from `develop` – Mark Adelsberger May 22 '18 at 17:59
  • OK, looking very carefully at the result, the result from git show 92dd09b~ return the correct commit. That commit was merged both into my branch, AND is the most recent commit on develop prior to my merge. It was merged separately before my merge. Go figure. Thanks for all the help. – bopapa_1979 May 22 '18 at 18:11
0

First, you don't need to checkout the merge so let's return back to develop:

git checkout develop

To get the previous commit from the known hash:

git show 92dd09b~

There are many different ways to navigate through history naming one commit from the other: https://git-scm.com/docs/gitrevisions

phd
  • 82,685
  • 13
  • 120
  • 165
  • Well, if @EricBurcham says that's perfect, then ok; but I'm guessing he hasn't tested it when he says that, because if `92dd09b` is the commit merged in, then `92dd09b~` is a commit from his other branch - same as with the procedure he used. – Mark Adelsberger May 22 '18 at 17:38
  • @MarkAdelsberger you are correct. It returned a different hash, so I assumed it was right. When I looked at the git log, I did not notice the fact that this commit is ALSO in my branch. Can you suggest the correct thing to do? – bopapa_1979 May 22 '18 at 17:44
  • @EricBurcham - Posted as an answer – Mark Adelsberger May 22 '18 at 17:49