1

I've got following history in git:

* 02e085a (master) readme update
| * 1d940da (HEAD -> b1) search.py
| | * 7aaa12d (b2) file2
| |/
| * 7be9db9 file1
|/
* 22601c0 initial commit

Now I'm on b2 and I would like to get the point, where b2 branched off (7be9db9). The algo is following:

  1. get next commit
  2. check branches the commit belongs to
  3. if only current branch, continue with 1. step else 4.step
  4. the commit belongs to multiple branches, this must be the point, where we branched off

However, if I want to do the same for b1, I end up at the same position - 7be9db9. But I would expect to end up at 22601c0, as this was the point, where b1 branched off. The history has been created like this.

initial commit branch from master (b1) and commit file1 branch from b1 (b2) and commit file2 checkout b1 and commit search.py

I'm afraid the way git is designed, it's not possible to distinguish, whether commit 7be9db9 (file1) has been created by b1 or b2, am I correct? So it's not possible to say, whether the parent of b1 is master or b2?

Thanks for confirmation.

ST Renegade
  • 311
  • 3
  • 14
  • 2
    What's wrong with using [git merge-base](https://git-scm.com/docs/git-merge-base)? To define where X is branched off you need to define first what's the branch it was branched off. – raina77ow Mar 12 '21 at 08:10
  • 1
    We want to introduce some automation step, so the robot has to determine this by himself. – ST Renegade Mar 12 '21 at 08:23
  • @STRenegade Why does the robot need to know this? – Schwern Mar 12 '21 at 08:33
  • 1
    Please edit the question to indicate why you'd want to know this. And bear in mind that a branch is a pointer to a commit - it's not more than an alias. – AD7six Mar 12 '21 at 08:35
  • The question was, am I right that this is not possible? Or should I dig deeper and spend time on it? The answer is, no, it's not possible, you need to find a different solution. :-) That's what I needed to know. – ST Renegade Mar 12 '21 at 08:52
  • Well, technically you can try to track the creation time of a _branch_; by definition you'll need to skip all the branches newer than the one you check for merges. But I'm not really sure it's worth the effort. – raina77ow Mar 12 '21 at 08:58
  • @raina77ow that's the point, whether it's worth the effort. Seems it's not and we can either live with this limited solution or spend more time in the future on fixing this properly. – ST Renegade Mar 12 '21 at 09:00

1 Answers1

4

I'm afraid the way git is designed, it's not possible to distinguish, whether commit 7be9db9 (file1) has been created by b1 or b2, am I correct? So it's not possible to say, whether the parent of b1 is master or b2?

Correct, it's impossible to know if b1 branched off b2, or b2 branched off of b1. When b2 branches off of b1 they point at the same commit and are topologically identical. The history of references is not stored.

If your process needs to know this, and I'm struggling to think why, choose another process.

What you can know is the common merge base between two branches with git-merge-base. Typically this is the information you want. For example, a Github Pull Request knows what branch you want to merge into, and it can get all the commits to be merged with base..branch.

Schwern
  • 153,029
  • 25
  • 195
  • 336