Git and GitHub Demystified : A Comprehensive Guide for Version Control System

Git and GitHub Demystified : A Comprehensive Guide for Version Control System

A Comprehensive Guide to Mastering Git Version Control System and GitHub with example

Introduction

Developers, software engineers, tech enthusiasts, blockchain developers, technical writers etc in as much as there will be changes or need to keep track of changes in a project progressively with the possibility of working with a remote team or extending an invite for collaboration GIT and remote repository tools is a must priority to learn. Remote file change synchronization with GIT and collaboration can be achieved using any of the following remote repository platforms: GitHub, Bitbucket, or GitLab. The same git commands can be used across these platforms. In this article, we'll learn in-depth about GIT, the open-source distributed version control system, GitHub, how to install GIT on a Windows machine, how to create an account on GitHub and set up GIT CLI authentication for remote access to an online repository(ies) and the importance of GIT.

Glad to have you here on my blog check out this article I published if you're familiar with GIT before you go: Reflecting on my Journey: Key Strategies I'd Apply as a Software Engineer Starting Over

GIT

Git is the world's most popular version control system maintained by Junio Hamano after its creation by Linus Torvald. It is essentially used to manage code changes, history, who made the change, collaboration, integration of third-party service for project support etc. It is an open-source tool.

Git is an Open Source Distributed Version Control System. Let's take a look at a breakdown of each word to understand what it means.

Open source: the git software is free to use, and available for download, redistribution and modification by anyone.

Version Control System: it can direct, regulate, manage, and keep track of changes in a file with a specific tag or unique ID address to enable ease of reversing back to it anytime.

Distributed Version Control System (DVCS): the DVCS facilitates the provision to enable synchronization between online and offline codebase changes and allows for a full copy of the codebase including the version histories to be remotely accessible and available on the system of the developers who have access to the repository. This allows for different users (developers, coders, programmers etc) to work independently on their different systems and have their changes tracked in one location via the repository commits, branch and merge functionalities. GIT tracks file changes through a distributed version control system.

GIT essentially keeps track of changes in files by organizing and managing them like a computer directory in the form of a repository. The history of the project from the time it was created and synced using GIT can be traced to the most recent changes that have been committed.

GIT is the software that will be installed on your local machine to interact with the GitHub remote storage on the web. This remote storage facilitates collaboration that takes place in the course of developing applications by a team regardless of their location. GIT is a prerequisite for developer roles even if the team works in a physical location.

Check out this link here for more information about GIT.

💡
Follow me on Hashnode: Alemsbaja X: Alemsbaja | Youtube: Tech with Alemsbaja to stay updated on more articles

GitHub

GitHub is a 2008-founded company that creates git-integrating tools. GIT is the backbone of GitHub, and its platform makes it simple to manage GIT repositories online (cloud). It is a service that offers a beautiful web interface for uploading, hosting, and managing your code using Git. GitHub cannot be used without GIT but GIT can be used without GitHub. GitHub has free and enterprise options. Some GIT commands can be performed on the GitHub web browser interface.

How to Install GIT

The GIT software is available for download on this link for different operating system.

This tutorial guide covers GIT installation on the Windows operating system. Head over to the folder where the downloaded GIT software is located and double-click to begin the installation process.

Next, click yes on the pop window and the following screens with the next option as shown in the image below.

  • Select a folder for the GIT program shortcut.

Select your preferred default editor to be used by GIT when you execute the code . command via the command prompt interface (GIT CLI)

  • Next, choose the name for the initial or default branch or allow GIT to choose for you.

  • Select the recommended option for using GIT from the command line and also from 3rd-party software. This implies that GIT commands can be executed from any command line interface (command prompt, Powershell etc) on a Windows machine.

  • Select the use bundled OpenSSH to use the ssh.exe that comes with GIT.

  • Select "use the OpenSSL library" and click next.

  • Select the "Checkout Windows-style, commit UNit-style line endings" to define how GIT should treat line endings in text files.

  • Select "Use Windows' default console window" and click next.

  • Select the Default option for fast-forward or merge and click next

  • Select to use the GIT credential manager and click next

  • Select to enable file system caching

  • Select the configuring experimental options based on your setup and stack preference and click install

  • Installing.......................

  • Successful installation --- all good!!! Let's GIT

  • To confirm the installation, open a command line interface and run git as shown in the image below.

  • To check the version of GIT via the command line interface
git --version

#git version 2.16.2.windows.1
  • Configuring GIT via CLI
git config --global credential.helper cache

... which tells git to keep your password cached in memory for (by default) 15 minutes.

GitHub Account

Let's set up a GitHub account before proceeding. Feel free to skip to the next section if you already have a GitHub account.

You can Login or Signup to GitHub by following the easy steps. Obtain your email and user_name to continue the GIT configuration on the CLI.

GIT configuration

  • Add the username
git config --global user.name "your_username"
  • Add the email you used on GitHub or any online repository.
git config --global user.email "your_email_address@example.com"
  • To check the configuration
git config --global --list

Creating a GitHub repository

Next, Let's create a repository on GitHub, you can skip the section if you already have a repository.

  • Define repository name and settings

GitHub access Link

Here's a breakdown of the repository URL access link:

#SSH access link
git@github.com:github-account-name/repository-name.git

#HTTPS access link
https://github.com/github-account-name/repository-name.git

#GIT CLI access link
gh repo clone github-account-name/repository-name

or github.com essentially pointing to git as the source or base to check for the resource on the link.

GitHub-account-name is the same as the username defined when creating a GitHub account.

repository-name is the name defined when setting up any repository on GitHub.

The URL is saying to look up GitHub on the User with name find this repository name and grant access to the command.

Since we have an online repository, let's head back to the command line and do some stuff to sync with the remote repository.

The touch & echo command

The touch command can be used to create a file in the GIT bash within the folder path of the project you want to synchronize with GitHub.

touch README.md
  • You can create a file and add text using the echo command
echo "# git-developer" >> README.md

Basic commands and Authenticating GIT CLI for GitHub remote access.

  • The git init is used to initialize a folder (new or existing). After this command is executed a .git folder is created which is hidden by default except if the settings of the PC have been altered to show hidden files.
git init

  • The git status command can be executed at any time on the GIT CLI to confirm the status of the file changes. It could be staged, committed or nothing to track.
git status
  • The git add is used to stage files. The git add . is used to stage all the changes while the git add file_name is used to stage a particular file.
git add file_name 

OR

git add .
  • The git commit is used to commit the changes with a message to identify the version of the commit. Every commit has a unique ID. This commit tag | ID can be used to reverse the changes in it. The git commit -m "message" is a one-liner for committing messages. The git commit opens a commit tab on the code editor to enable a detailed description of the commit with the accompanying commit title. A commit is an entry in your code that shows the modifications you have made since your last commit.

NB: Ensure to write a good commit message.

git commit -m "First time using GIT"

#open in the code editor for detail description of the commit messgae
git commit
  • The git branch is used to manage different branches of the repository. A few advantages are that it can be used to manage different features of an application and it can enable for testing of changes before they are merged into the most stable version of the repository. The git checkout branch branch_name is used to create a branch.
# -M is used to specify that branch main is the main branch of the repository
git branch -M main

# to name a branch
git checkout branch branch_name
  • The git remote add origin is used to add the origin that the changes should be traced to or can be used to manage the changes from the online version. The link obtained from the repository you wish to sync communicate to can be added after the origin keyword as shown below:
git remote add origin https://github.com/github_name/repo_name.git
  • The git push -u origin branch_name is used to push a copy of the local repository to its corresponding remote repository as defined by the remote origin URL
git push -u origin main

If you're executing this command for the first time after installing GIT after creating an account on GitHub this pop-up window will be displayed for authentication:

You can use the browser where your GitHub account is logged in or a device or preferably choose the token option as shown below:

Next, go to your profile to obtain or create a token

Next, click on developer settings

Next, click personal access token and click token classic

Next, define the scope (access and modification authorization) for the token

Next, click generate

Next, copy and save the token.

Paste the token copied from this page on the token session of the popup window.

Upon successful authentication, the local repository files are pushed to or synced with the online copy of the repository.

Follow the GitHub CLI manual authentication documentation for detailed GitHub CLI authentication.

More GIT Commands

  • The git log command is used to show the logs of the git commits with their timestamp, title and author.
git log
  • The git pull command is used to retrieve the remote copy of the repository to the local copy repository regardless of the location with the right access.
git pull origin
  • The git merge command can be used to combine the separate development branches that the git branch command produced into a single branch by using the git merge command. It'll produce a new commit by merging the changes from two branches.
git merge
  • The git clone command is used to clone the copy of the remote repository to the local machine. Unlike Private repositories that need access permission or addition for cloning, public repositories can be cloned without the owner's permission. Ensure to select the appropriate visibility option based on the repository confidentiality.
git clone repository_url

These basic commands are good to get started while learning other commands along the line.

.GITIGNORE FILE

Just as the name implies git ignore file(s). Public or private repositories have files that shouldn't be added to the GIT repository.

The files or folders to be ignored are added to the gitignore file. Files or folders such as node_modules, vendor can be added to be ignored by GIT in the gitignore file. Environment variable files or any file containing secret keys and credentials should be added to the gitignore file (public or private repository) to avoid public access or unauthorized access to credentials.

Importance of GIT & Remote Repositories (GitHub, Bitbucket, GitLab)

  • Team collaboration regardless of location.

  • Keeps track of changes in a file.

  • Actions/scripts to enable Continous Integration / Continous Deployment

  • Features release of a product can be managed by creating branches

  • It can be used by developers, designers, customer support, technical writers etc.

  • GIT is a free and open-source distributed version control system.

  • Remote repositories keep a copy of the local repository online (cloud)

Recommended Resources

GIT Videos

GIT Book

About Github

GIT Tutorial

Pro Git by Scott Chacon and Ben Straub

Conclusion

GIT is a distributed version control system software. It is essentially used to manage code changes, history, who made the change, collaboration, integration of third-party service for project support etc. GitHub is an example of online remote repository tool that is built on GIT. Feels like a whole lot going through the article right? Mastering GIT and its commands takes a while. Please apply caution when running commands and ensure to ignore credentials and secret files in the .gitignore file. A few commands are important to know at this basic level when starting on GIT and they're the most commonly used when keeping track of file | folder changes while collaborating with a team on a project.

Before you go, I have an interesting article worth your time on Top recommended tools every developer should have in their locker and many other fantastic topics simplified on my blog here.

Find this helpful or resourceful?? kindly share and feel free to use the comment section for questions, answers, and contributions.

💡
Follow me on Hashnode: Alemsbaja X: Alemsbaja | Youtube: Tech with Alemsbaja to stay updated on more articles

Did you find this article valuable?

Support Alemoh Rapheal Baja by becoming a sponsor. Any amount is appreciated!