Getting Git

A comprehensive video course from git init to Git Master.

init: git rebase

In this video we'll learn how to use git rebase.

There will be times when the branch you are working on falls behind the one you branched from. Normally because other changes were finished and merged after you created your branch.

While you could use git merge to bring in the recent commits, using git rebase is more appropriate.

git rebase will bring your branch back up to date with another branch and reapply any commits you've made on top.

Effectively, git rebase will make it as if you just created the branch and did your work.

Let's demonstrate this by creating a stale branch.

I'll do so by checking out a new branch and passing it an additional argument of an older point as covered in the Master video for git checkout.

In this case, I'll reference an older commit by using HEAD~2.

Now I'll make some changes by creating and committing File 3.

If I run git log we'll see this branch has formed it's own base and is missing commits from master.

We can bring get all of the new changes from master as well as keeping the new changes with git rebase.

In this case, by running:

git rebase master

From the output we see Git first rewinds our branch to a point where its commit history was similar to master.

Then it fast forwarded our branch with the recent changes on master.

Finally it took any commits from our branch and reapplied them on top.

If we run git log again, we see now that our branch now contains all the commits from master and is one commit ahead of master.

Similar to git merge, it is possible to receive conflicts when you rebase a branch.

The process for resolving these conflicts is the same as we learned in Master: git merge.