Skip to content

GIT Branching and Merging

Christopher Hopkins edited this page Jun 4, 2014 · 5 revisions

Scenario: You have an existing project, and wish to add a feature (or modify it in some substantial way), but don't want it to affect the production project until you've got it complete and tested. Branching and Merging may just be what you are looking for.

If you want to see what branches currently exist, then type the following command:

$ git branch

The 'master' branch is the 'main thread' for the repository.

Step 1: Create a new branch

$ git branch foo

In this case, we are calling our new branch 'foo'. Generally, the branch name should be descriptive of what you are trying to do.

Step 2: Switch to your new branch

$ git checkout foo

Now you are in your new branch, and can implement your changes without worrying about any adverse impact to the mainline of the project.

Step 3: Add, Commit, Merge

One you have implemented all your changes, and are ready to merge then back into the mainline of the project.

$ git add . 
$ git commit -m "Comment describing changes implemented"
$ git checkout master
$ git merge foo

Now all the changes you made will be added to the mainline of the project.

Step 4: Remove the Branch when you are done

It was useful, but now that you have merged your changes back into the 'master' branch it is probably a good idea to remove the branch you created in Step 1.

$ git branch -d foo

This is some alt text!

Clone this wiki locally