In this lab, you'll start working with branches by creating a new branch and making changes on it.
PREREQUISITES
This lab assumes that you have done Connected Lab 4: Using Git History, Aliases, and Tags. You should start out in the same directory as that lab.
STEPS
Starting in the same directory that you used for Connected Lab 4, use the git branch command to look at what branches you currently have.
$ git branch
You see a line that says “* master”. This indicates that there is currently only one branch in your repository: master. The asterisk (*) next to it indicates that it is the current branch (the one you've switched to and are currently working in). If your terminal prompt is configured to show the current branch, it also says “master”.
Before you work with a new branch, you need to update the files in the master branch to indicate that these are the versions on master so it will be easier to see which version you have later. To do this, you can use a similar version of the same way you have been creating and updating other files. Run the following command for each file.
$ echo "master version" >> filename
Stage and commit the updated files. Because these are files that Git already knows about, you can use the following shortcut command:
$ git commit -am "master version"
When you work with branches, it can be helpful to see a visual representation of what's in the repository. To do this, you can use the gitk tool that comes with Git. Start up gitk in this directory and have it run in the background.
$ gitk &
In gitk, create a new view, as follows:
In the menu, select View, and then select New View. A dialog box will open. In the dialog box, find the field named “View Name”. Type in a new name for this view.
Check the “Remember this view” check box.
Check the four check boxes under the “Branches & tags:” field. Click OK.
If needed, switch to the new view under the View menu.
You have a new feature to work on, so you now create a feature branch with the name feature. Switch back to your terminal, and in the directory, run the following command:
$ git branch feature
Notice that this command creates the branch, but does not switch to it. You can now check what branches you have and which is your current branch.
$ git branch
You can now see your new branch listed. Change into the feature branch to do some work:
$ git checkout feature
To verify that you're on the feature branch, run the following command, and observe that the asterisk (*) is next to that branch:
$ git branch
Switch back to gitk, and refresh the screen, or reload, to see what the window showing the branches, tags, and changes looks like now. You should see your new branch showing up there now.
Back in the terminal session, create a new file and then update the files in the feature branch to indicate that they are the feature branch version.
$ echo "new file" > file4.c
$ echo "feature version" >> filename (for each file)
When you're done, stage and commit your changes.
$ git add .
$ git commit -m "feature version"
(Note: If you just use git commit -am, it doesn't pick up your new file.)
Refresh/reload your view in gitk and take one more look at your feature branch.
In your terminal session, switch back to the master branch.
$ git checkout master
Verify that you're on the correct branch.
$ git branch
(Note: The branch listing should have an asterisk [*] by master.)
Look at the contents of the files and verify that they're the original ones from master.
$ cat filenames
Look for “master version” in the text.
Refresh/reload gitk and note any changes in the upper left window.