Git and Github

I realised that I would benefit from using version control after a particularly messy period at work where I'd made some mistakes at some point. Git sounded like a good tool to use thanks to its integration with GitHub which would facilitate sharing work with others.

Gitting Started

First thing to do is setup Git with your name and email address this is pretty straight-forward.

git config 'My Username'
git config ''

Git under Emacs

NB I would probably benefit from learning how to use Emacs mode magit from the off, but can't get my head round it just yet.

Its pretty simple to use Git from within Emacs, edit a file, once you've something substantial in it use C-c vv to place it under version control. You'll be prompted for a system to use (respond with Git) and asked for the root directory, which you should give some consideration to. It should be the highest level directory for the work the file is associated with (or in more colloquial terms the 'project' folder) as then every other file you create for the project and wish to version control will be part of the same repository. Once you're done use C-cc to save it.

Once a file is under version control you should get in the habit of making regular commits after making substantial changes using C-c vv, entering a short but descriptive note and then saving the changes with C-cc.


You've been meticulous in saving changes to your files locally but now you want to share your code with the world (or perhaps just your colleague at the desk opposite). This is where the choice of using Git comes into its own because it is tightly integrated into GitHub the only caveat being that your collaborator(s) also need to use Git/GitHub.

Starting a Repository

Obviously you will have created a GitHub account but how do you get your local version controlled files for a given project onto GitHub? Go to your Repository page and create a new repository in this example its called new-project. You are then shown a page with three different methods of starting/adding files to the repository. Note that the URL for SSH needs tweaking if you are using multiple GitHub accounts and have configured SSH as described below. If you've created a second account called work and configured SSH to recognise the URL then you can push your existing repository with…

git remote add origin
git push -u origin master

Your files should now appear in your GitHub repository.

Ignoring Important Files

You can tell Git to automatically ignore certain files by adding file blobs to the file `.gitignore` in the root of your repositories directory (i.e. the highest level). This is useful as you can exclude temporary files that your test editor might create (e.g. Emacs leaves behind `*~` files) and if you're working with patient data then its quite likely that this shouldn't be shared in a public repository such as GitHub. The github/gitignore repository has a number of skeleton/example files for different languages including R, I modify this to include all `*.RData` files to exclude any and all R Data objects and also Emacs temporary files `*~`.

# History files
# All Data files
# Example code in package build process
# RStudio files
# Emacs tmp files
# produced vignettes
# OAuth2 token, see

Deleting Files

You might rename a file and want delete the original, how to do this in Git? The solution is to rm it and then register the new file.

git rm file1.txt
git commit -m "remove file1.txt"
git push -u origin master

To me, to you - Pushing and pulling

You've changed a file locally, how to update GitHub?

SSH Authentication

You might find putting your password in each time you want to push changes a bit of a pain, make your life easy using SSH keys (which you like use for SSH anyway). If you don't have an SSH key then create one with ssh-keygen. Then upload your public key (by default ~/.ssh/ to your GitHub account by copying and pasting it under Settings > SSH Keys > New SSH Key and giving the key an appropriate name (e.g. one that reflects the computer its for). Assuming you use Keychain to load your SSH keys when you initially login (i.e. you're prompted for the password for your SSH key it gets loaded into memory and then used each time its required) you can then push staged changes without having to enter your password.

Test your SSH authentication with…

$ ssh -T
Hi [username]! You've successfully authenticated, but GitHub does not provide shell access.
==== Multiple Accounts ====
After having made a start I realised that I wanted to keep my personal and professional work separate so I created a second GitHub account for my work email address.  But how to handle that on my local computers?  Unsurprisingly I'm not the first person to have wanted to do this.
=== Generate a second SSH Key ===
You can't use the same public SSH key with more than one GitHub account so you have to create a second to use with work.  You don't want to overwrite your existing keys though so do the following (you can substitute ''work'' for anything that is useful/memorable/relevant to you)...
<code bash>
mkdir ~/.ssh/work
ssh-keygen -f ~/.ssh/work/id_rsa

Make sure that all keys are loaded when you login by modifying ~/.bash_profile to call keychain to add them…

# Keychain
/usr/bin/keychain --agents ssh ~/.ssh/id_rsa
/usr/bin/keychain --agents ssh ~/.ssh/work/id_rsa
. ~/.keychain/$HOSTNAME-sh

Now when you next login you should be prompted for your login in password followed by each of the passwords for your keys in turn (assuming you're using something like ssh-askpass-fullscreen.

Configure SSH to use different keys

You've two SSH keys, one for each GitHub account, but how does SSH know which to use when? This is done via ~/.ssh/config by setting up a default configuration for and a second one for the second GitHub account you created (in this example

     PreferredAuthentications publickey     
     IdentityFile ~/.ssh/id_rsa
     PreferredAuthentications publickey
     IdentityFile ~/.ssh/work/id_rsa

Test that the new key works using the following (substituting work for whatever you have called your repository)…

ssh -T

Modify the project to use the alternative account

When you place a directory (/project folder) under version control Git creates the sub-directory and populates it with the necessary files. One key file is the configuration for the project .git/config and within it are details of the username and email address. You should change these to use your secondary account…

	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
        name = My Name
        email =
[remote "origin"]
	url =
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master

…this means that when an attempt is made to push to GitHub the alternative work email address is used because the target url is specified as (rather than and you have configured SSH to recognise this and use an alternative SSH key.

Learning Git/Github

Multiple Accounts

git/git.txt · Last modified: 2016/12/06 17:43 by neil
CC Attribution-Noncommercial 4.0 International Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0