GIT: A VERSION CONTROL TOOL.

Introduction:

Nicholas Okeke
10 min readJul 3, 2023

Ever wondered how different versions of your mobile apps or web apps are still available even as far back as the first version? And then when a newer version gets enough complaints then the older, more stable versions are made available?

Photo Credit: Soham Sarkar’s Blog.

This powerful technique is possible because of what is called version control made available by Git.

What is Git?

Git is a distributed version control system that helps developers manage and track changes to their projects. It allows you to keep a historical record of your project’s development, collaborate with others seamlessly, and easily revert to earlier versions if something goes wrong.

Some Basic Terminologies on Git.

  • Repository: a Git repository is like a folder where Git stores all the files, history, and metadata related to a project. It contains the complete version history of the project. It is also called a repo.
  • Commit: this is a specific point in the Git history where changes are recorded. It captures a snapshot of the project’s files at a given time. Each commit has a unique identifier (SHA-1 hash) and contains information like the author, timestamp, and a commit message describing the changed made.
  • Branch: this is an independent line of development in Git. It allows developers to work on different features, bug fixes, or experiments without affecting the main codebase.
  • Conflicts: this occurs when there are conflicting changes in different branches or commits that Git cannot automatically resolve. Git identifies conflicts when the changes made in one branch overlap or conflict with the changes in another branch, requiring manual intervention to resolve the differences.
  • Merge: this combines changes from one branch (source branch) into another (target branch). When merging, Git automatically combines the changes, resolves conflicts (if any), and creates a new commit to capture the merged state.
  • Pull: this is the act of fetching changed from a remote repository and merging them into the current branch.
  • Push: this refers to sending local commits to a remote repository. It updates the remote repository with the latest commits and makes the changes accessible to other collaborators.
  • Remote: this in Git refers to a copy of a repository that is hosted on a different machine or server. Remote can be accessed over a network connection (e.g. GitHub, GitLab) or locally (e.g. another directory on your machine).
  • Pull Request (PR): this is a feature in Git hosting platforms like GitHub and GitLab. It enables developers to propose changes to a repository and initiate a discussion or review process before merging the changes into the main branch. Pull requests provide a collaborative workflow for code review and quality control.
  • Fork: this is the process of creating a personal copy of a remote repository under your own account. It allows you to freely experiment with changes without affecting the original repository. Forked repositories are typically used when contributing to open-source projects or working on collaborative development.
  • Staging Area(Index): this is also known as the index, is an intermediate area in Git where changes are prepared before committing them to the repository. It allows you to selectively choose which changes to include in the next commit, giving you more control over the commit contents.
  • README.md File: This is a file used in Git repositories and it stands for Read Me. It contains essential information and instructions about the project and that should be read by anyone accessing the project. It is also a markdown file which is a lightweight markup language that allows for easy formatting and structuring of text with conventions.
  • License: this equivalent to a license in business. It defines the permissions, restrictions, and obligations associated with using, modifying, and distributing the software.
  • .gitignore File: this is used to specify files, directories, or patterns that should be ignored and not tracked by Git.

How Do You Install Git?

To get git up and running on your system, first of all, you need to check if git is already installed on your system. The popular systems could be Windows, Mac OS or Linux. We use a pre-installed software tool called Command Line Interface (CLI)/Command Prompt to check for this.

On Windows it is called cmd.

On Mac/Linux it is called Terminal.

Open the CLI (search for it on your search bar), and type:

git — version.

If it does not output a version number like this:

“git version 2.33.0”

And says:

Then git is not installed.

The installation of git can be done manually or using CLI.

To install git, use the web addresses below:

Windows: https://git-scm.com/download/windows

Mac: https://git-scm.com/download/mac

Linux: https://git-scm.com/download/linux

After clicking for Windows above, you will meet this interface:

Photo Credit: PhoenixNap Global IT Services.

Click the required link that meets your systems specification to download and install git.

After the installation is complete, open the terminal and repeat the command as above to check if it has been installed.

How Do You Configure Git?

To configure git, you have to use a preferred username and email associated to your local repository. To achieve this, use the following commands on your terminal:

$ git config — global user.name “Your name”

$ git config — global user.email “Your Email”

After configuring git, you can continue using your cmd for more manipulations.

Web Hosting Platforms for Git.

While git is a version control tool that enables you to keep track of different versions of your software projects, it is important to note that this can be done locally (offline on your computer) or online (cloud-based hosting platforms).

An example of one of such popular cloud-hosting platforms is GitHub.

GitHub is a cloud-based hosting platform that offers services which enables you manage git repositories.

Other examples of such hosting platforms are GitLab, GitBucket, BitBucket, etc.

How Do You Register on GitHub?

To register on GitHub, go to GitHub.com and sign up, as shown below:

Photo Credit: Nick.

Input your email and password and do the necessary confirmations. You will also be required to put in your username.

Note that the username and email should be ones used in configuring git on your system else, when you try to push your project, it will throw and error.

Exploring GitHub.

After registering on GitHub, you will be met with the interface below:

Photo Credit: Kinsta.

You can familiarize yourself with:

  • How to create a new repository (usually done with a plus(+) button)
  • How to view your profile.
  • How to fork a repo.
  • How to make custom settings, etc.

Some Basic Command of the CLI.

Here are some basic useful commands that will used in handling git for Windows, macOS, and Linux.

1. cd — it means change directory. It used to navigate from one directory to another (or folder).

2. dir/ls — this is used to list all the files and folders present in a folder. cd is for Windows while ls is for Mac or Linux.

3. pw — this stands for present working directory. It is used to identify the present directory you are on.

4. mkdir — this used to create a new folder.

5. touch — this is used to create a new file.

6. cls/clear — this is used to clear the terminal. cls is for Windows while clear is for macOS and Linux.

Some Basic Git Command on the Command Line.

To be able to transfer your local repository to the cloud-based repository (on GitHub, etc) using the command line, you need to know the following git commands.

1. git config — this is used to configure username and email to git so as to be able to push your project to your online repo.

E.g. git config — global user.name “Your username”

git config — global email “email address”

2. git init — this command is used to initialise a folder (turn a folder into) a new git respository. You need to navigate into the folder of reference using cd folderName

E.g. git init

remember to name your folder using small letters and hyphens (for folders with two or more names).

3. git add — this command is used to add your project files to the staging area. It is usually written with the name of the files or with just a dot(.), which means to add all the files in the folder. It comes immediately after git init.

E.g. git init index.html //Adds a file in the folder named index.html to the staging area.

git init .(space dot) //Adds all the files in the folder to the staging area.

4. git commit — this command is used to commit (add) all the files permanently into the version history. It usually comes with a commit message written within quotes. The commit message can be about the changes you made to the files such as “updated CSS styles for login page”, etc.

E.g. git commit -m “Type your commit message.”

5. git status — this command is used to check the status of all the files to know which have been committed or not. If the files have are committed, it usually displays green while if it is not committed, it shows red.

E.g. git status

6. git rm — this command is used to remove files that are already tracked by git, both from your working directory and your repository. It just stages the deletion and does not delete the files until the deletion is committed using git commit.

E.g. git rm fileName.

7. git log — this command is used to list the version history, author, date created of the current branch.

E.g. git log

8. git branch — this command is used to list all the local branches that are in the current repository. It can also be used to create a new branch with your preferred name.

E.g. git branch //List all the branches.

git branch branchName //This creates a new branch with the preferred name given to it.

git branch -M main //This renames the current branch to main.

9. git checkout — this command is used to navigate from one branch to another.

E.g. git checkout branchName //switches from current branch to the branch called branchName.

git checkout -b branchName //creates a new branch and switches from the current branch to the branch called branchName.

10. git merge — this command merges the specified branch’s history into the current branch.

E.g. git merge branchName //Merges the history of the branch names branchName into the current branch.

11. git push — this command is used to send the committed changes (from git commit) from the current branch (e.g. master branch or main branch) to the specified branch in your remote repository.

E.g. git push origin master/main //this pushes to your remote repository name origin, in the branch called master/main.

12. git pull — this command is used fetch and merge the changes from your remote repository to your working local repository or directory. This is a combination of git fetch (retrieving changes) and git merge (combining changes).

E.g. git pull repositoryLink

13. git clone — this command helps to copy files from your remote repository into your local machine (computer). This usually makes collaboration with other developers possible.

E.g. git clone urlOfRepository

Note that before you clone into your system, you need to navigate to the folder where you want to copy the file(s) into.

Pushing to an Empty Repository for the First Time on GitHub.

Step 1: Create an empty repository without a README file, a .gitignore file or a licence.

Step 2: Using your command line interface (CLI) navigate to your working folder (or directory).

Step 3: Follow the other instructions in the image below.

Photo Credit: C# Corner

Pushing to an Already Existing Repository (a Repository with File(s).

Step 1: Clone the repo into your system, in the desired folder.

Step 2: Make your desired changes or add a new file.

Step 3: do git add .

Step 4: git commit -m “commit message”

Step 5: git push origin main

Note that for step 5, you can replace main with the name of the branch you want to push to.

Accessing Git Locally.

To access git on your local machine (computer), the following software tools can be used:

  • Git CMD — this tool is similar to the regular Windows Command Line Interface.
  • Git Bash — this tool allows you use all the git features in the command line interface alongside the standard unix-based commands on Windows operating systems. It is useful if you are already used to Linux-based commands.
  • Git GUI — this is a graphical user interface that allows you use git without the commands of the Command Line Interface (command prompt). It gives a visual representation of git commit and branch operations, git repositories, git histories and logs, etc. E.g. Git desktop, GitHub desktop, Sourcetree, GitKraken, etc.

If you are someone who is not fascinated by the command line, you can use one of the Git GUI’s. Although, I will recommend you to always use the CLI as it is fun and used more professionally.

A Recap.

So, at the beginning we discussed how git is a version control tool and how it can be used to keep track of the different versions of your software projects for easy access. We also talked about the basic commands of the Command Line Interface (CLI) that will useful in using git. Again we talked about the basic git commands and how to push your projects to the remote repository. I hope you have gained insights into git during the course of your read.

Happy Coding!!!

If you were inspired by the read, I urge you to give a resounding 50 claps. Thank you!!!

--

--