Hello Sunil
Essential Git commands - Basic to advance with examples

Essential Git commands : Basic to advance with examples

#2 git init & git clone

You can think of a repository (aka repo) as a “master folder”, everything associated with a specific project should be kept in a repository for that project. But do remember, repository can have folders within them, or just be separate files.

In other words, repository is a location where source code is stored. You can either create new repository or use existing repository.

New repository (git init)

“git init” command is used to create an empty Git repository. Assuming you have already created an empty directory for your project then type the following command in terminal:

git init

git init - git commands

Note: This method will be useful when you are starting with a new project.

Existing repository (git clone)

Another method is to use existing repository. Such a repository is hosted on remote server like GitHub. To download a remote repository from GitHub, click on “Clone or Download” button, and copy the link provided.

Copy your repo from GitHub

Come back to your terminal and use the following command:

git clone <remote_repo_name>

git clone - git commands

“clone” is a simple command. You only need to pass it a link to the GitHub project. You can use either an SSH link or HTTPS link with the same command.

What “git clone” does is it copies the entire project to a directory on your computer. The directory will be created automatically and will have the same project name as the remote repository.

If you don’t like the name of the repository you are cloning, just type the following command in your terminal:

git clone <HTTPS or SSH Link> <New_file_name>

#3 git status

To display the status of your working directory, use the git status command which will shows you any changes you have made; which files are not being tracked by Git; those changes that have been staged and so on.

git status

git status - git commands

Note: This command lists all the files that have to be committed.

#4 git add

Lets add a file to your newly created Git repo by typing the following command in terminal:

touch Readme.txt

Create a document by using touch command in Linux terminal

You have successfully created “Readme.txt” file by using “touch” command in Linux terminal. Next, add this newly created file to the staging area of your Git repo by typing the following command:

git add <file_name>

git add - git commands

Note: The git add command lets you start tracking files and folders for your Git repository and moves them to the staging area.

That’s it; you have added a file to the staging area with git add command.

But what has this git add command actually done? Let’s view an updated status:

Readme.txt file added to repo and check the status by using git status command

The status has changed! Git knows that there is a newly created file in your staging area and is ready to commit the file.

But what if you create or change several files? Let’s assume you have added another three files to your repo such as “file1.txt”, “file2.txt”, “file3.txt”. Now you want to add all of them to the staging area.

Instead of adding these files separately, you can add them all together by using following command:

git add <file_name> <file_name> <file_name>

Added three more files to repo by using git add command

All you need to do is type file names separated by spaces with git add command. When you run git status once more to see what has changed, Git will output a new message listing all the files you have added:

Three more files added to repo then check the status by using git status command

Adding several files to the staging area in one go is much more convenient! But hold on a second. Let’s add few more files and a directory in your Git repo by downloading and paste it as follows:

Add few more files and a directory into a git repo

What if the project grows enormously as above and you have to add more than three files? How can you add a dozen files or directory in one go? Git accepts the challenge and offers the following solution:

git add .

Instead of listing file names one by one, you can use a period – yes, a simple dot – to select all files under the current directory. Git will grab all new or changed files and save them into your staging area all at once.

But there’s a problem with the “git add .” command. Since your are currently working in the root directory, “git add .” will only add files located in the root directory.

But the root directory may contain many other directories with files. How can you add files from those other directories plus the files in the root directory to the staging area? Git offers the command below:

git add –all

Or

git add *

The option “–all” tells Git: Find all new and updated files everywhere throughout the project and add them to the staging area. Note that you can also use the option “-A” instead of “–all”.

git add multiple files and directory  - git commands

Let’s check the status:

Checking the git status after adding few files and a directory in a git repo

Git can also take things out of its staging area by removing the files. To remove files from the staging area, use the following command:

git rm –cached <file_name>

In this example, we specified the command “rm”, which stands for remove. The “–cached” option indicates files in the staging area. Finally, we pass a file that you want to unstage.

Unstage a file from repo

Let’s run git status again:

git status after unstage  a file from repo

Sunil Pradhan

Hi there đź‘‹ I am a front-end developer passionate about cutting-edge, semantic, pixel-perfect design. Writing helps me to understand things better.

Add comment

Stay Updated

Want to be notified when our article is published? Enter your email address below to be the first to know.

Sunil Pradhan

Hi there đź‘‹ I am a front-end developer passionate about cutting-edge, semantic, pixel-perfect design. Writing helps me to understand things better.