An Introduction to Git and GitHub

Sushmita Sah
WhatfixEngineeringBlog
6 min readSep 16, 2020

Let’s Git this party started :)

Topics we will cover :

  • What is Version Control?
  • What is Git and GitHub?
  • Difference between Git and GitHub
  • Why Git?
  • Basic Git commands
  • A basic introduction to GitHub

What is the Version Control System (VCS)?

It’s the system which is controlling all versions of an application. If we are working on a project: then the enhanced version will be developed by adding some new features to the previous version and so on. By adding new features to the project, it might be prone to errors and eventually, we may need to revert to the previous version.

At this point, the version control system is like a boon. It will keep track of each and every change that we are making in our project and help us to go back to the previous versions if needed. Ain’t this awesome?

What is Git and GitHub?

Git is one of the VCS which tracks all versions of a project and multiple people will be coordinating on the same.

It’s a distributed system where:

  • All versions of an application/project are not only on a single server but on multiple servers.
  • All the members working on a project can clone the project on their local machines and work on them.

Instead of having clones of the project on an individual’s system, we can put the project also on the 3rd party hosting service like GitHub. GitHub is a hosting service for git repositories.

Git != GitHub

Note: Project on Git is called repository.

Why Git?

  • Undo mistakes: If a user wants to revert to any previous version of the project: it easily solves this problem.
  • Collaborative work: Many people can work on a single project simultaneously and merge their features without conflicts.
  • Big community support: Developers community majorly uses Git and GitHub for managing their project properly.

Common Git Terminology:

Source: https://www.javatpoint.com/git-index
  • Working Directory: It's a project directory on which we work and make changes. Changes which we will make remain in the working directory until we add the files to the staging area.
  • Staging Area: Before committing the changes we have made in our project file, the file needs to be added to the staging area. It’s just like a preview of the next commit. We can add or remove the changes from the staging area.
  • Repository: If we want to make the checkpoint of the changes we have done to our files so that in future, we can revert to them if required, then this can be easily done by committing the changes of the files in the staging area and making them as a new commit. In git, repository contains all the files of our project and the history of changes done to those files.

Some useful Git commands:

  • git init: To initialise a git repository for a new or existing project, it converts the directory into a git repository which will be empty.
  • git add <file_name>: For adding file to staging area.
  • git commit -m “commit message”: For recording file changes to a local repository. This command moves the files from the staging area to the commit area.
  • git status: It will list out all the files that we have changed and still need to add or commit.
  • git commit -am “message”: It helps in adding as well as committing code at the same time. Putting a commit message is a best practice that explains changes made in the commit.
  • git log: This is the command used to see all the commits we have done. Head pointer is a pointer that will be on the latest commit.

Below is an example that creates a directory named LearningGit and inserts a python file named hello.py in it.

  • git rm - -cached <file_name>: To remove files from the staging area.

So, after updating the file and adding it to the staging area, we will remove it from the staging area thus making it an untracked file.

Branching in Git :

Source: https://www.nobledesktop.com/learn/git/git-branches

Branch is a movable pointer to a commit where the default branch is the master branch that git creates for us. Suppose the main project is on the master branch and you want to make a new feature of your project i.e., feature 1 and your colleague is making some other feature of the same project i.e., feature 2. So, to ensure that each of you is working independently, neatly, and simultaneously on the new features, the branching concept comes into play. Both of you can create your own branches from the master branch and work on the features. After completion of the features, both the branches should be merged into the master branch.

  • git branch <branch_name>: To create a new branch with name as branch_name.
  • git checkout <branch_name>: To switch from the current branch to another branch for merging on the local machine.

File in the master branch:

Created a new branch named dev and switched to it:

Changes are done to file to show in dev branch only:

Changes committed in dev branch:

  • git merge <branch_name>: To merge a branch into the current branch.

Now, all file changes that have been done in the development branch will also be reflected in the master branch.

Lets walk through GitHub:

To start with GitHub, we need an account and repository on its application:

It can also be handled from command prompt, by using below commands:

  • git remote add <short_name> <remote_url>: It connects the local repository to the remote repository(GitHub). <short_name> will reference to the remote instead of <remote_url>.The default <short_name> is origin.

Example: git remote add origin https://github.com/your-username/repository_name.git

  • git push <remote_url/remote_name> <branch>: It is for sending local commits to remote repository.

Example: git push origin master

  • git pull <remote_name> <branch_name>: This command will take up changes from the remote repository to the local computer.

Example: git pull origin dev

  • git clone <remote_url>: It creates a local working copy of a remote repository. By this command, the repository will be copied and downloaded on the local computer.

Example: git clone https://github.com/your-username/repository_name.git

Refer the following diagram for more information:

Source: https://www.reddit.com/r/git/comments/99ul9f/git_workflow_diagram_showcasing_the_role_of/

Note: Git Merge+Git Fetch=Git Pull

Well, that’s all about Git and GitHub.

--

--