Git tips: merge and rebase
merge is used to bring two (or more) branches together.
a little example:
# on branch A:
# create new branch B
$ git checkout -b B
# hack hack
$ git commit -am "commit on branch B"
# create new branch C from A
$ git checkout -b C A
# hack hack
$ git commit -am "commit on branch C"
# go back to branch A
$ git checkout A
# hack hack
$ git commit -am "commit on branch A"
so now there are three separate branches
(namely A B and C) with different heads
to get the changes from B and C back to A,
checkout A (already done in this example)
and then use the merge command:
# create an octopus merge
$ git merge B C
Example of how to rebase 7 commits in just one:
NUMBER_COMMITS_MERFE = 7
NAME_OF_BRANCH = testeBranch
1:
git rebase -i HEAD~NUMBER_COMMITS_MERFE
2:
Select the first commit to be "pick" and all the other 6, to be "merge"
3:
While tryng to do "$ git push origin NAME_OF_BRANCH" it will show up the following error:
$ hint: Updates were rejected because the tip of your current branch is behind
Therefore you need to do::
$ git push --force origin NAME_OF_BRANCH
4:
Change to develop branch:
"git checkout develop"
5:
Update branch:
"git pull"
6:
Merge with the branch that has the "NUMBER_COMMITS_MERFE" commits:
$ git merge NAME_OF_BRANCH
7:
Send the new branch to server:
$ git push origin develop
Merge two last commits:
git rebase -i HEAD~2
Push new changes:
git push origin NameOfBranch/Branch --force
From: https://www.derekgourlay.com/blog/git-when-to-merge-vs-when-to-rebase/
Keeping a clean history in git comes down to knowing when to use merge vs. rebase. Great quote describing when to use each:
Rebases are how changes should pass from the top of hierarchy downwards and merges are how they flow back upwards.
Rule of thumb:
- When pulling changes from origin/develop onto your local develop use rebase.
- When finishing a feature branch merge the changes back to develop.
----
Use git pull --rebase when pulling changes from origin