Getting Started With Git

Praveen NG
4 min readMar 19, 2021

--

As you may know, Git is the most popular version control software. It is used to track changes to codes, especially in a large collaborative environment. One of the main reasons for its popularity is that it is easy to get started with Git by learning only a few basic concepts and commands. At the same time, if one wishes, Git has many features for handling more complex requirements. Over the years, other auxiliary tools like GitHub have been developed, which played an important role in Git’s popularity. In this article, I’m planning to give a brief introduction to some of the basic Git concepts and commands. I hope that this would be sufficient for a beginner to get started with Git.

Some Basic Concepts

Git has three core areas — a working directory, staging area and local repository (or local repo). Working directory is the directory in which a user writes his/her codes. Local repo is a area (for lack of a better term) where commits (checkpoints or snapshots of the code) are made. It allows you to go back to a particular version of the code, just like a backup allows you to go back to a particular snapshot of a hard disk. Staging area is an intermediate area. When you make sufficient changes to the code (e.g. you are done adding a new feature) and you are ready to make a commit (or a snapshot) you move code from working directory to staging area to the local repo. One can also backup the whole repository to a remote repo with or without using services like GitHub and GitLab. However, I will not cover that in this article.

Installing Git

Installing Git is relatively easy but it is OS dependent. Since it is straightforward, I’ll skip it. You can refer git documentation.

Configuring your system

After installation, the first thing you need to do is to configure Git on your system. Basically this just means adding your name and email.

git config --global user.name “Jane Doe”
git config --global user.email “jane.doe@email.com”
git config --list

The last command lists all the current configurations. One needs to configure Git only once, unless they want to use different user names and accounts for different projects.

Initiating Git

Initiating Git refers to “telling” Git that we are starting a new project and keep track of the codes. The easiest way to start a project and letting git track your project is by using git init command. Make sure you are within the project directory before you write this command.

git init

When you run the above command, Git creates a hidden directory (with the name “.git”) within the current working directory. Git stores all internal files for the project within this directory. Usually a user never needs to open this directory or its contents. Now you can start coding!

Committing Changes

It is important to commit changes that you make to the repository. How often you backup is a bit subjective. If you don’t commit often enough, you may not be able to restore the particular version you wanted to go back to. If you commit too often, it can be confusing with a really large number of commits. A rule of thumb is that you make a commit whenever you “finish a step” in the code. The code has to be “run-able” when you make a commit so that if at a later date you retrieve the commit, it would run without throwing an error.

Committing changes involves two steps — adding your code to the staging area and then adding it to the local repo (latter is called committing).

Adding to Staging Area

git add filename 

Note that Git adds files to staging area silently unless something goes wrong. So if you don’t see any output from Git, it’s a good thing!

If you have multiple files to add, you can list them all or alternatively you can use a period (dot). The first command below adds three files (file1, file2, file3) and the second one adds all the new and modified files in the current directory. You of course do not need to run both the commands if you want to add three files.

git add file1 file2 file3
git add .

Committing Changes to Local Repository

To commit changes to the local repository, you can use git commit command. You need to add a message to the commit which helps you understand the code changes when you look back from a future date.

git commit -m “Added feature email verification feature”

Git Status

Git status command is used to take a peek at the status of files in a project. It tells the user whether there are changes that needs to be added to the staging area or if there are changes added to the staging area that needs to be committed to the local repo. The command to check Git status is,

git status

Some Other Commands

Some other commands commonly used by beginners are,

git log
git log -p
git diff

--

--

Praveen NG

A data science professional with a research background.