Git & GitHub Basics

Git & GitHub Basics

Git is the most advanced tool in the market for source code management (SCM). This tool is also known as a version control system that maintains a log of every change that is made in the code repository of the projects with efficiency ( i.e. who, when, and what changes took place). Git is used to store our code repositories in a remote server such as GitHub. At any point in time, we can get to the previous version with the help of a unique number known as commit-id.

Advantages of Git

  1. Speed: Git stores every update in the form of versions. For every version, it takes incremental backup (Snapshot) instead of taking the whole backup, therefore it occupies less space and is fast.

  2. Parallel branching: We can create any number of branches as per our requirements. Git branches allow us to work simultaneously on multiple features.

  3. Fully Distributed: A backup copy is available in multiple locations which are located amongst individual team member servers instead of a centralized location. So even if we lose the data from one server we can recover it easily. That is why we also call Git a Distributed Version control system.

Important terms used in Git

  1. Repository: A repository is a place where you have all your code or kind of a folder on the server.

  2. Working directory: It is the directory where you see files physically and does the modification.

  3. Commit or SHA-1 hash: Stores the changes in the git repository. You will get the unique commit-id (a reference to identity for each change) for every commit you make. It is 40 alpha-numeric characters.

  4. Tag: It provides a meaningful name with a specific version in the repository.

  5. Snapshot: It is a backup copy for each version git stores in the repository. It is an incremental backup copy (only backup for new changes)This snapshot can be taken in the Staging area.

  6. Push: It copies the changes from the local repository server to the remote repository. It is used to store the changes permanently in the git repository.

  7. Pull: It copies the changes from the remote repository server to the local repository. It is used for synchronization between the remote and local repositories.

  8. Merge: By default, we get one branch in the git local repository called "main". We can create multiple branches in parallel development. Initially, we will code in different feature branches, which once completed, will be merged into the main branch. We can merge in any of the branches but merging code into master is standard practice that is followed widely.

Different types of stages in Git

There are a total of 4 stages in Git:

  1. Workspace: It is the place where we can create files physically and modify them.

  2. Staging area/Indexing area: In this area, it takes a snapshot for every version. It is a buffer zone between the workspace and the local repository.

  3. Local Repository: It is the place where Git stores all commits locally. It is a hidden directory so that no one can delete that accidentally.

  4. Central Repository: It is the place where Git stores all commit centrally. It belongs to everyone who is working on the project. GitHub is most widely used as a central repository.

Difference between the Main & Master branch

There is no major difference between the main and master branches, however, due to the cultural sensitivity behind the word "master" it was renamed to "main". That being said, during the creation of a new repository on GitHub, it will automatically use the word "main" as a default branch name.

Difference between Git & GitHub

Git is a command line interface (CLI) tool installed and maintained on the local system, which is used to manage different versions of files in a git repository. On the other hand, GitHub is a graphical user interface (GUI) hosted on the web that provides the ability to upload a copy of the git repository.

How do you create a new repository on GitHub?

Log in to your GitHub account. In the top-right corner click on the + icon, new repository. Provide the name, and description and click on Create Repository. Your new repository is created in GitHub.

How to connect local to the remote server repository?

Method 1: Generate the Personal Access Token (PAT) in GitHub and clone the repository in the local server.

On GitHub go to, Settings > Developer Settings > Personal access tokens > Token (Classic). Click on Generate new token (Classic). Provide the name, and expiration date and click on generate token. The personal access token is generated, just copy the token and save it for future reference.

# Clone the repository locally via the https URL of a remote repository. For that, we need to provide Personal Access Token(PAT)followed by https URL.  

$ git clone https://<PAT>@github.com/ManviSood/demo.git

Method 2: Generate the ssh-keys locally and store the public key on GitHub.

Generate the ssh keys on the local server using the below commands:

# Clone the repository locally via ssh of a remote repository.
 $ ssh-keygen
 $ cd ~/.ssh/
 $ cat id_rsa.pub

On GitHub, go to Settings > Access ( SSH and GPG keys) > New SSH Keys. Provide the title, and paste the public key id_rsa.pub (copy it from the local server) in the key section on GitHub. Click on Add SSH key. Now you are all set to clone the remote repository using the ssh URL in your local machine.

 $ git clone git@github.com:ManviSood/demo.git
# Test the connection between local and remote repository. 
 $ ssh -T git@github.com