Learn Git - Branch Off Another Branch

Question Click to View Answer

This quiz will teach about creating a branch off of a branch and making a clean git history. Start by creating a directory called branch_off_branch and initializing a git repository.

$ mkdir branch_off_branch
$ git init

Run the following commands to make a few commits to master and start this example. After running these commands create a new branch called feature.

echo 11111 >> a_file
git add a_file
git commit -m "first commit"
echo 22222 >> a_file
git commit -am "second commit"
echo 333333 >> a_file
git commit -am "third commit"
$ git checkout -b feature

Make commits 4 and 5 on the feature branch in a similar manner.

echo 444444 >> a_file
git commit -am "fourth commit" 
echo 555555 >> a_file
git commit -am "fifth commit"

Create a branch off of the feature branch called sub_feature and make commits 6 and 7.

General syntax for making a branch off a branch:

$ git checkout feature
git checkout -b subfeature
echo 666666 >> a_file
git commit -am "sixth commit" 
echo 77777 >> a_file
git commit -am "seventh commit"

Checkout the feature branch and add the eighth commit in a similar manner.

git checkout feature
echo 8888888 >> a_file
git commit -am "eighth commit"

Checkout the master branch and add the ninth commit in a similar manner.

git checkout master
echo 9999999 >> a_file
git commit -am "ninth commit"

Merge the subfeature branch into the feature branch.

git checkout feature
git merge subfeature
Resolve the conflicts by deleting <<<<<<<, =====, and >>>>>>> and modifying the file however you want it.  
git add .
git commit -m "fixed merge conflicts"

What is the sequence of the commits on the feature branch after the merge.

Commits are 1, 2, 3, 4, 5, 6, 7, 8, and then the merge commit.

Use a built in graphical Git interface to view the feature branch after the merge.

gitk

The gitk user interface opens in a separate window. Study the graphical picture in the upper left corner and make sure to understand it. Pay special attention to the eighth commit. Simply close the window that opens when you run the gitk command to return the command line prompt.

Merge the feature branch into master.

git checkout master
git merge feature
Resolve the merge conflicts
git add a_file
git commit -m "resolve merge conflicts"

What is the sequence of commits in the history.

Commits 1, 2, 3, 4, 5, 6, 7, 8, 9, and two commits that revolved merge conflicts

Use the graphical Git interface to view the history after this merge and comment on the diagram.

gitk

The graphic is starting to get complicated because of the multiple branches, but study the diagram closely and make sure to understand it.

Delete the two merge commits, so we can return the project to the original status before we did the merges.

Checkout the feature branch, type in git log, and reset the branch to the commit one prior to the merge:

git checkout feature
git reset --hard commit_sha

The commit sha is the long string of numbers and letters that is next to the commit when you run $ git log.

Checkout the master branch and reset the branch to the commit prior to the merge with the same steps as well.

Rebase the subfeature branch on feature.

git rebase feature subfeature

Open a_file and resolve the conflicts Add a_file to indicate the conflicts have been resolved: $ git add a_file Continue with the rebase to complete it: $ git rebase --continue

Rebase master on top of feature.

git rebase feature master
Resolve the merge conflicts
git add a_file
git rebase --continue

Use the Git graphical tool to view the commit history.

The commit history is linear when rebasing is used. A clean commit history is one of the major advantages of rebasing.