Adding Multiple GitHub Accounts for SSH Communication

You may have multiple github accounts. In this post, we cover setting up ssh authentication for multiple github accounts from one computer, so pushing code to and from different github accounts is an easy process

Step 1. Generate the extra key in .ssh directory and copy it into clipboard

ssh-keygen -t rsa -C "your_email@youremail.com"
pbcopy < ~/.ssh/id_rsa_new_key.pub

In the line 1, it generates new ssh key into file(for example, in ‘id_rsa_new_key.pub’) and in line 2, it copies the new key into clipboard

Step 2. Paste new key into the GitHub account
In your account on the GitHub.com, go to the ‘Account Settings’-> ‘SSH Keys’ and past the new key

Step 3. Update ssh config (~/.ssh/config)
On you computer, in the ~/.ssh directory add the entry

Host github.com-kapasoft-views
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_new_key
Host github.com-kapasoft-scripts
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_new_key2

Here the ‘github.com-kapasofts-views’ is part of the git remote repo url(i.e. git@github.com-kapasoft-views:user/repo.git). The ‘IdentityFile’ is file created in Step 1. When you are going to push the content, ssh looks up the url of remote repo and compares it to the Host ‘github.com-kapasofts-views’ in .ssh/config file to find the action HostName(i.e. github.com) along the ssh key to use for authentication

Step-4. Add the key to ssh cache

ssh-add ~/.ssh/id_rsa_new_key

Step-5. Update the git remote repo accordingly in the git directory

git remote add origin git@github.com-kapasoft-views:github-account/remoteRepo.git

Here the ‘github.com-kapasoft-views’ part is the same specified in the ~.ssh/config file. You can also manually edit it in the repository .git/config file

Issues

1. ERROR: Permission to gitHub-account/remoteRepo.git denied to gitHub-account.
fatal: Could not read from remote repository.
The step 3 was missed or it was needed to be do again:

ssh-add ~/.ssh/id_rsa_new_key

This solved the problem. Important to note that this keep happening often time on Mac Lion where i have to re-add the key to the ssh-key cache often time

2. ssh: Could not resolve hostname nodename nor servname provided, or not known
I am not sure the cause, but after authenticating directly with github as following, the error went away:

ssh -T git@github.com

Useful links:

  • https://gist.github.com/jexchan/2351996
  • https://help.github.com/articles/generating-ssh-keys

Locally Untracked Files in Git While Making a Grails Plugin In-Place

At my current workplace, the grails application is divided into application and an extra plugin. To avoid package, maven-install every single time new change made in plugin that need to be seen in application, I decided to make this plugin in-place. To make it in-place plugin, i had to update BuildConfig.groovy as follows:

...
grails.plugin.location.nameOfPluginPlugin = '../../pluginLocation'
...
plugins {
    ...
    //uncomment plugin dependency
}
...

At the same time, i don’t want these changes go into others developers local repos, so i wanted to untrack BuildConfig.groovy file. Here are steps to untrack file from git repo.

Ignoring File in Git

To ignore file globally,here are steps

Step-1: Create global git ignore file, not tracked in repository that is user-specific

git config --global core.excludesfile pathTo/.gitignore_global
 

Note: You can find different sample git ignore files per technology here

Step-2: Added BuildConfig.groovy in my local global git ignore file as follows

...
/relative/pathToApp/BuildConfig.groovy
...

This will keep it ignored, but we still need it make it be untracked before it can be ignored

Step-3: Untrack the file itself since it is already tracked

git rm --cached BuildConfig.groovy

This makes the file to be ignored and also untracked, however. By pushing to the shared repo others will make this file to be untracked as well, which we don’t want

Ignoring vs Untracked

Ignore will only apply to untracked files. So, in our case, where we need the file to still be tracked, the solution of ignoring described above will not work. Instead, we set the file to be assumed unchanged by Git:

git update-index --assume-unchanged appname/grails-app/conf/BuildConfig.groovy  

By ‘assume unchanged’, the file is ignored and no changes appear in git repo. This makes the grails plugin in-place only for me since the changes are not tracked anymore and ,thus, pushed into the shared main repository

Useful links:

Moving from Mercurial and BitBucket to Git and GitHub

While skeptical at first, after trying it out github has won my favor as my favorite version control. This post records steps made to transfer from Mercurial and BitBucket to Git and GigHub while keeping history

Step 1 – Set up Password caching following article ‘Set Up Git’

Step 2 – Install git-hg module. This allows to clone a mercurial repository from BitBucket and then converts it to git repository that you can push into the GitHub.com

Followed article ‘Moving Your Mercurial To Rep To Git’

Step 3 – Clone the existing GitHub repository and push it to GitHub as following:

git-hg clone http://bitbucket.org/some/repo name-git-repo
cd name-git-repo
git remote add origin http://github.com/some/repo.git
git push origin master

Potential Issues

1. “fatal: remote part of refspec is not a valid name in .”
This happens when you created new branch that you would like to push into github repo while the github repo doesn’t contain the new brach. Solutions:

git config push.default current

This will change default setting for ‘push’ to create new branch in the github repo if doesn’t exist before pushing. Here are other options:

  • nothing : Do not push anything
  • matching : Push all matching branches (default)
  • tracking : Push the current branch to whatever it is tracking
  • current : Push the current branch

Useful Links