Getting Started with Git Basics
Tags: git, gitbasics, gitcommands
Published: 2022-11-14 06:27:08

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.

![git — version command](https://cdn-images-1.medium.com/max/2000/1*X0UIXwJZUDksxh84hrvy0A.png)

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.

![Create a folder named Git Project](https://cdn-images-1.medium.com/max/2000/1*Kit19ASRMVZkvFkeCDzzzQ.png)

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.

![Initialize a Git repository](https://cdn-images-1.medium.com/max/2450/1*V8JTVC314WuKhlPtBLRWDw.png)

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

![Create a file using touch command](https://cdn-images-1.medium.com/max/2018/1*2bW2OnwIJzvfijMr1kZJxg.png)
>  `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.

![Git Status Command](https://cdn-images-1.medium.com/max/2588/1*8eioVNiV1y35fOrELaYM1A.png)

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`

![Git add file to Staging area](https://cdn-images-1.medium.com/max/2108/1*DSGyJF3ospvZAdgD9TJpiw.png)

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"`

![Commit a file in Git](https://cdn-images-1.medium.com/max/2920/1*EjBs0pRhw7AlcrEqCtHwTA.png)

`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](https://cdn-images-1.medium.com/max/2296/1*BjyqamLvQpos0OOQI5pzRA.png)

`git log` command returns all commits with their SHA hash (Remember I’ve said about this in [my earlier post](https://5minslearn.medium.com/how-git-works-1d07a98aa99a)), 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.

![git log — oneline command](https://cdn-images-1.medium.com/max/2000/1*zeUVKQMsDsl4Pjpmpxg_bA.png)

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](https://5minslearn.gogosoon.com/?ref=devdotto_git_basics) to receive more such insightful articles that get delivered straight to your inbox.