Git Rebase then Merge
Whilst git’s rebase
command can be dangerous. It can also lead to cleaner commit logs than just using merge
.
git init
touch README
git add .
git commit -m "Add README"
git log --oneline
# 9e1b782 Add README
git branch alice
git checkout alice
touch A
git add .
git commit -m "Add A"
touch B
git add .
git commit -m "Add B"
git log --oneline
# a718f5a Add B
# c5849fd Add A
# 9e1b782 Add README
git checkout master
git branch charlie
git checkout charlie
touch C
git add .
git commit -m "Add C"
touch D
git add .
git commit -m "Add D"
git log --oneline
# 0c047d3 Add D
# fa7d087 Add C
# 9e1b782 Add README
git checkout master
git merge alice
git log --oneline
# a718f5a Add B
# c5849fd Add A
# 9e1b782 Add README
git checkout charlie
git log --no-merges --oneline charlie..master
# a718f5a Add B
# c5849fd Add A
git log --oneline
# 0c047d3 Add D
# fa7d087 Add C
# 9e1b782 Add README
git rebase master
git log --no-merges --oneline charlie..master
git log --oneline
# 7ddd032 Add D
# d51a0d8 Add C
# a718f5a Add B
# c5849fd Add A
# 9e1b782 Add README
git checkout master
git merge charlie
git log --oneline
# 7ddd032 Add D
# d51a0d8 Add C
# a718f5a Add B
# c5849fd Add A
# 9e1b782 Add README
If you find yourself rebasing master onto your feature branch then something has probably gone wrong. You will know if you have accidentally done this because git will politely object to pushing your newly ruined master. In short, do not force push.