Git Branching

October 15th, 2011 by Regaz

When you work on a project is Good Practice to Create and Switch to a New Branch every time your are Developing or Testing something New. This permit to leave Untouched your Current Work on the Master Branch and Experiment on the New One.
Here are Shown the Basic Commands on Branching.
To Create a New Branch:

git branch testing

Switching to a Branch:

git checkout testing

There is also a Synthetic Command that allow to Create and Switch in a single Step:

git checkout -b testing

The -b flag tell Git the necessity to Create the Branch before Switching to.
Now to have a list of all your local Branches give the command:

git branch

Instead if you have some remote Branches:

git branch -r

After you have Completed your Development on the Testing Branch and you want to Merge your Code into the Master Branch.
First you Add and Commit Changes on the Current Branch:

git commit -a -m "Completed Development on the Branch"

Next you Switch back to the Master Branch and Merge the Testing Branch into the Master one.

git checkout master
git merge testing

After you Merged the Branch it doesn’t need anymore so you can Safely Delete it by:

git branch -d testing

And if you don’t need any more a Branch that still is Not Merged you can Force Deletion with the Flag -D:

git branch -D needlessBranch

To see all Branches that contain Work you haven’t still Merged in you execute:

git branch --no-merged

Instead to see all Branches already Merged in:

git branch --merged

Tags: ,