git/list[1] front-page[2] threads[3] people[4] search[5] about
 

How does git follow branch history across a merge commit?

From
Steven E. Harris <seh@panix.com>
Date
Aug 28, 2009, 22:37 UTC
Message-ID
<8363c75zug.fsf@torus.sehlabs.com>
This question concerns git's representation of merge commits.

I understand that branches in git are references to commit vertices in a graph, that each of these vertices have zero or more reference edges to a parent (predecessor) vertex, and that merges create vertices with at least two parents. Walking back in time from a branch head involves traversing parent edges.

But what happens when such traversal reaches a merge commit? If one asks git to move back, say, in time by a few days, or so many predecessors, how does it know (or choose) which way to go upon reaching a merge commit?

I set up an experiment with two competing branches that require merging back together:

,----
| mkdir gitexp && cd gitexp && git init
| $EDITOR file1.txt
| git add file1.txt
| git commit -m'Initial impound.'
| $EDITOR file1.txt
| git commit -a -m'Added a second line.'
| 
| git checkout -b competition
| $EDITOR file1.txt
| git commit -a -m'Extended second line.'
| 
| git checkout master
| $EDITOR file1.txt
| git commit -a -m'Added third line.'
| 
| git checkout competition
| $EDITOR file1.txt
| git commit -a -m'Edited second line.'
| $EDITOR file1.txt
| git commit -a -m'Added an alternate third line.'
| 
| git checkout master
| git merge competition
| # Edit to resolve conflict:
| $EDITOR file1.txt
| git commit -a
| 
| git checkout 'HEAD^'
| # At this point, git picked the predecessor along branch "master".
`----

Was it just luck that "HEAD^" referred to the predecessor that came from branch "master" rather than branch "competition"?

-- 
Steven E. Harris
Next: Junio C Hamano
Message 1 of 7 in “How does git follow branch history across a merge commit?”
  1. Steven E. HarrisAug 28, 2009
  2. Junio C HamanoAug 28, 2009
  3. Junio C HamanoAug 28, 2009
  4. Steven E. HarrisAug 28, 2009
  5. Junio C HamanoAug 29, 2009
  6. Steven E. HarrisAug 30, 2009
  7. Björn SteinbrinkAug 29, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.