Getting Started with Git Basics

Getting Started with Git Basics

As we discussed in our previous blogs, Git is a distributed version control system.

In this article, let’s create a new sample project with a couple of files and play over some basic git commands.

Before reading further, please ensure running git --version command on terminal returns installed git version as shown below.

Let’s start by creating a new folder named Git Project . You can use terminal to create a folder by running mkdir "Git Project" command.

mkdir command is used to create a folder (which is abbreviated as make directory). Directory and folder refers the same.

Let’s initialize git by running git init command.

You’ll get a message on terminal saying Initialized empty Git repository in <folder_path> . This means all your further changes on this folder will be tracked by Git.

Let’s create a file by running a command touch File1.txt

ls command is used to list all the files in a particular folder (current folder by default)

Let’s verify if this file has been tracked by Git. We can easily find this by running git status command.

git status is one of the powerful commands used often in Git world. This command shows the current status of our Git project.

The above screenshot reveals almost all information about this project. Let’s go line-by-line.

On branch master — We’re on the master branch of this git project (We’ll learn about git branches in future)

No commits yet — We’ve not yet made a single commit

Untracked files: File1.txt — The file (File1.txt) has been created and it’s not been added to staging area yet to track it’s changes further.

nothing added to commit but untracked files present (use “git add” to track) — No files are added to staging area to commit. But, you have untracked files on your folder. Use git add command to start tracking the file.

Let’s start tracking File1.txt file on Git. Run git add command on terminal to start tracking file on Git.

git add File1.txt

You can see the last 2 lines has been changed from previous and this screenshot. Let’s understand the difference.

Changes to be committed: new file: File1.txt — You have added your file (File1.txt) to Git tracking. But, you haven’t committed the changes yet.

Only when we commit the changes, it’ll be stored in the Git Memory

Let’s commit this file. Run git commit command to commit a file.

git commit -m "[Arunachalam] Add File1.txt file"

git commit command needs to be accompanied by -m param. -m is used to set a message to our commit. This is basically used to briefly describe our action on that commit.

Now we have committed our file. On running git status command now will show you that, you’re on a master branch, no changes to commit and working tree is clean.

To view our last commit, we can run git log command

git log command returns all commits with their SHA hash (Remember I’ve said about this in my earlier post), the author who made the commit, timestamp and the commit message.

Most people prefer to use git log --oneline command over git log command. Because, this command returns only the SHA and commit message which is enough to track the changes on our project.

We’ve learnt the basics of Git in this article. Practice all the commands we discussed in this article. These are the commands that you’ll be using often in almost all of your projects. We can learn deeper about using these commands effectively in our upcoming articles.

Subscribe to our newsletter to receive more such insightful articles that get delivered straight to your inbox.