Git and GitHub – an Introduction
In previous article we have been gabbing about the Version Control Systems and its different types as well fundamental approaches of Git and GitHub. Version Control Systems are a kind of software tools that help development teams to manage changes to source code over time. It keeps track of every modification to the code in a special kind of database.
Git is one of the top Version Control System, a tool to manage your source code history. Along with this, GitHub is a web-based hosting service for Git repositories. Better to visit the preceding post to get an overview about Git and GitHub, because they work on top of the Repository and Working Copy though not the same thing.
At this point in this post we will walk through some hands on activities like –
- Connect an Ubuntu VM using PuTTY client
- Install Git and set up your GitHub account
- Execute some most popular commands in Git
- Push all the files from the local repository to GitHub
Meanwhile, if required, then you can visit some previous articles to know a bit more about the following topics –
Pre-requisites
Before proceeding further, need some pre-requisites to go ahead to deploy all the source code files from the local repository to GitHub, like –
- Azure subscription, if you don't have an account then sign up for a free Azure account - https://azure.microsoft.com/en-gb/free/
- A running Ubuntu Linux VM
- PuTTY client to be used as the SSH client
- GitHub account, if you don’t have an account then sign up - https://github.com/
STEP – 1: Validate the existence of an Ubuntu VM
It is essential to exist an Ubuntu Linux Azure Virtual Machine (VM) to accomplish this demo task, login to the Azure portal https://portal.azure.com/.
On the left Hub menu, click All resources and select the existing Ubuntu virtual machine, verify the VM is either running or stopped, if it is in stop mode, i.e. deallocated then start the same.
STEP – 2: Fetch the connection details of Ubuntu VM
Next, required to connect the VM, you can go with either SSH key or PuTTY client depends on the configuration and setup done with Ubuntu VM.
I am moving ahead with PuTTY client, click the Connect button from the menu bar to launch the connection details.
Here you can see a new blade as Connect to virtual machine appeared, copy the account details which exist under the Login using the VM local account, in my case - ssh demoadmin@40.117.153.69.
STEP – 3: Connect the VM using PuTTY client
Since the Ubuntu Linux VM is configured (password-based) in such a way to connect using the PuTTY client, so open up PuTTY, and in the Session page, submit the host name into the Host Name box, the same we copied earlier.
For example, in my case, it was - ssh demoadmin@40.117.153.69, but need to submit only demoadmin@40.117.153.69, exclude the ssh prefix and then, under Connection type, select SSH and click Open.
Once the SSH session has been established, promptly, it will ask password for the connecting server, enter the administrator password you specified during provisioning the Ubuntu VM.
Post authorization, you will be connected with the Ubuntu Linux 18.04.1 LTS Virtual Machine (VM), happy to go ahead.
STEP – 4: Install Git
Mostly Git used to be installed, better to check the version of Git by executing the following command in the terminal.
Git --version
It will display the current installed version, but if not installed, then you can install Git by executing the following command.
sudo apt-get install git
Quickly the package will be starting to install, though in my case the newer version was already installed.
STEP – 5: Set up GitHub account
We came to know that GitHub is a web-based hosting service for version control using Git. In addition, it offers different plans for public and private repositories; here we will demonstrate the hands on activity on public repository, i.e. free version.
Next, click on Sign up for GitHub once you provided a user name, email id and password.
Sooner you will get a new page segregated by three steps, in the first step; you will have to click the Create an account that will lead some verification. In the second step, Choose your subscription, select Free, and proceed further by clicking the Continue button.
In the next step, you can share basic information about yourself and preferences or you can skip this step. Meanwhile, you will receive an email to verify your account. It is essential to verify your email address, once confirmed; your GitHub account is set up successfully.
Do not forget to note down the user name and email id that required in next steps.
Congratulations, the GitHub account set up done!! 😊
STEP – 6: Login from Git local to connect remote GitHub
Move to terminal window and execute the following commands by replacing your username as well email, which you have been copied in previous steps. In fact, you need to provide the registered email address and user name with your GitHub account.
git config --global user.email your_email_id
git config --global user.username your_username
STEP – 7: Create some multiple demo files and content in each file
In the meantime, we required some code file that considering different languages for the hands on activity, henceforth you can create some demo files with diverse extensions.
First, create a folder where you can store all the files in one place, execute the following commands.
mkdir demoproject
cd demoproject
Now required to create different files, you can use the touch command. Touch command is the easiest way to create new, empty multiple files, and execute the following command.
touch index.html texts.txt c_program.c java_program.java index.js styles.css
ls -l
Now you can use any text editors available in Linux, but I will prefer the vi editor. To open and work the vi in editor mode, follow the subsequent steps –
- Open the editor - vi filename.extension
- Activate the insert mode – click the I key
- Save and execute the vi editor – Click Esc, then : (shift) wq
For example, required to add the below code in c_program.c file.
#include<stdio.h>
int main(){
printf(“Hello! I am C-Program. Thank you!”);
return 0;
}
In the first step, open the vi editor with the c_program.c file, execute the following command.
vi c_program.c
It will launch the vi editor, change the mode to INSERT and write the above sample code. Once done, click the Esc key and follow with :wq to save and return to the terminal.
You can do verify the contents by using the cat command that displays contents of the file, execute in following way.
cat c_program.c
Similarly, you can add some contents in all specified files as well can skip some files that will not contain any codes or statements.
STEP – 8: Initialize Git
In fact, all the files are to be pushed to GitHub, essential to initialize a .git folder inside the directory by executing the following commands.
git init
git add .
git commit . -m "I am pushing all the sample files to my GitHub"
git status
Note, required to follow the process step-by-step confirmation of each command executed.
Although, you can visit the official site to get more information about the Git commands - https://git-scm.com/doc.
STEP – 9: Create a repository in GitHub account
Next, time to create a repository that is a storage location where you can push code files as well pull and installed on a computer.
Move to the home page, here click on Create a repository link that appears in the left most corner as you can see the below snapshot.
It will launch the Create a new repository page, where you need to provide details of the following properties –
- Owner/Repository name – Provide a short but meaningful name for your repository.
- Description – Though it is an option but can submit the description about the repository.
- Public/Private – Go with the by default selection with Public option.
- Initialize this repository with a README – It will place a README file in your repository.
Once you have submitted the details, proceed ahead and click the Create repository button. Here you will be navigated automatically inside the directory you have created.
STEP – 10: Cloning with SSH URLs
Since a repository has been created, now using the URLs you can use to clone the project into your computer. Here you will go through SSH URL that provide access to a Git repository via SSH, a secure protocol.
Navigate inside the directory, and click the Clone or download button that will provide two URL based clone option i.e. HTTPS and SSH. Click the Use SSH link.
If you get messages about the SSH key something like “You don't have any public SSH keys in your GitHub account….”, then time to create an SSH key and adding it to GitHub.
Move to the terminal and generate an RSA key for the registered email id by executing the following command.
ssh-keygen -t rsa -C registered_email_id
RSA key has been generated, by using the cat command you can display on the terminal, and execute the following command.
cat /home/demoadmin/.ssh/id_rsa.pub
Copy the entire Key that is required to add to the GitHub account, move to the same directory in GitHub homepage and click the add a new public key link that comes at Clone with SSH option. Alternatively, you can go via the Personal Settings - > SSH & GPG Keys.
Promptly Add new key page will be loaded the time you click the link, now provide the key title and the key under Key box accordingly.
Save the SSH key by clicking the Add SSH key, and you can see the acknowledgement of list that associated with the GitHub account.
Once the RSA Key has been added, you can get the SSH URL, move back to the Clone section and copy the SSH URL as displayed in below snapshot.
STEP – 11: Add Remote and Push Cloning with SSH URLs
Next, required to add this new remote by using git remote add command on the terminal, in the same directory where the repository is stored. In fact, the git command required the remote URL followed by origin keyword, so use the same-copied the SSH URL for the remote herewith.
Copy the git remote add origin <SSH_URL_of_Your_GitHub_Repository> and execute it in the terminal, something like that.
git remote add origin git@github.com:rajendrxxxxxxxxxx/demoproject.git
Remote origin added, now time to execute a push command that is used to send the commits from your local branch in the local Git repository to the remote server. Execute the following command to deploy all code files to the GitHub repository.
git push -u origin master
If you get some failed or rejected error using the above command, then go with a forced update by executing the following command.
git push origin master --force
Navigate the repository under the GitHub account and reload the page; you will get the newly added code files that were pushed from the local repository by using Git.
Congratulations, the files deployed to GitHub via Git successfully!! 😊
I trust you came to know a bit more about the Git & GitHub approach and enjoyed the hands on activity tasks. In parallel, we have covered some precise Git commands during the assignment pushing files to GitHub.
Undoubtedly, in further posts, we will see some more diverse topics and exercises, keep visiting the blog.