Learn Git - Rebasing

Question Click to View Answer

What is rebasing?

Taking all the changes that were made to one branch and reapplying them on another.

What is an advantage of rebasing?

There is a cleaner commit history compared to merging.

What is the main difference between rebasing and merging.

Rebasing replays changes from one line of work onto another in the order they were introduced. Merging takes the endpoints and merges them together.

What is the main disadvantage of rebasing? When should rebasing not be used?

Rabasing should not be used when commits have been made public and people have based work off these commits. To play it safe, do not rebase commits that have been pushed to a public repository.

Create a directory called git_rebase and initalize a Git repository.

$ mkdir git_rebase
$ cd git_rebase
$ git init

Make a file called some_example and make three small changes with three commits called "commit 0", "commit 1", and, "commit 2".

$ touch some_example
$ vim some_example
Enter the text: This is the 0th line
$ git add some_example
$ git commit -m "commit 0"
Enter some more text: This is the 1st line
$ git add some_example
$ git commit -m "commit 1"
Enter some more text: This is the 2nd line
$ git add some_example
$ git commit -m "commit 2"

Create a branch called experiment and make a commit called "commit 3".

$ git checkout -b experiment
Enter some text: This is the 3rd line
$ git commit -am "commit 3"

Check out the master branch and add a change on line 20. Commit the changes and call it commit 4.

$ git checkout master
Enter some text on line 20: Sup bro
$ git commit -am "commit 4"

What is the sequence of commits on the master branch?

Commit 0, commit 1, commit 2, commit 4 (commit 3 was made on the experiment branch) You can see this commit history with the following command:

$ git log

What is the sequence of commits on the experiment branch.

Commit 0, commit 1, commit 2, commit 3 (commit 4 was made on the master branch)

Rebase master on the experiment branch.

$ git checkout experiment
$ git rebase master
<notice that you are not on any branch when in rebase mode>
The code will tell you that you have merge conflicts.  Open the some_example file and eliminate the merge conflicts (must delete all the <<<<< and ==== that has been added).  After the merge conflicts are resolved, add the file.
$ git add some_example
Continue the rebase:
$ git rebase --continue

The final command should complete the rebase and the changes will be applied to the experiment branch.

What is the commit history on the experiment branch after the rebase.

Commit 0, commit 1, commit 2, commit 4, commit 3.

Are the SHAs the same for the commits before and after the rebase?

The SHAs for commits 0, 1, and 2 are the same before and after the rebase. However, the SHAs for commits 3 and 4 are different after the rebase. If other developers are relying on the old SHAs of 3 and 4 (i.e. the pre-rebase SHAs), it will get messy and that is why Pro Git does not recommend rebasing after code has been pushed to a public repository.

Make the master branch the same as the experiment branch using rebasing.

$ git rebase master

This command will not have any conflicts, so the rebase is completed automatically.

Delete the last commit on master.

$ git reset --hard HEAD~1

Make the master brach the same as the experiment branch using a more conventional merge.

$ git checkout master
$ git merge experiment

We want to start another project with a master branch, feature branch, and a subfeature branch that is a branch off the feature branch. Run the following commands to get this started.

cd ..
mkdir complicated_rebase
cd complicated_rebase
git init
echo 11111 >> main
git add main
git commit -am "commit 1"
echo 22222 >> main
git commit -am "commit 2"
git checkout -b feature
echo 33333 >> feat
git add feat
git commit -am "commit 3"
echo 44444 >> feat
git commit -am "commit 4"
git checkout master
echo 55555 >> main
git commit -am "commit 5"
echo 66666 >> main
git commit -am "commit 6"
git checkout feature
git checkout -b sub_feature feature
echo 77777 >> sub_feat
git add sub_feat
git commit -am "commit 7"
echo 888888 >> sub_feat
git commit -am "commit 8"
git checkout feature
echo 99999 >> feat
git commit -am "commit 9"

I strongly recommend drawing out the SHAs and branches (like they do in the book Pro Git) for the upcoming exercises.

Replay the changes from the sub_feature branch on master.

$ git rebase --onto master feature sub_feature

What commits do the branches have after the command from the previous question.

Master (unchanged): 1, 2, 5, 6 feature (unchanged): 1, 2, 3, 4, 9 subfeature (changed): 1, 2, 5, 6, 7, 8

subfeatures's commits were added to the end of master

Fast forward the master branch. What is master's commit history list after the fast-forward.

$ git checkout master
$ git merge sub_feature

This is master's commit history after the fast forward: 1, 2, 5, 6, 7, 8

Rebase the feature branch onto the master branch.

The general syntax is:

$ git rebase [basebranch] [topicbranch]

$ git rebase master feature

Fast forward the master branch.

$ git checkout master
$ git merge feature