Git and GitHub Basics: Part 1 — git commands

What is git?

Git is a software that tracks changes made to the files and folder of a project. It is mostly used to develop the source code of a software collaboratively between a number of developers.

What is GitHub?

GitHub is a web application based on git that is used to host source code of software projects. The hosted source code is called a repository in GitHub terminology. GitHub utilizes the cloud for hosting and collaboration of software projects.

In this blog we will create a software project, use git to track changes and in the next one we will host our project in the cloud using GitHub.

I have created a folder named KickStartGit that has two files index.html and README.md.

The index.html contains the source code of our project and README.md contains information on what our software project is about.

Setting up git bash

git bash is an application that can be used to execute git commands in a CLI (Command Line Interface) environment. We need to connect our GitHub user account to git bash. You can head over to GitHub to create an account if you don’t already have one. You can download git from git-scm

Once git bash is installed, head over to the working directory (in my case it is the KickStartGit) and right click anywhere on the screen, and select the option Git Bash Here

A window such as this should appear

After this we add our GitHub username and email id. Use the command git config --global user.name "Your name" to configure your username and git config --global user.email "Your email" to configure your email.

If your configuration was successful, you’ll have your username and email address returned once you type in these commands and hit enter.

git config --global user.namegit config --global user.email

You should get something like this

Basic git commands

At a glance, following are some of the commonly used git commands

git init

The git init command is used to initialize a git repository. The folder where the git init command runs can now track changes of the files inside it. Executing the git init commands creates a .git folder. The .git folder contains information about the tracked files. The command is

git init

A brief side note; before moving on to other git commands, we need to understand about a git terminology called a staging area or index

Staging area

A staging area can be thought of as a preview of all the files that are changed before the changes are saved with a message. This process of saving changes with a message is called commit-ing. The files can be added and removed from the staging area. Git has three areas where files changes can be stored.

git status

The git status command shows the state of the working directory and the staging area. This command shows which files have changed and which files needs to be tracked. The syntax of the command is

git status

This command is only used to display information and does not modify any information in the working directory. Main categories of git status call are:

  • When a new file is added/deleted
  • When an existing file is modified

git add

The git add command adds the changes of the files in the working directory to the staging area. git add command can add a single file or multiple files at the same time. The syntax is

git add [filename]

The above commands add a single file to the staging area

If you wish to add all the files that are changed to the staging area, then use the following command

git add .

The ‘.’ adds all the files in the current directory that have changed

TL;DR

  • Set up git bash and link it to your GitHub account
  • Initialize git, i.e. git init to track changes of your working directory
  • Use git status command to check the state of the files in your working directory
  • Use git add to add the changes to the staging area

In the next post, we will “push” our code to a GitHub repo and talk about more useful git commands. Stay tuned!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store