Basic Git: A Beginner Guide to Understanding Git for Developer

Mohamad Rifqy Zulkarnaen
6 min readMar 22, 2021

Software Engineering currently is one of the field that people started to taking interest. Software Engineering teach us how to build a software that can provide useful service for the users. Surely, when we’re going to build a software, usually we works in team so that we can achieve our goal easier and faster.

Because we work in team, we need a system that can provide us to easily collaborate and sync our work. There is an answer for our need, and that is Git, one of most popular version control system. I will explain to you what is Git, and how we can utilize it so that it can helps us to collaborate in software engineering. So let’s dive deeper into it!

image: Git

What is Git and Version Control System?

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. — https://git-scm.com/

Basically, Git is a system that enables us to collaborate with other in terms of software engineering with easy and efficient.

Since Git is one of version control system, let’s understand what is version control system really is. Version Control System is a system that can track and manage changes in software code over time. Version Control System offer many benefit to us such as:

  1. Branching our work so we can isolate our work.
  2. Merging our branch so that we can integrate our work with our teammate work.
  3. Complete history of every changes made into our file.

First thing First, Git Setup

In order to use Git, we need to do some stuff first. First of all, we need to download and install git client. We can download it from here.

After the client is installed, open your computer terminal. We can proceed to identify ourselves by using command git config.

git config — global user.name "yourUsername"
git config — global user.email yourEmail@example.com

Once the git successfully set your username and email, we can check our configurations by using this commands:

git config --list

Here’s the example of git config in my local computer. We can find the username and user email that we just added here.

Hooray! We’re now set and good to go! Now we can start to learn basic Git command.

What Can You Do with Git?

There are a lot of things we can do using Git. In this article i will cover some of git basic stuff that we can use to help us collaborate in software development.

Creating Repositories

One of most basic stuff in Git is that we can create a repositories for our software code. To do this, we start by creating a folder somewhere in our computer, and then we open a terminal here. Run this command on the terminal:

git init

Once executed, we can find a new hidden folder in our new repositories called .git. This folder marks that we have initialized an empty Git repository and it hold some of information related to the project, such as origin, backup, upstream, branch, etc.

When command git init executed
.git folder on empty repositories

After creating a local repository, we can save our work into online repository so that our teammates can collaborate with us. In order to do that, we need to set some information such as origin and upstream(optional). Please note that we need to create the online repository at GitLab or GitHub before we set the origin of our local repository.

To set the repository origin, we can use command:

git remote add origin <repository-URL>

Once we set the origin of the repository, we can check it using command:

git remote -v
git remote -v example

Cloning Repositories

We also can copy or download existing online repository into our local. In order to do that, we need to know the online repository URL. We can get the URL by copying URL on our GitLab or GitHub project that we made before. After that, we can copy the repository by using clone command from Git:

git clone <repository-URL>
executing git clone

When successfully executed, we can find a new folder with name same as the project name that we registered on GitHub or GitLab on our current working directory.

Add and Committing Changes that Has Been Made

Say that we added some files or changing some line of code inside the file one time. If we’re going to track our progress and save it into online repository, we need to add our files to the Git with command:

git add <path>

After we add our files, we can check what just we added before using command:

git status

Then, if we’re set and ready to save it, we must type some message that indicate our action for the changes. We can start committing the message using command:

git commit -m "[EXAMPLE] change hello-world.txt"

After that, we push our changes into our online repository using command:

git push origin <branch-name>

Please note that we usually not pushing our changes directly into master, but we push it into other branch for review and collaboration related stuff.

example of add and committing changes, and push the changes into online repository

Be wary that sometimes we can’t push into branches because there are some changes ahead. To sync it, we need to execute command:

git pull origin <branch>

Changing Branch

We can change our working branches in case we’re going to work on other feature that doesn’t related with our current feature. To do this, we simply run command:

git checkout <branch-name>
changing branch

Once we executed it, Git will tells us that currently we’re at the branch that we want to work. All file that related to the branch will also added or changed according to the current branch state.

Turning Back the Time

In Git, we also can jump back to the changes that we’ve been committing before. Usually we do this when we found error or simply we want roll back our changes. We can do this by using git revert command. This command will create a new commit message that indicates our rollback action. But in order to do this, usually we need to know the commit hash that we want to go.

In order to get the commit hash, we can use this command:

git log
git log example

After we got the commit hash, we can execute this command to turn back the time:

git revert <commit-hash>
git revert example

Conclusion

As a software developer, it’s hard for us to work alone when we’re building a software. Therefore, it’s best for us to work in teams and utilize version control system so we can work faster and smarter to achieve our purpose. Version Control System, especially Git provides us many commands that can helps us to track modification and manage the changes toward our software code.

Thank you for Reading!

--

--

Mohamad Rifqy Zulkarnaen

just your typical curious and storytelling loving software engineer.