Git and GitHub Reference

Prasad Rao
3 min readMar 13, 2021

One of the many revolutionary changes that has occurred in software engineering, is GitHub. It took software code repository management and collaboration to new heights. GitHub wasnt immediately apparent as a revolutionary system, atleast not for me, but within software engineering it is probably as huge as bringing down the Berlin wall was for humanity.

Source: New York Times — East German police spraying water on West Germans they broke through the wall at the Brandenburg Gate in Berlin on Nov. 11 1989

What is so different about GitHub, in comparison to other source code repositories?

  1. It is a “platform for Social Coding”. You can have a repository of code you want to share with the world, and have others build upon it. The cascading effects and value generated is much larger than the individual parts. Innovation is all about building upon others achievements. We wouldnt be walking with a smart phone that can take pictures and compute things on a 4" screen, face-timing a parent on the other side of the world, if it wasnt for innovations like the telephone, electricity, battery, computer, so on and so forth
  2. Collaborate with anyone on the planet and they can take the code and build upon it, and even contribute back to your code and improving the base code. In technical terms, they can branch your code, make changes and submit pull requests
  3. Advanced features like integrations with many 3rd party tools for code quality checks, automated testing, continuous integration and delivery and being able to push code to locations automatically when checked in

Here is a quick command reference on git:

Check version

git remote -v

Check what has changed since the last commit

git status

To initialize a new empty repository. The ‘git’ command works only within the initialized repos

git init

Prefer to use a SSH login, instead of userid / password. Here are the steps:

Step 1: Generate a public and private key pair for the machine. The files are saved in ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub by default

ssh-keygen -t rsa -b 4096 -C “emailaddress”

Step 2: Start SSH agent and add server identity to SSH

eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa

Step 3: Add public key to GitHub as trusted client

  • Copy public key to the clipboard (On Mac or Windows). View the file contents for the ~/ssh/id_rsa.pub. Copy the contents to the clipboard
  • Sometimes on a basic Linux machine you may need additional software to view the contents via cat
apt install xclip
xclip -sel clip < ~/.ssh/id_rsa.pub
cat ~/.ssh/id_rsa.pub
  • On GitHub website, add new SSH Key under SSH and GPG keys menu
  • Check that the SSH authenticates
ssh -T git@github.com

Clone a repo

git clone <<GitHub repo URL>>

Add a new remote repository

git remote add origin <<GitHub repo URL>>

How to use Git Hooks to sync with a remote git repository

Using git hooks to sync a local git folder with a server, bypassing GitHub

To setup a new remote git repository on a server, create a new folder src/projectbare.git. On the remote server:

mkdir src/projectbare.git
cd src/projectbare.git
git init — bare

Also create a src folder for the actual repo and initialize it:

mkdir src/project.git
git init

How to SSH into multiple user accounts from one machine (Mac)

Source

Here is the way it worked for me:

Edited the /private/etc/ssh/ssh_config file. Removed all previous instructions — there were only a couple of lines. I added the multiple host instructions and which userid to use

Host gitHub.com-user1
AddKeysToAgent yes
UseKeyChain yes
IdentityFile ~/.ssh/id_rsa1
ForwardAgent yes
Host github.com-user2
AddKeysToAgent yes
UseKeyChain yes
IdentityFile ~/.ssh/id_rsa2
ForwardAgent yes

Stopped the SSH Agent using the -k command

Edited the git config, to use the right SSH key for the right repo

[remote "origin"] 
url = git@github.com-user1:user1/your-repo-name.git
fetch = +refs/heads/*:refs/remotes/origin/*

For everything else…

For everything git, refer to the Pro Git book, written by Scott Chacon and Ben Straub and published by Apress. The authors have generously made this information widely available here — http://git-scm.com/book/en/v2

--

--

Prasad Rao

Software Engineer at Heart, Photography Enthusiast, Gadget Geek, Philosophy Student, Knowledge Manager