Git Lifecycle: Create Your Own Branch

Once you've cloned the master copy of rest-server, create your own branch. This is a sandbox where you can play. Anything you do there can be broken without harming in any way the main development branch. See Git Scheme on what you can do in what branch. It's important to respect this scheme absolutely.

Create your own branch using the following command. We're assuming for demonstration purposes that the project is rest-server.

$ cd rest-server $ git branch jack

Your new branch name should be properly evocative of you though anything will do. Here, we're using "jack". The output from this sequence is something like what's below. Any time you want to know what branch you're dealing with, just use this command.

$ git branch * master jack

To change the current branch you're dealing with, use the following:

$ git checkout jack $ git branch master * jack

What's just happened?

You've just refocussed HEAD on a new branch named jack. Head is the tip of a potential chain of modifications beyond the point at which you created the new branch. In this case, the master branch hasn't disappeared; it's still there. But all modifications from now on will go into the jack branch.

At this point, if you modify Xyz.java, then switch back to the master branch, you''ll see that the changes you made to that file are not reflected:

$ gvim Xyz.java (make some changes) $ git checkout master $ git branch jack * master $ gvim Xyz.java (those changes don't appear)

Cleaning up

Later, if you wish to eliminate your new branch, switch back to its parent (more likely a named branch in practice rather than master as show here), then delete the new feature branch. Of course, if you didn't push your changes to another branch, you'll lose them. Here is a command sequence:

$ cd rest-server $ git branch master * jack $ git branch new-feature $ git checkout new-feature $ git branch master jack * new-feature ...do work on new feature, push it to jack branch, etc.... $ git checkout jack $ git branch master * jack new-feature $ git branch -d new-feature $ git branch master * jack