Sitemap
Shradha Katyal

Shradha Katyal

Learning something new everyday!

Git Basic Commands

5 min readMay 7, 2018

In this article, I will talk a bit about Git and GitHub. I will also go through some basic git commands that a new developer should be aware of.

What is git?

The official site of git, https://git-scm.com gives the following definition:

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Let me break this definition down.

A version control system is something that records the changes made to a file over a period of time, so that you can work on the previous versions of the file or revert back to them in case of any errors. Most text editors today have the simple option of undo (ctrl+z) that can be used to revert the changes made to our file. Git works in a similar way but has many more features and is way more advanced than a simple undo action. Git allows us to record different versions of our project files and go back to work on any of them. Git makes it easier to manage our project and make changes to it without worrying about ruining something.

Git stores our project or group of files in something known as repository. Each project should have it’s own unique repository. A number of users can work on a single Git repository simultaneously. Git repository or repo can have many branches. Branches are different copy of the same repo. The changes that are made to the repo locally must first be pushed to the remote repo and them committed. Each change that is committed, is provided with a commit message and is represented by a unique SHA.

I am not going to go into more detail. Simply google what is git? And you’ll find plenty of useful resources explaining what Git is and how it works.

What is GitHub?

GitHub is a web based service that provides version control using Git. So, Git is a Version Control System(VCS), whereas GitHub is an online service that helps us manage our repositories using Git. GitHub is one of the most popular Git service provider and there are others such as GitLab, Bitbucket etc.

Before moving forward, please make sure you have at least a basic understanding of what Git is and what it does! Also, to setup Git on your pc refer to this link https://git-scm.com/downloads and follow the instructions!

Press enter or click to view image in full size
Photo from Unsplash

Git Commands

Creating a Git repo

There are two ways to do this. Either create a repo from scratch or clone an existing one.

To create a repo from scratch:

  1. Using the terminal, go to the directory you want to create a git repo in. Say you have a folder in your home folder called ‘myproject’ and you want to create a git repo in it. Go the folder with the following command: cd myproject .

2. Once you’re in the project folder, simply enter git init.

That’s it. You’ve created a new git repo.

To clone an existing repo:

  1. Using the terminal make sure you’re in the home folder.
  2. Open GitHub and search the repo you want to clone.
  3. There will be a green button on the right hand side with ‘clone or download’ written on it. Click the button and copy the link provided.
  4. Go back to the terminal and enter git clone url . Enter the copied url here.
  5. This will clone the repo into a folder of same name.
  6. If you want to clone the repo into a folder with a different name, simply enter git clone url folder-name .

Checking a Repo’s status

To check the status of a Git repo, simple enter git status into the terminal from inside the repo.

Git status command tells us the following things:

  1. New files
  2. Modified files
  3. Deleted files

The files that are present in the project folder but not yet added to the git repo are displayed in red colour. The files that have been added to the staging area but not yet committed are displayed in green colour. If all the files have been added, committed and pushed to the repo, the following message is displayed:

Get Shradha Katyal’s stories in your inbox

Join Medium for free to get updates from this writer.

On branch master

nothing to commit, working directory clean

Adding commits to a Repo

After initializing a Git repo, the next step is to add the files from our local repo to the staging area. The staging area comes before we make commits to our repo. To add files to the staging area we run the following command:

git add filename

The file name is written with the extension. For example, if we want to add index.html to the staging area, we’ll enter the following command in our terminal:

git add index.html

If we want to add all the untracked files to the staging area with a single command, we enter the following:

git add .

We can add multiple files with a single command, by writing all the files in a space separated list as follows:

git add filename1 filename2 filename3

Once we’ve added files to staging area, we can finally commit them. To commit all the files in the staging area, use the following command:

git commit -m ‘initial commit’

Here, the -m flag stands for commit message and is followed by a meaningful message.

Pushing to a Remote Repo

To push the files to a remote repo, use the following command:

git push remotename branchname

For example if you want to push the changes to the prod branch and the name of your remote repo is origin, you’ll run the following command:

git push origin prod

Reviewing the history of the Repo

To view the history of all commits made in a repo, run the following command:

git log

This command displays the following information:

  1. A unique SHA for each commit
  2. The author of the commit
  3. The date the commit was made
  4. The commit message

The log command displays all the commits ever made and that can be a lot of information. If a user wishes to see only the commit message and SHA, the following command can be used:

git log — oneline

This command shows one commit per line.

That is it for this article. Do give a clap if you liked it!

--

--

Shradha Katyal
Shradha Katyal

Written by Shradha Katyal

Learning something new everyday!