A Complete Guide To Use Git For Beginners

Writing code might be complex but it is easier to control with the right support tool. How do you know a new build of your software has shipped successfully? How do you know a single feature added to your website is working as you want it to? To ease the pain of tracking down all the issues, version control systems are useful tools that help you keep monitoring the changes that you make to your code. These systems are useful for a lot of reasons, but the primary reason is the time saved on tracking and fixing issues.

The term Version Control is used to describe a system that tracks and stores modifications made to files and data for future reference. There are two types of version control system, traditional and distributed. Traditional version control tools, such as subversion(SVN), were designed to be used by a single individual working within a single project, you will have a single central server where you will have to commit all the changes and are often referred to as a Centralized Version Control System (VCS). By contrast, distributed version control tools, such as Git and Mercurial, were designed to be used by a group of people working on a shared project where site owners do not need a server to store all the files instead they can clone a copy of a repository locally with which you can know the complete history of the project and are often referred to as a Distributed Version Control System (DVC).

Scroll down to know more about one of the most used version control system by developers today: Git, its commands and more specifically Git Checkout Remote Branch.

  1. What is Git?
  2. Need Of Git
  3. How to Use Git?
  4. Features In Git

#1 What Is Git?

Git is the leading and most popular source control management tool which is free and open-source distributed version control system for computer programmers and system administrators. It was initially developed by Linus Torvalds in the year 2005 with other kernel developers for the development of Linux Kernel. Its name is short for "get it together", because it's designed to maintain a collection of files in a system's version control. Git is so powerful that it's used by companies like Facebook, Twitter and so on as well as by a wide range of open source projects.

It’s available on many different operating systems, including Linux, Mac OS X, and Windows. It is also supported by most web browsers and file transfer software. The basic concept behind Git is that changes to your source code are identified by commit messages. The commit history of your code is saved in the Git repository, which you can think of as a vast storage facility for your code.

Here is the basic git workflow diagram with the commands that we use in daily basis as a developer.

Git Workflow

#2 Need Of Git

  • There will be multiple developers across regions working in parallel which might affect the project and code conflicts might arise. To avoid this a version control system like Git is needed.
  • You can handle different versions of the code at the same time.
  • A developer can easily create a own copy of the project, can make changes into it and can commit those which you can pull using the pull request.
  • Since it is speed and efficient it can handle projects of any size from small to larger ones.
  • Developers can keep track of code history.
  • Collaborate on code as a team.
  • You can see the code changes whenever you want.
  • Deploy code to staging or production.

#3 How To Use Git?

Working with git may be difficult for the first few times. When you understand the workflow of using git and its commands you will become a pro in it.

Git Life-cycle

To use git you should know the commands and the correct usage of it. Now let's go through some of the commands in the git, see how it operates and when should we use these commands.

  1. Create Directory, Initialize and Clone a Git Repository
  2. Steps Behind Commit in Git
  3. Check Status and Push the Changes
  4. To fetch changes from remote repository

#1 Create Directory, Initialize and Clone a Git Repository

a.) Create a Directory

mkdir <dir_name> 

The first and foremost step is to create a directory to contain the project.

b.) Move to the Directory

cd <dir_name>

Navigate to the newly created directory using the Change Directory command.

c.) To Initialize an Empty Git Repository:

You can start a git repository with the below command

git init

A .git sub-directory is created inside the current directory which holds all the Git metadata information. This is used only once during the initial set up of the new repository. It will also create a new branch in the repo.

d.) Clone the Repository

git clone <cloning_url>

To obtain the repository from the existing URL in command line use the following command.

To copy the repository from the central repository this clone command is used for users to obtain a local development clone. Copy the URL using the clipboard icon and paste it in the cloning_url field and click enter in the terminal to clone the repository.

#2 Steps Behind Commit in Git

a.) Make Changes or Build a New Feature

Add changes in the existing working directory or create a new file and implement the modifications.

To make changes and commits first we need to create a file. Create a sample file named text.txt and insert some content in it.

b.) Check the status before adding or committing any file

git status

You can use the git status command to view the updates you have made in your local git repository.

It displays the state of the repository and staging area. You can easily check the tracked, and untracked files using git status command.

Status when Working Directory is Clean:

Clean Working Directory

Status when a new file is created and made changes in it:

New File is created

c.) Review Changes Before Adding the Files.

Review all the changes before staging the files.

git diff

d.) How to Stage Changes in Git?

The file you have created is in untracked files. To stage the changes made, add the file using the following command.

git add <filename>

Check whether the file has been added or not using the command git status.

The file has been added.

Add all files together using this command.

git add .

This might save your time when you have bunch of new files to add in the repository.

e.) Commit your Changes

If you commit changes on a regular basis in your code, you are creating a version control history of your work. To commit the changes that you have made use the below command.

git commit -m "<message describing the change>"

The above command can be used if your commit message is short and crisp. When you are adding a bunch of files you might have to add a lot more insightful and descriptive in the commit message. In such case use the following command.

git commit

Once this command is executed you will be navigated to a editor in the terminal, where you can insert the commit message related to the feature or modifications you have made in the file.

If you are using vi or vim editor, Type i or click on insert button in your keyboard to add some commit message in the terminal. Press Esc and enter :wq to save and quit editor.

If your file has successfully been committed the above message will be shown in your terminal.

#3 Check Status and Push the Changes

a.) Check the Status

git status

Check your status in local repository using the git status command to ensure that you have staged and committed all the modified files.

This shows that there is no files with changes to be committed and the current working directory is clean.

b.) Push the Changes Made In Local to a Remote Repository

git push

By using git push command you can upload all the changes from local repository to the remote repository.

It will ask for your github username and password. Enter the login credentials to push the modification to the remote repository.

#4 To fetch changes from remote repository

To update the current local working directory use the below command to fetch all the changes from the remote repository.

git pull

This means your working directory is clean and there is no update or your teammates haven't modified anything in the repository.

#4 Features In Git

Here are some of the basic and most vital features of git which you should know before using it.

a.) Free and Open-Source

Git is a free and open-source version control system where different developers can work on the same code at the same time and can sync and share changes to a central repository. This makes it easy for developers to collaboration. Git grabbed all the attention by offering the functionalities such as repository space, privacy of codes, accuracy and speed etc.. unlike other paid version control system.

b.) Supports Non-Linear Development

Non-linear development is all about working on a project in a way that is useful to any number of users, regardless of how they are connected to one another. Git supports Non-linear development with branching and merging.

c.) Speed

Since Git is written in C language it makes every processing 10 times faster that other VCS tools, this has been proved based on the test conducted by Mozilla. In the websites like G2, Hackernoon and so on, GitHub and GitLab still holds the first position among the top 10 VCS tools.

Most of the git operations are performed in local repository and so it provides huge speed.

d.) Secure

Today, millions of devices are being used every day, all over the world. This means that there is a lot of information being transferred, and it also means that there are a lot of vulnerabilities. One of the most important security features to ensure safe data transfer is encryption which Git offers.

By using SHA1, git stores all the records in the form of objects in the hash. these objects collaborates with each other using the hash keys. Since git uses SHA1 to store records, it converts all the commit objects into a 14-digit Hex code. Thus if any problem arises from the developer side they can keep track of the issue, can easily detect which commit has been failed and the issue in it.

e.) Faster Release

Long ago, software releases took longer than 30 days, but then some of our favourite tech companies, Microsoft, Apple, and Google, adopted agile development techniques. These methods have improved efficiency by enabling rapid software updates, reducing risk, and enabling more frequent releases.

Many software developers now work in agile environments, where they frequently share changes with one another and the entire project community. By sharing smaller changes more frequently, developers are able to reduce their risk of introducing errors into the product. With Git, you can share every small changes frequently for faster release cycle.

Conclusion

Since Git is open-source and free distributed version control system you can learn everything practically. Git is a fast, efficient tool and it's used by hundreds of thousands of programmers around the world to manage code. With the above article you can easily find out the benefits that Git offers to many software development teams and organizations. Stay tuned with us to get more updates about Git and Git related blogs in LinkedIn, Facebook and Twitter.

Practice Makes A Man Perfect

Do practice and collaborate with other developers who has open-source projects to explore more in git. Hope this article is helpful in learning the basic fundamentals in git.

Share your thoughts with us about this article in the comment section.

Atatus

#1 Solution for Logs, Traces & Metrics

tick-logo APM

tick-logo Kubernetes

tick-logo Logs

tick-logo Synthetics

tick-logo RUM

tick-logo Serverless

tick-logo Security

tick-logo More

Vaishnavi

Vaishnavi

CMO at Atatus.
Chennai