Connected Lab 5

Working with Branches

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

  1. 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
  2. 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”.
  3. 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
  4. 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"
  5. 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 &
  6. In gitk, create a new view, as follows:
    1. 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.
    2. Check the “Remember this view” check box.
    3. Check the four check boxes under the “Branches & tags:” field. Click OK.
    4. If needed, switch to the new view under the View menu.
  7. 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
  8. 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
  9. You can now see your new branch listed. Change into the feature branch to do some work:
    $ git checkout feature
  10. 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
  11. 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.
  12. 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)
  13. 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.)

  14. Refresh/reload your view in gitk and take one more look at your feature branch.
  15. In your terminal session, switch back to the master branch.
    $ git checkout master
  16. Verify that you're on the correct branch.
    $ git branch

    (Note: The branch listing should have an asterisk [*] by master.)

  17. Look at the contents of the files and verify that they're the original ones from master.
    $ cat filenames
  18. Look for “master version” in the text.
  19. Refresh/reload gitk and note any changes in the upper left window.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset