What is Git and GitHub? Important Commands for Programmers

In the programming world, it is necessary to understand and manage the versions of your code and projects that you create working hard day and night. In this article, I will introduce you to Git and GitHub, one of the prominent and famous version control system used by most of the developers across the globe, some basic Git commands, and how to set up your local and remote repository on GitHub using the terminal of mac computer.

What is Git and GitHub?

When it comes to Git and Github, most people get confused and think that both are more or less the same thing but, Git is a free and open source distributed version control system whereas GitHub is an online hosted platform for various services that I will explain you in the below section of this article. Now, you would be thinking what is version control? lets us understand this by a simple diagrammatic example shown below.

Screen Shot at . . PM

Suppose, initially, you created a file say ABC.txt within a folder on your system. Using the concepts of version control, you can manage, track, commit changes, roll back to a previous safe state and much more easily. In the above diagram, the local file can be converted to trackable files i.e a version control system using Git commands. Why is version control Important?

While programming and as a human, it is our tendency to make mistakes and then land up ourselves in a  position from where coming back to original state is not possible and we are lost. Using version control, we can track and control our huge files full of codes very easily and quickly.

Screen Shot at . . PM

There are basically three type of version control system i.e.

  1. Local version control system that is demonstrated above.
  2. Centralized version control system.
  3. Distributed version control system.

Here, I am only interested in local version control system and distributed version control system, using Git command to manage and manipulate them and then adding those files to remort version control hosted site such as Github for distributed VC.

Screen Shot at . . PM

So, hope you must have understood what is Git and GitHub? GitHub is a hosted platform i.e online server where you can create a repository, push the local repository that you have created using git commands on your personal computer, start a branch, write comments, open a pull request and much more.

Also, Read: 5 Best Github Alternatives for Programmers and Developers

Basic Git Commands

There are several Git commands but, I have highlighted some of the important ones, that a beginner should be familiar with those are:

1. Git Config

It is the command used to configure the name and email of the user. For eg.

Screen Shot at . . PM

2. Git init

This command is used to create a git local repository of your file and then you can start tracking the changes you make in the future. This command initializes an empty git repository inside the same directory that you are currently in with .git extension eg. of code on the terminal

Screen Shot at . . PM

3. Git Status

This command is used to show you the untracked files in your working directory. Between file and local git repository, there is an intermediate area called as a staging area. Untracked files are those files that are not under version control and will be shown in red color on the terminal. Command is.

Screen Shot at . . PM

4. Git Add

This is the command used to add the untracked file to tracking status, of the current directory, so that you can keep tracking of your modifications. See the demo code below.

Screen Shot at . . PM
Till now we have done the following as shown in the figure below. I have used mac computer to demonstrate you, and some basic mac terminal commands which are more or less similar to Window command line commands. I hope you all would be familiar with some of the basic terminal commands.

I have created a demoproject using mkdir command on the desktop directory. I have created 3 files named as file1, file2, file3 using touch command. Then, they have been initialized using the git init command and at the initial stage they all are untracked and shown in red color in the screenshot. To track them, I have used git add command and then they are shown in green color.

Screen Shot at . . PM

5. Git Commit

This command is to commit the changes in your file in the existing local git repository. See the figure below.

Screen Shot at . . PM

6. Git Log

This is the command used to see the changes or commits that you have made to your files. See the figure below.Screen Shot at . . PM

The hash shown in the highlighted section above uniquely identifies the particular commit that you make with the author name and time as well.

7. Git Diff

Suppose, you had a file and you made some changes in a hope that you are doing well and saved it and then closed the file. When you opened your file, you saw, the changes were not working as expected and then you wanted to see what changes you made. Well using git, you can do this by using the git diff command.

Screen Shot at . . PM

8. Git Checkout

Once you see the changes, and if you want to roll back to the previous one then this command is used.

Screen Shot at . . PM

9. Git Remote Add origin

This command is used to link up the GitHub account to your local git repository.

Screen Shot at . . PM

10. Git Push -U Origin Master

This command pushes your local repository to the remotely created Github’s repository using the U flag/option.

Screen Shot at . . PM

Setting up GitHub & Pushing your Local Repository

Head over to https://github.com/ and sign up if you don’t have an account by providing your username, email, and password. Once done, you need to verify your email id.

Screen Shot at . . PM

You will be directed to guide page where +sign is shown on the top corner as shown in the figure below.

Screen Shot at . . PM

Now follow the following steps as shown in the figure.

Screen Shot at . . PM

Now, we want to push our local repository to the GitHub. So,

Screen Shot at . . PM

use git remote add origin command as shown above and copy your GitHub account URL shown in the red box and then use git push -u origin master command. It may ask you for your username and password so, provide them and press enter and that’s it, your local repository will be uploaded to the GitHub’s server.

Screen Shot at . . PM

Now, your terminal should show you something like the above figure. When you go to your GitHub account, you can see the pushed file.

A summary diagram.

Screen Shot at . . PM

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.