Ateev Duggal

Sep 4, 2021

6 min read

Getting Started with Git

Git and GitHub

Git is a free and open-source Distributed Version Control System(DVCS).

It is used for tracking changes in the project directory and manage source code. It keeps a log of our complete history with all the comments we have made with their changes. By simply passing a command we can see all those changes with the names of the person who made them.

If you’re involved with a project you can clone the project’s Git repository, and then work on it as if it is your own copy.

Developers can even work independently and offline. They just have to push their work to the main repository and pull the changes made by their co-workers from the same repository.

Installing Git and GitBash

Go to https://git-scm.com/ and install it by just keeping the default settings and clicking NEXT every time.

After the installation, open GitBash either in your editor or right-clicking on the desktop and selecting GitBash from options.

Type git — version — this will tell you which version of Git you have installed in your systems.

Before starting your work on Git, we have to give our details to Git first for future use in projects.

For adding username type -

‘git config — global user.name <name>’

For email type -

git config — global user.email <email>

Now that we have a basic idea of Git, we will move forward and learn how to use it.

Before moving forward, just an introduction of how Git works -

There are mainly three stages for any local repository

In the above image, we can see the three stages in which Git works on our local system. Let’s understand them one at a time -

Working Directory — A directory in which we are currently working and has been initialized as an empty git repository (local) through git init.

Staging Area — Files and folders that you currently have in your working repository are untracked meaning any changes in them will remain unnoticed by Git.

Using command git add <filename.ext> or git add . (all files and folders) can be sent to the staging area where Git will start tracking them.

Repository — This is the local repository that we will be creating using the git init command and is initially empty. This repository will be on your local device in the same folder where your file is located (use cd to navigate ).

Now we have a brief idea of what are the three stages in which git works. From here, we will be seeing how to achieve them one by one -

Making a Remote Repository on GitHub

Step 1: Making a repository on GitHub

Go to https://github.com/ and click new above your repositories (assuming you already have a GitHub account).

Step 2: Filling required details

Fill in the required details and click on create the repository. Do not initialize it with anything like a readme file, or this will cause trouble later on.

Step 3: The following will appear after you have a remote repository.

This page will appear after you have clicked Create repository. You will stop here and continue this in GitBash on your local device.

Making a local Repository using GitBash

Step 4: Making an empty local Git repository

By using cd we can directly go to our working repository in which we will make our empty local git repository by typing git init in Git Bash.

Step 5: Checking the status of our repository

Write git status in the Git Bash and press enter. Files will appear but in red. This means that they are still in your working directory.

Step 6: Sending files to the staging area

Using the command git add <filename.ext> or git add . we can send them to the staging area, and now the files are being tracked until we send them to the empty git repository that we created locally.

We can even confirm this by typing git status and pressing enter, and the color of the files will decide in which stage they currently are. If the color is green — they are being tracked and are in the staging area, but if it is red — they are not being tracked and are still in the working directory.

Step 7: Sending files to the local repository

By typing git commit -m’message’, we can send the data from the staging area into our already made local repository.

You should always give a message for your commit and only in single quotes.

Step 8: Connecting the two repositories

The command git remote is used to check if the two repositories are in a link or not.

As you can see, every repository has its unique URL. Just copy and paste it in GitBash, and press enter for connecting the two repositories.

After typing the URL and pressing enter, the two repositories have now been joined. Type git remote, and press enter. If origin is displayed then the repositories are connected.

Step 9: Sending the data from local to the remote repository

We have a method to add all the files in the local repository that we have committed with a message and send that to the remote repository, and that is git push,

Since this will be your first push you have to specify the branch where your project will be saved, generally it is the main branch or the master branch.

Just copy the marked code from your remote repository and press enter for adding your contents into the remote repository from the local one.

Step 10: All done

Just refresh the GitHub page, and you will see your changes and the added files in your remote repository.

Now, use the following commands for further changes in the working repository.

git add ./git add <filename.ext> -> making any changes into the files

git commit -m’message’ -> sending files from staging area to the local repository.

git push -> to push the changes from local to the remote repository

git pull -> to pull any changes that your peers have made if you are working on a project with a team.