Git : Introduction and Commands

Git : Introduction and Commands

In this post, we will learn about git and some of its common commands.

What is Git?

First, GitHub is not git. Many people understandably confuse the two. GitHub is a website for hosting projects that use git.

Git is a type of version control system (VCS) that makes it easier to track changes to files. For example, when you edit a file, git can help you determine exactly what changed, who changed it, and why.

It’s useful for coordinating work among multiple people on a project, and for tracking progress over time by saving “checkpoints”. You could use it while writing an essay, or to track changes to artwork and design files.

Installing Git

To use git, you will need to install it. You can download and install git from git's website. Make sure you add git to Path in case you are using it on Windows. In Linux, you can directly install it from the terminal using:

sudo apt-get install git

After installation is complete, to check if git was installed properly, execute the command:

git --version

How Git works?

Git works by storing a snapshot of the entire repository at any given point of time when you commit changes (a commit can be considered to be an equivalent of a save). So whenever you wish, you can go back to any previous commits and make modifications.

Git Basics

Initializing a repository

Before you can use any functionalities of git, you would require a repository. To initialize a repository use:

git init

The git init command creates a new Git repository. It can be used to convert an existing, unversioned project to a Git repository or initialize a new, empty repository. Most other Git commands are not available outside an initialized repository, so this is usually the first command you'll run in a new project.

Staging Changes

To commit changes, you need to specify the files whose changes you want to commit first. This is done by staging the changes

You can stage-specific files using:

git add <file 01 path> <file 02 path> <...>

or you can stage all changed files at once using:

git add .

The git add command adds a file to the Git staging area. This area contains a list of all the files you have recently changed.

Committing Changes

Finally, we come to committing changes. To save the changes you have staged, use:

git commit -m "<small description of the change (for ease of understanding)>"

Logs

After several commits, if you require to check out the commits you have made, check out the logs using:

git log

The git log command displays a record of the commits in a Git repository. By default, the git log command displays a commit hash, the commit message, and other commit metadata.