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

...or...

$ cd rest-server $ git checkout -b jack

Your new branch name should be properly evocative of you though anything will do. Here, we're using "jack". Now, whatever has already been modified, but not yet committed will be that way in the new branch (and must therefore be added/staged, committed, etc.).

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

Once you've finished with a branch, merged your changes into master, etc., it's desirable to remove the (temporary) branch:

(To remove a branch can only be done if there's nothing done or if it's already been pushed to (and merged with) its remote.)

$ git branch --delete jack # removes local branch named jack $ git fetch origin --prune # removes (all) obsolete remote-tracking branches (including jack)

However, here's how it really goes for me:

$ git branch master * jack $ gs $ git checkout master Switched to branch 'master' Your branch is up to date with 'origin/master'. $ git branch --delete jack error: The branch 'jack' is not fully merged. If you are sure you want to delete it, run 'git branch -D jack'. $ git branch -D jack Deleted branch jack (was 53f51ed). $ git fetch origin --prune remote: Enumerating objects: 6, done. remote: Counting objects: 100% (6/6), done. remote: Compressing objects: 100% (2/2), done. remote: Total 11 (delta 4), reused 4 (delta 4), pack-reused 5 Unpacking objects: 100% (11/11), done. From github.../containers 3c12e5b..aa00978 master -> origin/master $ git branch * master