Merges / Rebasing

git rebase <ref>

Rebasing replays the diff of commits on to a certain point. The key difference between merges and rebasing, is that with rebasing the commit hash references are changed; Merge commits keep their same refs.

Local changes

Say we are working on a feature branch called branch off of origin/develop, and we’ve made some commits to it. We haven’t pushed this branch to origin, but new commits have been made since to origin/develop that we want.

So we can get those commits on our branch

$ git fetch
$ git checkout branch
$ git rebase origin/develop

Take this example from our workshop repository

$ git checkout master
$ echo hi > important_file
$ git add important_file
$ git commit -m 'added important file'
$ git checkout -b super_branch
$ mv afile file
$ git add file
$ git rm afile
$ git commit -m 'rename to file'
* 335d198 (HEAD -> super_branch) rename to file
| * 5fd910b (master) added important file
|/  
* 87b5eff (branch_a) forth commit
* c890001 thirdcommit
* 16797ce (tag: first_commit_tag) first commit m8

Now we want that important_file in super_branch, we can rebase our work on top of master:

$ git checkout super_branch
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: rename to file

When we look at the graph of commits, we see our branch is now ahead of master, and the commit hash refs have changed

$ git log --oneline --decorate --all --graph
* 7c39e9e (HEAD -> branch) rename to file
* 5fd910b (master) added important file
* 87b5eff (branch_a) forth commit
* c890001 thirdcommit
* 16797ce (tag: first_commit_tag) first commit m8

Now we can rebase our changes on top of another branch, lets look into interactive rebasing.