Tag Archives: Source Control

Branching with git and github

Essentially you want to isolate code from the current work you are working on. One of the ways to do this is to branch the code. This will create a separate “place” for you to work and you can merge these changes back into your existing code base.

Here is how to do this with git:

  1. Change directory to your repository code.
  2. Branch the code from the code you are working on with git branch new-branch-name.
  3. List of branches with git branch.
  4. Switch to the newly created branch in step 2 with git checkout new-branch-name.

There are other options like creating a fresh new branch, merging and deleting branches. Consult the links in the reference section.

Committing the branch to github
To push the changes to git use git push origin new-branch-name.

References:

  1. Git branch manual page about branching.
  2. Git on-line book reference about branching.
  3. Github reference about branching.

Different git user for project

When using git you typically set your user name and email at a global level.

This is done like so:

  • Name – git config –global user.name “Binary Codifier”
  • Email – git config –global user.email “[email protected]

This creates entries in your ~/.gitconfig file, which looks like this:

[user]
name = Binary Codifier
email = [email protected]

However, if your like me and want to use a different user name and email when working on other projects, then you don’t want to keep changing the global config.

Luckily for us the git creators thought about this. You can configure your user name and email on a project by project basis.

Here is how it’s done:

  • Change directory to your project.
  • Name – git config user.name “Your Name”
  • Email – git config user.email “Your Email”

Getting started with github

Github is a source code repository hosting service. It uses git as the version control system and it is free for open source projects.

You need to register, thereafter you can create or fork a repository, follow the instructions in the help section.

Creating SSH keys
You can skip this step if you want to because you can cache your github password for a period of time (see set up git, scroll down to password caching).

However to be more secure and instead of having to specify your login credentials each time you want to commit to a repository or after password caching has expired, it is really helpful using SSH keys. Here is how you would do this with Linux:

  1. In your shell, create a .ssh directory mkdir -p ~/.ssh, then change to it cd ~/.ssh.
  2. ssh-keygen -t rsa -C “your-email-address-here” (this will ask your for a filename and password).
  3. You should have 2 files with the same name but one has a “.pub” extension (these are your private and public keys respectively).
  4. Copy the contents of your .pub key to your clipboard less ~/.ssh/your-key-file.pub and copy all the contents of the file. Or if you’re paranoid about missing anything out then use xclip -sel clip < ~/.ssh/your-key-file.pub (you will need to install xclip).
  5. Login to github, choose “account settings”, scroll down to “SSH Keys”, then give a name for your key and CTRL+V to copy the contents of your clipboard into the key field.
  6. Test with ssh -T [email protected] (leave [email protected] as is), type in the password used in step 2 and you should receive a message along the lines of “Hi binarycodifier! You’ve successfully authenticated”.

Creating a repository
This can be achieved on the server side, login to github and simply follow the create repository instructions. The server side repository creation is really easy. However if you want to create a repository from your machine, you will need to use the github API, see the repo create option.

Clone your repository locally
In order to work on repository code, you will need to have a copy of the code locally on your computer from the repository you created – this is called cloning.

  1. Go to one of your repositories on github.
  2. At the top, choose the “Code” tab, you should then see “Zip, HTTP, SSH and Git Read-Only” options.
  3. For normal password authentication choose “HTTP”, for SSH key based authentication choose “SSH”. Copy the URL next to the option you chose.
  4. Create or change to a directory on your local computer.
  5. On your local computer use git clone github-repository-url you copied in step 3.
  6. You should be able to see a new directory matching your github repository on your computer.

Updating repository
On your local computer you will be making the changes. Thereafter you will push these changes back to the github server. Follow these steps:

  1. Change directory to the copy of your repository directory on your local computer.
  2. Update file(s), save and then add git add . (note the dot).
  3. Commit the files with a message of the change git commit -a -m “updated X”.
  4. Push the changes to the github server git push.
  5. Browse your github repository and check to see that the changes did indeed get pushed.

References:

  1. Github – Wikipedia reference.
  2. SSH Keys – Wikipedia reference about SSH communication.
  3. Github generating SSH key – creation and testing SSH key with github.
  4. Git manual – on line manual for git.