Git and Github

Git Tutorial 2: Basic Git Commands (Git init, Git Add, Git Status, Git Commit)

Git Basics, Git Init, Git Add, Git Status, Git Commit

In the previous knowledge bit, we have introduced Git and discussed the difference between Git and Github. We have also seen that how we can install Git in our operating system. Now in this knowledge bit, we are going to discuss very basics of Git. We will introduce very basic git commands including Git init, Git add and Git commit.

Basic Git Commands:

After the installation of Git on your operating system, you can check the version by writing command git –version in your command line.

Now, we are going to configure Git to do that we need to run the following command.

git config –global “”
git config –global “Your Name”

Where in the place of “” you will write your email address and in the place of “Your Name” you will write the password. This username and password will be set globally in ~/.gitconfig file.

how to check the version of git

So we have git version 2.18.0 installed in our operating system. Now the first thing we are going to do is create a new directory and in that directory, we will create a simple application. In that folder, we will use Git to track my changes and those changes back if I make any mistake. Now we are going to create a simple JavaScript file with name app.js and then add simple one line of code.

Git Init Command:

The Git init command used to initialize the new git repository. We can use Git init command to convert any new, existing and unversioned project to a Git repository. We also use it to initialize a new and empty repository. The git command only works inside of an initialized repository, so this is going to be the first command you will run in a new project.

So now we are going to run Git init command in our project directory.

basic git commands : how to initialize git init

Git init command has initialized empty Git repository in our project directory. You can also notice that it created .git repository in our project directory. So .git is a hidden folder in our working directory, which contains all of the necessary metadata for the git repository.

Git Status Command:

The next command we are going to discuss now is Git status. It basically asks for a current status of the Git repository and used to monitoring the states of both the working directory and repository. So when you type git status, then it will show you the following output:

git status check git status

So it tells me that I am on the master branch and it also tells me that we have not committed yet. You can notice that it’s telling me that we have an untracked file post.js. So the reason it says it to untracked files is that we have just initialized a repository and it does not automatically track every file that we have in the repository and every change we make.

Git Add Command:

Git Add command is used to index our files. If we want to track our project files then we use git add command. It’s a two-step workflow first we add the files then we tell git these are the files that we want to track and in the end, we will commit.

So if I write git add in our project directory and then write the name of the file that we want to track then it will track that file. Here is the example below:

how to check git status_4

You can notice that the status got changed and now it tells that our post.js changes to be committed, this means when we run commit command this new file going to be saved.

Git Commit Command:

Now the next command we are going to discuss is Git commit command is used to save the changes to the local repository.

So if we write git commit in our working directory then it will save the changes. So let’s go ahead and write following command in your working directory.

>git commit –m“post app message.”

Where –m is for message and in the quotes we write the message about our file. Here is the demonstration below:

how to commit git git commit

Now let’s make some changes in post.js file and write some code in that file and save again. Now if we check the status then it will tell us that post.js file got modified.

how to check git status

So when we run the status command it says that we have modified post.js and it also tells that changes not staged for commit. So the first thing we need to do is that update the changes. To do that we will run git add post.js command again then we will run the commit command. Here is the demonstration below:

git commit

So now if we check the status again then it will say that we have nothing to commit and our working tree is clean.

git status command

In this knowledge bit, we have introduced some basic git commands. We have discussed git init, git add, git status and git commit command and we have seen that how we can update our program and track the changes.

Click to comment

Most Popular

To Top