2010 Best Places to Work      Give us a call 888-856-2664
Get a Quote Today

Things a Chameleon Would Say

Web design

Git – Committing to a Branch
By: Jacob Haskins 08/28/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.

$ cd myProject $ git init

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.

$ git add . $ git commit -m 'comment goes here'

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.

$ git branch

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:

$ git branch newBranch

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.

$ git checkout newBranch

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.

$ git remote add origin git@github.com:user/repository.git

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:

$ git push origin newBranch

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.

Leave a Reply




Have a Question? Need a Quote?

Drop us a line or give us a call at 888-856-2664

Your Name (required)
Your Email (required)
Phone (required)
Your Message

What Our Clients Are Saying

John,
Can I just tell you how impressed I am with your team?! This whole process has been so great and working with you guys has made it SOOOO much easier! Thank you for all your hard work and for convincing us to choose Accella.

SO THRILLED!!

Alycia White
Kubota

Recent Posts

Apple iOS 5 Tech Talks - What Accella Took Away

I had the opportunity to attend Apple's iOS 5 Tech Talks on January 23rd in...

Building Tablet Apps for the Enterprise: The Time Has Come

Having a background in rugged mobile devices and seeing the advantages that...