2

Sorry if this is a stupid question with just a yes/no answer, but if I understand correctly, in git a branch is just a pointer to a commit. Doesn't this imply that once you've merged two branches, git doesn't know which one pointed to which set of commits?

Before

A---B---C---D---E    <- X
 \                
  1----2----3----4   <- Y

After

A---B---C---D---E--M  <-X & Y
 \                /
  1----2----3----4
Benjol
  • 63,995
  • 54
  • 186
  • 268
  • After a single merge only one of the branches would point to M. It is possible to arrange the topology you illustrated, but it would involve another merge, a reset, or a `git branch --force`, etc. But “which one pointed to which set of commits” is not something that Git records. I have read that Mercurial records the branch name in a commit, but Git does not do this (except when it records the name of a merged branch in the commit message of a merge commit). To record the “name(s)” a commit had, you might use extra tags, branches, or *git notes* (in Git >= 1.6.6). – Chris Johnsen May 22 '10 at 21:52

2 Answers2

9

If I recall correctly you merge a branch into another one, like feature1 into master, so master now points to merge-commit but feature1 still points to where it pointed before.

Edit by Jefromi: This is correct. The pictures should look like this:

git checkout branch X

    A---B---C---D---E   branchX (HEAD)
     \                
      1----2----3----4   branchY

git merge branchY

    A---B---C---D---E--M  branchX (HEAD)
     \                /
      1----2----3----4  branchY
Cascabel
  • 479,068
  • 72
  • 370
  • 318
Arkaitz Jimenez
  • 22,500
  • 11
  • 75
  • 105
  • Yup - you merge one branch into another. So whatever branch you were on when you issued the merge command will be the current branch after the merge. – bergyman May 21 '10 at 22:08
  • @Jefromi ok, so what happens if I do a rebase then merge, as suggested here (http://stackoverflow.com/questions/804115/git-rebase-vs-git-merge/804178#804178)? – Benjol May 25 '10 at 04:39
  • @Benjol You can have a look at the Git community book at http://book.git-scm.com/index.html if you're interested in its inner workings, but in short, neither rebasing nor merging changes the position of any branch other than the current branch. Whatever git is doing, it will not delete or change ANY existing commit (unless you run stuff like `git gc`, etc). All it does is create new commits (even for rebasing), so there is no need to change other branches. – Interarticle Oct 31 '11 at 14:34
1

Merging branches doesn't actually merge the ref of the other branch; it merges the content of the other branch into the current one.

hasen
  • 161,647
  • 65
  • 194
  • 231