Git and GitHub: Are Git and GitHub the same? What is a Version Control System? Why Git is necessary? : An Introduction to Repository Management

Git and GitHub: Are Git and GitHub the same? What is a Version Control System? Why Git is necessary? : An Introduction to Repository Management

What is a Version Control System

With a growing collaborative and distributed working environment with frequent changes, it is necessary to record the progress of the application workflow.

As it is difficult to track every individual's code locally, the need for hosting the working code in a centralized application came into play. When working in collaboration, continuous improvement is necessary to address evolving changes, and to maintain the different versions of the working software, seemingly important when a bug arises.

While developing a software product, it is important to keep track of

  • What changes were made
  • Who made the change
  • When the change occurred

What is Git

Git is a version control system that helps to go back in time and track the file progress. Git is used in a distributed environment allowing multiple users to access the code. Git keeps track of the changes being made, has a record of the work done, and reverts to a specific version if needed.


GitHub: A web-hosting application

GitHub is an application that uses Git, allowing one to store git projects on a remote server.
GitHub helps users to download the files from the remote server to the local machine and upload the local files to the remote server using Push/Pull System.
Here, the files and folders are referred to as "Repositories".

A Repository tracks the timeline of the entire project. Including all the previous changes and modifications made.

Local Repository - A git repository that is stored on a local machine.
Remote Repository - A git repository that is hosted on a shared remote server.


Installing Git

Git is software that runs on your local machine.
To create local repositories, download and install Git.
Git is accessed via the command line terminal.

On successful Git installation, the git --version command displays the git version installed.

$ git --version
git version 2.37.1.windows.1

Initializing a Git Repository

To initialize a git repository, the git init command is given on your project folder.

Here, the path to the project folder is /d/Study/Git/Project/MySoftware.

git init is an irreversible command.
Make sure your path to the project folder is correct.

The .git folder is created on the project folder upon initializing the git repository.

elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware
$ git init
Initialized empty Git repository in D:/Study/Git/Project/MySoftware/.git/     

elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)

Git Workflow: The three states of files

Files are created in the Working directory. These files are initially not tracked and are called "untracked files". The state of the files keeps changing in time.

image.png

Image Source

  1. Staged/Tracked - The untracked files are moved to the staging area to track them or to be staged. git add command is used to move the file from the Working directory to Staging Area.

  2. Modified - Any modification made to the tracking files is reflected in the Staging Area. These files are tagged as modified.

  3. Committed - git commit command is used to store all the staged and modified files in the local repository.


Working Directory, Staging Area, and Repository

gitworkflow.png

Image Source

1. Working Directory

Once git is initialized on the Project Folder, it acts as your Working Directory. Working Directory is where you create files and folders to work on the project on your local machine.

Creating Files on Working Directory.

elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
$ touch hashnode.text
$ touch README.MD

The files you create initially in your Working Directory are "not tracked" until you add them to the Staging Area.

git status command is used to check the status.

Here, it returns that untracked files are present and it prompts to add the files to the Staging Area.

elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        README.MD
        hashnode.text

nothing added to commit but untracked files present (use "git add" to track)

2. Staging Area

The Staging Area contains all the modifications made and changes that are going to be committed. At any point in time, the newly created, untracked, and modified files are added to the Staging Area before committing the changes.

git add command adds the files to the Staging Area and are tracked further.

Adding Files to Staging Area

Syntax :

git add [filename] - adds a single file to Staging Area.
git add . - adds multiple files to Staging Area.

elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
$ git add .   // adding new files to Staging Area

elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   README.MD
        new file:   hashnode.text

In the code block given below, hashnode.text has been modified and it prompts to add the file to be staged before commiting the change.

elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   hashnode.text

no changes added to commit (use "git add" and/or "git commit -a")

git add . moves the modified files to Staging Area.

elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
$ git add .

On checking git status, it displays the changes to be committed.

elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   hashnode.text

git commit commits the changes made in the text file.

elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
$ git commit -m "Modified hashnode.txt"
[master e92ab29] Modified hashnode.txt
 1 file changed, 1 insertion(+)

3. Repository

A repository contains all the project-related data, the set of all files, and folders stored on your local machine. The git commit command is used to move the files from Staging Area to the Repository. Repositories are the timeline of the entire project including all previous changes.

Storing Files in a Local Repository

Syntax: git commit -m " [type your commit message] " - to commit the changes.

elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
$ git commit -m "Adding files to Repository"
[master c38a048] Adding files to Repository
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.MD
 create mode 100644 hashnode.text

After the git commit all the files are added to the Repository.

Now upon checking the git status,

nothing to commit, working tree clean implies that all the files are being tracked.

elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
$ git status
On branch master
nothing to commit, working tree clean

git log command displays the series of commits executed.

elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
$ git log
commit e92ab2910ec6761e2c6b27383619b3864f056992 (HEAD -> master)
Date:   Fri Sep 23 01:20:51 2022 +0530

    Modified hashnode.txt

commit c38a0488cb594e502316369f1c0c0fa027b4496b
Date:   Fri Sep 23 00:10:08 2022 +0530

    Adding files to Repository

git log --oneline displays the summary of commits.

elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
$ git log --oneline
e92ab29 (HEAD -> master) Modified hashnode.txt
c38a048 Adding files to Repository
d702494 Added 3 files [html,py,csv]
3457526 Added path for terminal
b17b6c2 -Initial Commit