10/16/2015 - 3:16 PM

Git tips: merge and rebase

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:

NAME_OF_BRANCH = testeBranch


Select the first commit to be "pick" and all the other 6, to be "merge"

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

Change to develop branch:
"git checkout develop"

Update branch:
"git pull"

Merge with the branch that has the "NUMBER_COMMITS_MERFE" commits:
$ git merge NAME_OF_BRANCH 

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

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