Getting started with ‘Git’

Kaveen Laksitha
MS Club of SLIIT
6 min readNov 10, 2021

--

Introduction to git and GitHub

Prerequisites

  1. Install git.
  2. Create an account on GitHub.

You can ignore this if you have already installed git and have an account on GitHub.

Introduction

Hello, everyone! I hope you're doing well. From this article, I will explain how to use git in your project while talking about other things which are related to this topic.

When you are doing a project with your friends or with your teammates, maybe you used to save your project with different versions and share your source with others through emails, USB drives, etc. And when your project is completed and needs to integrate it, you may be doing it by copying other people’s code and pasting it into the relevant files. But there is a way to do all those things via a version control system. From this article, I will explain how to use git in your project while talking about other things which are related to this topic.

Now you are wondering what is a version control system?. Basically, Version control is a way to save changes over time without overwriting previous versions. You can read more about version control by visiting this link. There are many version control systems out there. But ‘git’ is extremely popular and It has some major advantages.

Git is a free and open-source distributed version control system that was developed back in 2005. Compared to other version control systems, git is responsive and easy to use. Once you are familiar with using git, you will agree with me if I say “git” is the kind of a life savior of developers😎.

Git and GitHub

Even though git and GitHub both sound like same, they are different. I already talked above about git. It is a distributed version control system. But ‘GitHub’ is a cloud-based hosting service that lets you manage git repositories. You don’t need GitHub to use git, but you cannot use GitHub without using git.

Let’s get started!

Now let’s see how to use git to do version control. I think you have installed git on your computer. To use git, you have to open the windows command prompt, Git Bash, or terminal on your IDE.

Now let’s tell Git about your name and email.

git config --global user.name “Your Name Comes Here”
git config --global user.email you@yourdomain.example.com

In order to make sure git saved our configuration settings, we can use the following command.

git config --list 

Alright, now we need to tell git which folder we want to initialize git. To do so, get the path of your project directory and type the below command on your terminal/ command prompt. It will change the working directory of your terminal/command prompt to your project directory.

cd <path-to-project-folder>

Okay, we have navigated to the project directory. Let’s initialize git in the project directory. The following command will do that.

git init

If you want to create a new project directory with git configurations, use the following command

git init <folder-name>

You can contribute to an existing project by making a copy of the repository in your system. In simple terms, create a clone. You can use the following command to do that.

git clone <remote-repository-URL>

Now you have initialized your project directory with git configurations. Before moving forward, you should know about four major areas in git. Look at the below figure.

01. Working Directory

Your project directory. sometimes also called the “working tree”.

02. Staging Area

The staging area is a place to record things before committing.

In Git, a commit is a snapshot of your repo at a specific point in time. Committing is the process that brings your changes to the local repository.

03. Local Repository

The local repository is the one on which we will make local changes, typically this local repository is on our computer.

04. Remote Repository

A remote repository in Git, also called a remote, is a Git repository that’s hosted on the Internet or another network.

Alright, let’s get back to our topic. We have already initialized your project directory with git configurations. Let’s add a remote repository connection by the following command.

git remote add <remote-name> <remote-repository-url>

I think you know how to get the URL of the central repository. If you don’t want to use a remote repository, you can ignore this step. But it’s not preferred.

In order to tell git which files you want to stage, you can follow the below command.

git add <file-name>

Following the below command you can add all changed/ edited to the staging area.

git add --all

or

git add -A

Now your files are in the staging area. In order to bring those files to the local repository, you have to commit(mentioned above) your changes by using the below command.

git commit -m “<commit-message>”

When it comes to a commit message, you can use any message as a commit message. But try to provide a meaningful message which belongs to your commit.

Whoo! now your files are safe in the local repository. But we have not pushed those files to the remote repository yet. We can do it by using the below command.

git push <remote-name> <branch-name>

You already know what is the remote name is. But what is this <branch-name>?. Before getting to know about branch name, you have to know what is a branch. A branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is ‘master’. This default branch is created on the point we have initialized our project directory(remember the git init?). If you want to create another branch you have to use the below command.

git branch <branch-name>

After you create a new branch, you have to checkout to that branch in order to work there. you can do it by the following command.

git checkout <branch-name>

There is another command to do above both things at once.

git checkout -b <branch-name>

Ok 😂. Let’s resume from the point which I’ve paused. Where were we? Ah, I remember… We have pushed our commits to the remote repository. Now other teammates or collaborators of your project can use the changes you made. What if other teammates or collaborators make changes to the project and they have pushed their changes to the remote repository. How are you going to get those changes to your working directory?🤔. Simply you have to do it type the following command.

git pull <remote-name> <branch-name>

The above command will fetch the modified remote repository to your local repository and then merge it to the branch that you are currently in. If you have any doubts about what we have done so far, you can understand them by referring to the bellow figure.

Figure 02

And that’s it! Now you can use git in your project to do the proper version control while saving your time and life😉.

Something little extra for you

When cloning a git repository that you have forked, you can add another remote named upstream in order to keep track of the original repository. It will sync changes you make, with the original repository.

First of all, we can list the URLs of the current configured remote repository using the below command on cmd/ terminal.

git remote -v

If there are no upstream branches, you can create an upstream branch by using the below command.

git remote add upstream <remote-repository-url>

Then you can collect the latest changer of the upstream repository with the below command. Repeat this every time you want to get updates.

git fetch upstream

After that, you can merge those changes with your branch by using the following command.

git checkout <branch-name>git merge upstream/<branch-name>

Those things may be a little bit confused for you. But this will be helpful when you are working with git.

--

--

Kaveen Laksitha
MS Club of SLIIT

Undergraduate of Sri Lanka Institute of Information Technology