Reputation: 66531
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
Upvotes: 3
Views: 161
Reputation: 1397
Chris Johnson is of course correct in his comment, git does not record the branch name. But the other answers suggest to me, that it may be unclear how this situation may arise. Here is an example:
Lets assume work is done on main
and a feature1
branch in parallel.
$ git status
On branch main
$ echo M1 > t.txt; git add t.txt; git commit -m"M1"
$ git checkout -b feature1
$ echo F1 >> t.txt; git add t.txt; git commit -m"F1"
$ git checkout main
$ echo M2 >> t.txt; git add t.txt; git commit -m"M2"
Now you want to sync your featrue1
branch with main
so that the developer with the permission to merge to main
can do so via fast forward merge. You could do it like this:
$ git checkout feature1
$ git merge main
Auto-merging t.txt
CONFLICT (content): Merge conflict in t.txt
Automatic merge failed; fix conflicts and then commit the result.
$ vi t.txt
$ git add t.txt
$ git merge --continue
[feature1 e393a9c] Merge branch 'main' into feature1
Now the maintainer of the repo can merge to main
via fast forward:
$ git checkout main
$ git merge feature1
Updating 9adb9c3..e393a9c
Fast-forward
t.txt | 1 +
1 file changed, 1 insertion(+)
$ git log --oneline --graph -3
* e393a9c (HEAD -> main, feature1) Merge branch 'main' into feature1
|\
| * 9adb9c3 M2
* | 6b1adde F1
|/
At this point you cannot see any more to which branch M2
or F1
belong.
The trick is for the maintainer to use the --no-ff
option in this case when merging to main
. With this option we would have seen the following graph:
$ git checkout main
Switched to branch 'main'
$ git merge --no-ff feature1
Merge made by the 'ort' strategy.
t.txt | 1 +
1 file changed, 1 insertion(+)
$ git log --oneline --graph -5
* 079274c (HEAD -> main) Merge branch 'feature1'
|\
| * ba06737 (feature1) Merge branch 'main' into feature1
| |\
| |/
|/|
* | 7f7b2f0 M2
| * ab53267 F1
|/
* 5ddd4f7 M1
Upvotes: 1
Reputation: 166102
Merging branches doesn't actually merge the ref of the other branch; it merges the content of the other branch into the current one.
Upvotes: 1
Reputation: 23168
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
Upvotes: 9