Search code examples
git

How can git remember the path that a branch took if branches are only pointers?


Consider the following: a main branch and a feature branch almost ready for merging. The main branch gets updated, and the feature developer decides to merge main into their feature branch before making a pull request, to make things easier for upstream approval.

Then, the commit graph ought to look something like this, with the original branch creation in green, and the merge from main in red:

A rudimentary drawing made in paint, depicting two git branches. The second branch is connected to main in two places, both colored. The green coloring depicts the point where branch "feature" first deviates from main. The red coloring depicts a merge from main into feature.

My question is: How does git remember that the feature branch, as an abstract "branch", follows the path on the bottom, and not the path through the red line? There is the git reflog, but upstream/GitHub/any-other-user doesn't get to see this.

Is this why git gives warnings for merging from main into a feature branch?


Solution

  • You're right that branches are just pointers to commits, but commits themselves record their parents, ordered.

    So in your example, the feature branch points to a commit with two parents, the first one being feature~ and the second one being main. Then git knows it merged main into feature~ and not the other way.