Git – Committing to a Branch

August 28th, 2010

Having VERY little exposure to Git, I admit I was a bit nervous learning that I would need to familiarize myself with Git for a recent project. Looking back, I realize my task was quite simple. Once I had finished developing a project, I needed to create a branch off a master Git repository and commit the project files to the new branch. Here are the steps to take to commit to a branch as well as a few other things I learned along the way.

The place to start is of course in the help section of GitHub. You will need to install Git, generate a keypair and set you username and email into Git. Look to the help section for step by step instructions. Once you have that setup, you’re ready to start working with Git.

[code]
$ cd myProject
$ git init
[/code]

First, we have to setup our project directory to use Git. The code, ‘git init’, will do this for us. For my situation, I did not want to touch the master branch. I only wanted to work in a separate branch. I did not even want to fetch the files from the master repository.

[code]
$ git add .
$ git commit -m 'comment goes here'
[/code]

This code will add all the files in the directory to be committed and then commit those files with a comment. This will add the files to the master branch. However, I don’t want to change anything on GitHub in the master branch. Not to worry though, we haven’t connected to the remote URL. So, at this point it is only committing locally. Now that everything is established locally, let’s look at our branches.

[code]
$ git branch
[/code]

This will show you all the branches you have created, as well as list the branch you are working in with an asterisk. For now, it should only show ‘* master’. We are currently working in the ‘master’ branch. My instructions were to not touch the master branch. So, let’s create a new branch:

[code]
$ git branch newBranch
[/code]

You have now created a new branch named, ‘newBranch’. If you now type ‘git branch’, it will list ‘master’ and ‘newBranch’. However, you’ll notice that ‘master’ still has the asterisk next to its name. We are still working in the ‘master’ branch. To switch to our new branch, use the ‘checkout’ command.

[code]
$ git checkout newBranch
[/code]

Now we are working in the right spot when we commit our project. We are going to be committing our project to a remote location, so our next step is to define that location.

This will add our remote location so when we commit our changes, it not only commits them locally, but also to the server. You can find the URL to use at the GitHub repository page. The word ‘origin’ is the name of the remote location that we are adding. So, in the above example, we are adding the remote location ‘git@github.com:user/repository.git’ and naming it ‘origin’. And now, the final magic command to get our project into GitHub:

[code]
$ git push origin newBranch
[/code]

Remember, ‘origin’ is the name of our remote location that we have saved, and ‘newBranch’ is the name of the branch that we are creating/pushing to. The project is now on GitHub in a new branch that we created just for the project. If you have any tips or suggestions for achieving this task, please do share.

Jacob Haskins

Jacob has always had an interest in learning and problem solving. Whether working with Objective C, PHP, Actionscript, FLEX, JavaScript or any other language, he finds that there is always something new to learn. He enjoys development projects most when getting to use new technologies.