# branch1 comes from master's head.
# branch1 is not integrated on master yet but we need to create branch2 that depends on branch1.
# this gist shows how to work on branch2 and then integrate it on master.
# initial situation:
#
# 1 - 2 - 3 - 4 master
# \
# 5 - 6 branch1
git checkout branch1
git checkout -b branch2
# add your commits
# 1 - 2 - 3 - 4 master
# \
# 5 - 6
# \
# 7 - 8 branch2
# branch1 may change after branch2 was created, for example if a fix is needed
# 1 - 2 - 3 - 4 master
# \
# 5 - 6 - 9 branch1
# \
# 7 - 8 branch2
# apply these changes on branch2
git checkout branch2
git rebase branch1 branch2
# 1 - 2 - 3 - 4 master
# \
# 5 - 6 - 9 branch1
# \
# 7'- 8' branch2
# branch1 must be merged on master before branch2 since the latter depends on the former.
# before merging, rebase branch1 on master.
git rebase master branch1
# 1 - 2 - 3 -------------------- 4 master
# \ \
# 5 - 6 - 9 (detached) 5'- 6'- 9' branch1
# \
# 7'- 8' branch2
git checkout master
git merge branch1
# 1 - 2 - 3 -------------------- 4 --------- 10 master
# \ \ /
# 5 - 6 - 9 (detached) 5'- 6'- 9' branch1
# \
# 7'- 8' branch2
# rebase branch2 on master.
# note that 5 - 6 - 9 are part of branch2 and not master, which means these commits will be kept when rebasing the branch.
# 5 - 6 - 9 are duplicates of 5'- 6'- 9', so you don't want them on the rebased version of branch2.
# In order to avoid such behaviour, specify a commit to start with rather than a branch.
# Specify 8+ rather than branch2 (5 - 6 - 9 - 7'- 8').
git checkout branch2
git rebase --onto master <commit>
# commit is the commit before 7'. It can be HEAD~2, the commit code for 9, the commit code for 7' with ~ appended to it.
# everything after this commit will be used
# 7"- 8" branch2
# /
# 1 - 2 - 3 -------------------- 4 --------- 10 master
# \ \ /
# 5 - 6 - 9 (detached) 5'- 6'- 9' branch1
# \
# 7' - 8' (detached)
git checkout master
git merge branch2
# 7"- 8" branch2
# / \
# 1 - 2 - 3 -------------------- 4 --------- 10 ---- 11 master
# \ \ /
# 5 - 6 - 9 (detached) 5'- 6'- 9' branch1
# \
# 7'- 8' (detached)
# master history looks like this: 1 - 2 - 3 - 4 - 5'- 6'- 9'- 10- 7"- 8"- 11
# note that if there is no conflict when merging, changes will be fast-forwarded
# 7"- 8" branch2
# / \ \
# 1 - 2 - 3 -------------------- 4 - 5'- 6'- 9'- 7"- 8" - master
# \ \ / / /
# 5 - 6 - 9 (detached) 5'- 6'- 9' branch1
# \
# 7'- 8' (detached)
Last active
October 14, 2020 11:57
-
-
Save PierreTurnbull/19391ec0fbc980784eb2b09ea1fe338a to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment