Getting Started With Git

Lakindu Hewawasam
5 min readJul 15, 2021

Learning git is a challenging obstacle for many, but once you grasp the fundamental commands, you are good to go!

Git might sound scary, but trust me its not. I used to think Git was a difficult VCS to learn but once I started playing around with it, boy, did it make my life easier.

Lets start off by asking, What is Git?

Well Git is software that we can use to track changes that we have made to any file. Yes, you heard it, any file. But this comes in handy especially for us developers. Haven’t we always had a time where we backup our codebase to OneDrive or even copy the entire project onto a flash drive to maintain a backup? Haven’t we always had files like version 1 , final , new final . Well fear not, git will help you overcome this obstacle.

Setting up Git

Lets start off by setting up Git.

https://git-scm.com/downloads — visit this link and first download the setup for your platform. Provide the default options and proceed with the installation. Make sure to add git to the path variables during installation.

How Does Git Work?

Git works in a concept called repositories . We commit our codebase onto a repository to maintain a local revision history. And then if we ever need to backup the data, we can push our code to a remote (online) branch when needed.

Don’t worry, I will be walking you through all of this to help you get started with Git.

For Demonstration purposes, I will be using a single file named index.html to help you walk through this step-by-step tutorial.

Starting Check Point

This will be the place where I initially start. For those that want to follow along, I have placed this screenshot above.

Step 1 — Initializing a Git Repository

Before we do anything, we have to create a Git repository so that our code can be committed to a container of sort.

To create a repository, open up your terminal. I prefer to use the cmd on VsCode. But you are feel free to use any terminal of your choice.

On the terminal, make sure you are on the root of the project directory. Once you are in the root directory, run this command.

git init — This will initialize the repository. Once this is done, you will get a message Initialized Empty Repository.

Then we can go ahead and make our initial commit to the repository…

Step 2 — Adding Files to the Staging Area

The staging area is a place where we first add our files before we make the actual commit.

To add files to the staging area:

  1. Add files by the file name
  2. Add all files.

Adding files by file name — git add fileName.type

Adding all files- git add .

Once you run either of these commands, your files will be in the staging area. To check this: run the command — git status

Output of Git Status

The green shows that a new file has been added to the staging area.

Step 3 — Making a Commit

To make a commit to the local repository, run the command: git commit -m "YOUR RANDOM MESSAGE"

Once you get this output, your files would have been committed to the local repository successfully. Re-run git status to check if your files files have been committed.

All Files Committed — OUTPUT.

Step 4 — Making a Change

Go ahead and change the index.html to this.

Change Output.

After changing, re-run git status . You should get an output as illustrated below.

If you get this, this means that git is successfully tracking changes made to your file.

If you wish to re-commit this change, run the commands

  1. git add .
  2. git commit -m "random message"

Step 5 — Viewing my Commit History

If you ever want to view the commits that you made to the repo, simply use the command git log . This will show all the commits that you made to the repository.

Step 6 — Connecting to GitHub

Lets assume that we have created our project, and we want to publish this code to the cloud and maintain a backup. This is simple. Go to GitHub and create a repository. I will be maintaining a private repository, but if anyone wants access, just drop me a message in the comments.

Once you create the repository, copy the ssh or the https link. If you have enabled two factor authentication on GitHub, make sure to copy the ssh url.

Go back to your terminal and add the following: get remote add AnyName PasteRemoteURL

I personally prefer AnyName = origin . But this is up to you.

Once you have done this, run the command git remote to view the list of remotes.

Output for Git Remote

If you get this, this means you have set it up correctly.

Finally, run the command git push origin master to publish your local master branch to the GitHub online repository.

This will indicate that your push has been completed.

Go to GitHub and check your repo for results.

VIOLA!!! Just like that, we configured a local branch and published it onto a remote branch.

And there we have it, Git is an absolutely powerful if used correctly. Master these commands that I walked you through and your skills as a developer will be honed to another level.

I hope you enjoyed this tutorial because I always enjoy posting content like this for people to read.

Do comment if you think I have made any mistakes because I, too, want to keep learning and grow as a developer.

Thank you!

--

--