January 1, 2021

#66 - Introduction to Git and github

Version control is critical in software development. One of the most popular products amongst programmers is Git and the web interface github. In this post a quick introduction and how to get started using both of them are described.

Git is the program installed in the local computer to download/upload projects and files from/to the web user’s account on github. It can be downloaded from the website https://git-scm.com/downloads

github is the web interface that enables the programmers from all around the world to interact via software. Besides version control of your own projects, it is also possible to copy the code from someone else’s, work on this code in your local computer and even to propose improvements to the original creator. It has become a great tool for small and large team projects. 

Create an account on www.github.com

The drawing below shows a simplified flow diagram with some of the most common operations and the interaction between the local computer with the web interface.
  • Forking a repository from another user: find the desired repository from another user and click on the Fork button. This will create a copy of this project in your account.
  • Establishing a connection between local computer and github: Most of the commands can be sent from the command prompt. At the beginning is good to check the settings on the computer by sending the commands below: 
$  git config --global user.name     //get username
$  git config --global user.name “myName”    //set username
$  git config --global user.email    //get email
$  git config --global user.email “myemail@1.com"   //set

The connection between the local computer and github can be done by the HTTPS or the SSH protocols. 

To establish a SSH connection, open the command prompt and send the commands:
$ ssh-keygen -t rsa -b 4096 -C "myemail@1.com  //Enter the passphrase and password. It will be generated a public/private rsa key pair, that will be stored on (/c/Users/myUser/.ssh/id_rsa):
Your identification has been saved in /c/Users/myUser/.ssh/id_rsa
Your public key has been saved in /c/Users/myUser/.ssh/id_rsa.pub
The key fingerprint is:
SHA256: *****mySHA256***** myemail@1.com

$ eval $(ssh-agent -s)

$ ssh-add ~/.ssh/id_rsa
Identity added: /c/Users/myUser/.ssh/id_rsa (myemail@1.com)

$ clip < ~/.ssh/id_rsa.pub    //copy the certificate to paste on github

The command clip above, will copy the SSH certificate. Then paste it on the github webpage personal settings > SSH and GPG keys. 

Click on the “New SSH key” button and paste the certificate. When successful, a green key icon will appear and the list of SSH keys associated with the github account will be updated.  

$ ssh -T git@github.com     //testing the SSH authentication
Hi myUser! You've successfully authenticated, but GitHub does not provide shell access.

In case of failed connection, the message below will be displayed:
git@github.com: Permission denied (publickey).
  • Cloning: Make a local copy on your machine from your github account.
> git clone https://github.com/userName/repositoryName.git
  • Pushing modifications from local machine to the github repository:
> git init
> git add .
> git commit -m "my comment about this new version..."
> git remote -v 
If the origin displayed from the previous command is not the desired one then: 
> git remote add origin git@github.com:myUser/Test.git
> git push origin master
  • Deleting an old repository:
Click on the repository > Settings > Scroll down and click on Delete this repository (inside Danger Zone)
  • Other commands:
> git help
> git show
> git status
> git log
> git config -l   
> git config –e

No comments:

Post a Comment