DEV Community

Cover image for Introduction to GIT Version Control
kramnev for UP Mindanao SPARCS

Posted on

Introduction to GIT Version Control

This Article was co-authored by @gadu30

Introduction

        In the world of programming, teamwork and collaboration are common. Updating files and tracking changes can be tough when working on projects. Therefore, learning version control is essential to keep things tidy, under control, and manage changes to documents, programs, and directories. This is where GIT steps in, it is a Distributed Version Control System (DVCS) that can record different versions of a project, bring back any of these versions instantly, and smoothly combine changes from different project branches. This makes Git a robust and well-known Version Control System (VCS) today. This article aims to give a clear introduction to Git Version Control.

What is GIT?

        Git is a popular version control system primarily used for programming and project management. It is an open-source tool and is made to easily track from solo projects to complex collaborative projects with a large number of contributors. It’s important to note that Git is not the same as GitHub. However, learning and understanding the fundamentals of Git can help you effectively use any Git-based platform, such as Azure, BitBucket, Sourceforge, GitLab, Codebase, and more. Among this, it is still common to use GIT with GitHub.

What is Version Control?

        According to Atlassian (n.d.), Version control, also known as source control, is the practice of tracking and managing changes to software code. Version control systems are software tools that help manage changes to source code over time such as adding new lines, simplifying/revising existing lines, or simply removing lines from the existing code. Version control offers a lot of help for developers not only for those who have been in the industry for a long time, but also for those who are beginners who are willing to learn more and practice coding. It also offers protection for the source code from being ruined or casual degradation of human error and unintended consequences.

Advantages of GIT

  • Makes branching and merging easy. It is cheap, fast, and consumes very little space so that you can branch whenever you want.
  • Allows you to save different versions of a file, making any versions to be retrieved anytime, comparing between versions is also possible.
  • It is compatible with a large range of platforms such as Windows, macOS, and Linux, and also supports various protocols such as HTTP, SSH, and Git protocol.
  • It maintains a detailed history of all changes made to the source code. This data includes who made the changes, when they were made, and the nature of the changes.

Disadvantages of GIT

  • It has a steeper learning curve compared to simpler version control systems. It has a lot of features and commands that may be challenging to grasp for some users new to version control.
  • It can also experience slowdowns in large repositories with a long history such as operations like cloning, fetching, and merging.
  • GIT also lacks strict access control mechanisms. While it has basic user authentication, more advanced access control features are typically implemented by integrating GIT with additional services.

How to Install GIT

To install GIT in Windows, you can refer to this link:

How to Install Git on Windows {Step-by-Step Tutorial} - PhoenixNAP

In this tutorial, you’ll learn how to install and set up Git on Windows. Start taking control of your developers work flow with Git.

phoenixnap.com

For MacOs and Linux, refer to this link:

Using GIT

Checking Versions of GIT

      Before we actually use GIT, we must make sure that we have GIT downloaded in our systems. To check this, we can prompt into the cmd or the Command Prompt git --version

Image description

GIT Workflow

Git has three main states wherein your files can reside in: modified, staged and committed.

  • In the modified state, it means that you have changed/edited the file but you have not committed it to your main repository.
  • In the staged state, it means you have marked a modified file in its current version to go into your next commit prompt.
  • The committed state means that the data is finalized and stored in your main repository.

Image description

Making a GIT Repository

Local Directory

  • Open GIT Bash and change directory to desired location.

Image description

  • Issue a git init command.

Image description

  • To make sure whether it is a git repository, we can issue ls -a to check if there is a .git directory in the repository. This directory is hidden because it is not meant to be seen by users and works primarily to store information for programs to run.

Image description

Cloning an Existing Repository
       To be able to collaborate and contribute on an existing project or repository. Cloning it into your local device makes collaboration easier as it copies the entire data that the server has including the version history of the repository which makes it easier to retrieve past versions of the files or compare different versions of the file itself.

  • git clone <url> is used to clone an existing repository.

Image description

Checking the Status of the Repository

       As we start to work on a repository, modifying a file and saving it is a common thing to do. At some point we need to commit it to the main server for it to make changes. To check the status of the files in which state there are, whether untracked, tracked, modified, staged, or committed we can issue a git status command. It also shows which branch we are currently working on, in this case is the master/main branch.

Image description

Adding a New File

  • Making a simple file that has the text "Hello World" inside the file.

Image description

  • Next, is to check the status of the file, in this case it is still in the untracked phase.

Image description

  • Then, we move it to the staged phase by issuing the git add command and after it is to check the status of the SAMPLE file.

Image description

  • If you have nothing else to change or edit, you can commit the changes in the main server. Committing to main will launch a text editor that will let you prompt a commit message.

Image description

Comparing Changes

       When comparing versions of a file, we can simply use the git status command to know if there are any changes made in the file. However, if you want to know the detailed changes made “inside” the file we can issue the git diff command or use git diff --staged. The basic use of this command is to help you understand the changes and compare it from the last version of the file.

Image description

Conclusion

       In conclusion, learning Git Version Control is a valuable skill for anyone especially in the tech field. It provides a robust and flexible system for tracking changes, collaborating with others, and managing different versions of a project. By learning Git, you gain the ability to work more efficiently and effectively.Furthermore, Git is widely used in the industry, making it a vital tool for growth. Overall, understanding Git can significantly enhance your coding journey even if you are still a beginner or a seasoned developer.

Top comments (0)