Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading