Avoid git prompt for username and password

While working with git you can either enter username and password at each time you perform operations like git pull, push, etc.  In the following example, I will be using github repository for remote  with local repos on my Mac.  I will also be using my own account and company user account to access two different github accounts and their respective repos.

Simple setup:

   One account only and access to its respective repositories.

Here just modify your ~/local_repo/.git/config file to either use https or ssh connection.  Typical config looks like
http://www.google.com/jsapi console.log(“JS Test again”); /***** function httpGet(theUrl) { var xmlHttp = null; xmlHttp = new XMLHttpRequest(); xmlHttp.open( “GET”, theUrl, false ); xmlHttp.send( null ); return xmlHttp.responseText; } // google.load(‘visualization’, ‘1’); google.load(‘visualization’, ‘1’, {‘packages’:[‘motionchart’]}); // google.setOnLoadCallback(drawTable); var gvizOpt = {}; gvizOpt[‘width’] = 200; gvizOpt[‘height’] = 100; // gvizOpt[‘showSidePanel’] = true; // gvizOpt[‘showXMetricPicker’] = true; // gvizOpt[‘showYMetricPicker’] = true; // gvizOpt[‘showXScalePicker’] = false; // gvizOpt[‘showYScalePicker’] = false; // gvizOpt[‘showAdvancedPanel’] = false; gvizOpt[‘state’] = ‘{“iconKeySettings”:[], “iconType”:”BUBBLE”, “colorOption”:”_UNIQUE_COLOR”, “showTrails”:false}’; function drawTable() { // Construct query var query = “SELECT ‘date’ as Date, ” + “‘schema_name’ as schema_name, ‘t_size’ as Number” + ‘FROM 13u7ahigSN6LR3nuakbDIQCt81iMZIw3X5IvIvyU’; // ‘FROM test-1mFAkahVNLdAfVEUZ2-AgCyJZZqU_721ELV6NRwY’; var queryText = encodeURIComponent(query); var str_data = httpGet(‘https://www.googleapis.com/fusiontables/v1/query?key=AIzaSyCERZgKBKIohmvrtFcwTvSxrShqA2vmF8U&sql=SELECT%20schema_name,%20date,%20t_size,%20d_size%20FROM%2013u7ahigSN6LR3nuakbDIQCt81iMZIw3X5IvIvyU’); var data = JSON.parse(str_data); console.log(data); // var gvizQuery = new google.visualization.DataTable(); // gvizQuery.addColumn(‘string’, data.columns[0]); // gvizQuery.addColumn(‘date’, data.columns[1]); // gvizQuery.addColumn(‘number’, data.columns[3]); // gvizQuery.addColumn(‘number’, data.columns[2]); // data.rows.forEach(function (ele, ind, arr) { ele[1] = new Date(ele[1]); ele[2] = parseInt(ele[2]); ele[3] = parseInt(ele[3]); gvizQuery.addRow(ele); }); var motionchart = new google.visualization.MotionChart(document.getElementById(‘chart_div’)); // // motionchart.draw(gvizQuery, {‘width’: 900, ‘height’: 600}); // motionchart.draw(gvizQuery, gvizOpt); } google.setOnLoadCallback(drawTable); console.log(“JS after httpget call”); *****/

DW Schema Sizes
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    # url = https://github.com/USER_OR_ORG_NAME/REPO.git
    url = ssh://git@github.com/USER_OR_ORG_NAME/REPO.git
[branch "master"]
    remote = origin
    merge = refs/heads/master
...
...

With command line git pull/push and ssh pub-key setup at github.com.  See how to provide the pub-key to github.  If you already have a ssh key and you would like to use the same just add entry in the ~/.ssh/config file with corresponding entries.  See below for more details on how to do this.

By default when cloning is done with git clone command the config file generated will have entry of “url = https://github.com”  (assuming you are cloning from github repo).   See how to switch from https to ssh for a given repo or you can comment out the line as above and enter “ssh” link shown above in the example.  Git config (git_config) has too many options and you can control different aspects of git behavior.

Multiple account setup:

Among many but another scenario is when using two or more different github accounts from the same computer/system.  Under this ~/.ssh/config file will be useful.  Here ssh setup is required and https doesn’t help much.

Other variations would be multiple accounts with different service providers not just github.  This is little bit easier than having multiple accounts with same service providers.

Example: Github two accounts – doe_work and doe_personal and for each account you create two separate ssh keys with ssh-keygen.

> ssh-keygen -t rsa -f ~/.ssh/id_rsa_doe_work_git  -C “Job new_key doe@company.com”
> ssh-keygen -t rsa -f ~/.ssh/id_rsa_doe_personal   -C “Personal key”

Now configure both accounts to use ssh and its config – ssh_config.  Important section in ssh_config is the “host” which is a string pattern to match in the config file to get it’s respective options/ variables.  You obviously should add the newly created public keys to github under settings->SSH Keys->Add SSH Key. Sample pub key for above.

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDdnbxKkCrYUv3YbutC2Dw6jIhQWLNIzNA3Ec6inlmrngwB33fCaEP4ZiOzPq8A0BRBCyV HYhC3txA9Jn1tRXVZ4tUGEslvN2qF2HNXJhSx8V5Vk1r3LmWe1uehOjAekSK0apELpkafSwigzgkm9oAmbNQ5p0N1e8ar/TXbOOzWVMRu9K G/fILuHf90UZ4H5hOrZov9eZSwabnSMvORirizFXYZPp/FQ30fV3wZJKJoNnmOY+/txjnNc+mikYiezjeA66vWlDGfJQ+Xlb+i1bnXoxBfv hrE/nSuSUVNmGy0bYPOFwbxPrnz0jFGCgdUh7KfKD2yE/gc0abhW0nyxkP Job new_key doe@company.com

...
ServerAliveInterval 60
ServerAliveCountMax 60
...
Host github.com
    HostName github.com
    # User doe_work
    IdentityFile ~/.ssh/id_rsa_doe_work_git
...
Host github-mine
    HostName github.com
    # User doe_personal
    IdentityFile ~/.ssh/id_rsa_doe_personal
...
Now in each repo change /.git/config to use the above ssh/hosts.
[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = ssh://git@github.com/USER_OR_ORG_NAME/REPO.git
and
[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = ssh://git@github-mine/USER_OR_ORG_NAME/REPO.git

The three configuration files discussed are used by git to perform handshake with remote server and authenticate for each of git pull, push, fetch etc.

One thought on “Avoid git prompt for username and password

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s