TIL — git merge, rebase and cherry-pick

This post will show you some ways you can manage commits from different branch, git provides us with so many options but we will focus only on merge, rebase and cherry-pick. I won't explain to you what's behind the wheels about these command because I also not shure how they works.

All I want to show you is the effect of these command. Each command will be run with following commit history :

Initial commits

* 11ef50f (HEAD -> master) Add kafka connection tests
| * 24b31fd (add-rabbitmq-connection) Add rabbitmq connection tests
| * b46f12f Update rabbitmq credentials
| * 0dd5b92 Add scafold for rabbitmq connection
|/
* 7d4cd7b Update config for kafka connection
* 46c6632 Add initial setup for kafka worker
* f101bc9 Initial commit

As you can see from above history, we have two branch, master and add-rabbitmq-connection. All we want is move all changes that happens in add-rabbitmq-connection branch to master.

Git merge

git merge add-rabbitmq-connection -> conflict -> resolve -> git add . -> git merge —continue

*   8ca2a4d (HEAD -> master) Merge branch 'add-rabbitmq-connection'
|\
| * 24b31fd (add-rabbitmq-connection) Add rabbitmq connection tests
| * b46f12f Update rabbitmq credentials
| * 0dd5b92 Add scafold for rabbitmq connection
* | 11ef50f Add kafka connection tests
|/
* 7d4cd7b Update config for kafka connection
* 46c6632 Add initial setup for kafka worker
* f101bc9 Initial commit

With git merge, it will create a new commit on top current commit in master containing all changes from add-rabbitmq-connection branch.

Git rebase

git rebase add-rabbitmq-connection -> conflict -> resolve -> git add . -> git rebase —continue

* 996fbe5 (HEAD -> master) Add kafka connection tests
* 24b31fd (add-rabbitmq-connection) Add rabbitmq connection tests
* b46f12f Update rabbitmq credentials
* 0dd5b92 Add scafold for rabbitmq connection
* 7d4cd7b Update config for kafka connection
* 46c6632 Add initial setup for kafka worker
* f101bc9 Initial commit

git rebase will try to insert and fit all changes from source branch to target branch. One advantage of using rebase is you can see and access your commit from source branch.

Git cherry-pick (one commit)

git cherry-pick b46f12f -> conflict -> resolve -> git add . -> git cherry-pick —continue

* a3640ed (HEAD -> master) Update rabbitmq credentials
* 11ef50f Add kafka connection tests
| * 24b31fd (add-rabbitmq-connection) Add rabbitmq connection tests
| * b46f12f Update rabbitmq credentials
| * 0dd5b92 Add scafold for rabbitmq connection
|/
* 7d4cd7b Update config for kafka connection
* 46c6632 Add initial setup for kafka worker
* f101bc9 Initial commit

The name says it all :)

Git cherry-pick (range commits)

This functionality only works for Git 1.7.2+ but you need to be aware of the consequence of cherry-picking for future merge as stated here.

git cherry-pick 0dd5b92^..24b31fd -> conflict -> resolve -> git add . -> git cherry-pick —continue

^.. used so the first commit is included. If use use .. the first commit will be excluded.

* a24c15b (HEAD -> master) Add rabbitmq connection tests
* fdd75ca Update rabbitmq credentials
* 06091ac Add scafold for rabbitmq connection
* 11ef50f Add kafka connection tests
| * 24b31fd (add-rabbitmq-connection) Add rabbitmq connection tests
| * b46f12f Update rabbitmq credentials
| * 0dd5b92 Add scafold for rabbitmq connection
|/
* 7d4cd7b Update config for kafka connection
* 46c6632 Add initial setup for kafka worker
* f101bc9 Initial commit