-1

I encountered this question while building an application with multiple branches

I was expecting not only the code but also the commits I made previously in a previous branch to get merged into a new branch

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Emmanuel
  • 9
  • 1
  • No commit, once made, is ever changed - but this refers to commits as found by their raw hash IDs. The set of branches that contains any one particular commit, however, *does* change, even though the commits themselves are completely frozen. This is because it is the idea of "a branch" that is loose and fuzzy! The commits are solid; the *branches* are slippery. – torek Nov 28 '22 at 08:41
  • As VonC answered, making a true merge makes a new commit with *two* parents. This causes commits that weren't yet on the current branch to suddenly be "on" that branch, without changing any commits anywhere. So the commits that were only on branchA are now on *both* branchA and branchB (after "git switch branchB && git merge branchA", that is). – torek Nov 28 '22 at 08:43

1 Answers1

1

A merge would only create one commit on branchB with two parents.

b--b--b--M (branchB)
        /
 a--a--a   (branchA)

If you want the commit from branchA to be part of branchB, you would need to rebase branchA on top of branchB first, before merging A to B.

git rebase --onto branchB firstCommitofA~..branchA
git switch branchB
git merge branchA

That is:

b--b--b--a'--a'--a' (branchB)
                    (branchA)
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I don't really think so, the merge I tried did update everything including the commits – Emmanuel Dec 07 '22 at 09:38
  • You raised a great point there about rebase; I wouldn't lie I'm familiar with it but I don't really know what it does, can you recommend some resources that could teach me that? – Emmanuel Dec 07 '22 at 09:40
  • @Emmanuel https://learngitbranching.js.org/ has a few exercises on rebase. https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase is a good introduction. – VonC Dec 07 '22 at 09:46