Using Git locally

Tracking changes and collaborating is one of the most important aspects of IT. Version control is specifically important in software development where many people contribute to the same project. With VCS (version control system), it becomes easy to track and commit changes as it provides us with a rich history of changes. Git is a popular VCS and we would be discussing it in this post.

VCS has multiple real-life applications including storing configurations and documentation.

Why do we need a VCS?

  • As history is maintained, no information is lost. Additionally, there is a defined history of who made the changes and why. Hence,It is easier to back track when an incident occurs.
  • Using VCS, you can fix the problem now, and check what happened later on. Thus,it is very helpul in restoring production and removing the business impact.
  • With VCS, code becomes easy to understand for someone who is new as he has an elaborate history of what has been going on in the system.
  • VCS makes a team agile. VCS makes project management and progress tracking convenient.
  • With VCS, you can review other’s code or ask for recommendations when you get stuck.

What is Git?

The creator of Linux, Linus Torvalds, created his own collaboration tool called ‘git’ as he couldn’t find any that fit his requirements. Today, git is widely accepted as a VCS. Therefore, there are a number of hosting services available- Github being one of them.

Installing GitHub

Installing GitHub is straightforward. On Linux, git can be installed using apt, rpm, etc based on the distro. Windows uses an executable installer that can be downloaded from the site. The detailed steps can be seen here.

Git Flow

Changes in git follow the below-mentioned sequences to safely make and commit changes.

The git flow.
The git-flow.
  • Working Area

This is the place where changes are made to files. This is also called the index.

  • Staging area

Once changes are made and verified, they are hosted in the staging area.

  • Git Directory

Changes in the staging area are committed and finally moved to the git directory.

Configuring Git

The first step is to tell git who you are. You can set your email and username as follows:

git config --global user.name "Name"
git config --global user.email "[email protected]"

Initializing Git

Once git has been configured, it’s time to initialize a repository. Use git init to initialize a repository in your current directory.

git init

Furthermore, note the content of the folder once git is initialized. The .git is the git directory.

Initializing git
Initializing git
Contents of the working tree
Contents of the working tree

Git clone can be used to download a repository that is remotely hosted.

Making and committing changes

Create a new file: Let’s create a sample file ‘calculate_rate.py’.

Create a new file
Create a new file

Check Status: To check the current status, use the status command.

git status
Here, we can see the un-tracked file.
Here, we can see the un-tracked file.

Add file to staging: Once changes are saved, let’s add them to the staging area.

git add calculate_rate.py
Adding file to staging
Adding file to the staging area

Commit the change.

git commit calculate_rate.py

When we commit a change, a text editor opens as shown below. To add further, we can give a description of what is going on in this current commit.

Commit message
Commit message

Checking details of our changes: Change history can be seen with the git log command. It provides us with a timestamp, commit ID and

git commit calculate_rate.py

Logs

Unstaging changes that are not comitted.

Let’s say we accidentally added a file to staging. It can be reverted without any issue.

git reset HEAD
Reverting uncommitted changes
Reverting uncommitted changes

Reverting a commit

Let’s assume we committed a change and we need to roll back. Git provides an excellent option to revert the committed changes.

git revert HEAD
Reverting a commit
Reverting a commit

`Git` revert will create a new commit that will delete lines in the new commit. Revert simply rewinds that current commit and history is preserved. The logs are shared below.

Reverting a commit
Reverting a commit

Wrapping up

In this post, we have learned to use git which is a highly used utility for collaboration and tracking changes. Learning and using git definitely pays you back 🙂 In conclusion, apart from using a VCS, documentation is also important when collaborating. One such example is Confluence. You can review a detailed post about Confluence here.

About the Author

Hi there, I’m Zaira! I’m an IT professional with diverse experience in Linux-based application management. I keep applications up and running for businesses. I’m currently exploring cloud technologies. My message is simple and straightforward: ‘Always keep exploring and learning. IT is beautiful and almost like magic, you’ll never get bored and always find something surprising. So, keep learning 😊.

If you need any tech-related help, you can connect here: https://www.linkedin.com/in/zaira-hira/

Leave a Comment

I accept the Privacy Policy