Git

Merge vs Rebase

Javier Negre @ tnwlabs

master $ git checkout -b dev

master = dev

# Commiting changes

master < dev

# Hotfixes in master

master <> dev

$ git merge

# Initial scheme

master <> dev

master $ git merge dev

master $ git merge dev

$ git rebase

# Initial scheme

master <> dev

dev $ git rebase master

master <> dev

dev $ git rebase master

master < dev

master $ git merge dev

master < dev

master $ git merge dev

master = dev

Why rebase and not merge?

Small features, developed within few days/weeks

Linear commit history

Cleaner history, without merge commits

Easier to debug ( $ git bisect )

$ git push

$ git push origin [local-branch]:[origin-branch]
# pushing current branch to origin/dev
$ git push origin HEAD:dev
# pushing dev branch to origin/dev
$ git push origin dev:dev
# same as
$ git push origin dev

Rebasing from remote changes

 

o/master < l/master

* $ git fetch

o/master <> l/master

l/master $ git push origin master x

o/master <> l/master

l/master $ git rebase origin/master

o/master < l/master

l/master $ git push origin master

o/master = l/master

Sharing a development branch

l/master $ git pull origin master; $ git checkout -b dev

l/dev $ git push origin dev

o/dev = l/dev

l/master $ git pull origin master

o/dev = l/dev

l/dev $ git rebase master

o/dev <> l/dev

l/dev $ git push origin dev x

o/dev <> l/dev

l/dev $ git push -f origin dev

o/dev = l/dev

 

o/dev = l/dev

$ git push -f (force)

Forcing a push is dangerous! You might lose code by:

  • Pushing a wrongly resolved conflict
  • Overriding a remote branch with the wrong branch

During panic mode:

  • Create branches to test with
  • Create backups

Branches are cheap!

$ git checkout -b current-branch-backup-140928

Questions?

◂ negre.co