Understanding the Basics of Git.
Tags: webdev, git, beginners
Published: 2024-03-17 11:57:57

## INTRODUCTION

Linus Torvalds developed the free and open-source Git version management system back in 2005. Git is a distributed version control system; each developer has access to their code repository's complete history locally, in contrast to more traditional centralized versions like SVN and CVS. This results in a slower first clone of the repository, but significantly faster commit, blame, diff, merge, and log actions afterwards.

Git has outstanding support for branching, merging, and rewriting the history of repositories, which has produced a number of powerful and creative workflows and tools. One such well-liked technology that helps teams work together on Git branches and quickly assess each other's code is pull requests. Currently the most used version control system worldwide, Git is regarded as the industry standard for software development.

## HOW DOES GIT WORK?

📌 Create a "repository" (project) with a git hosting tool (like GitHub)

📌 Copy (or clone) the repository to your local machine

📌 Add a file to your local repo and "commit" (save) the changes

📌 "Push" your changes to your main branch

📌 Make a change to your file with a git hosting tool and commit

📌 "Pull" the changes to your local machine

📌 Create a "branch" (version), make a change, commit the change

📌 Open a "pull request" (propose changes to the main branch)

📌 "Merge" your branch to the main branch

It's important that you comprehend what Git commands mean before we begin working with them.
And in Git, we are going to be working with Repositories a lot. 

## What is a repository?
All that a repository, sometimes known as a repo, is is a group of source codes.

## Four essential components comprise the Git Workflow.
📌 Local repository 
📌 Remote repository 
📌 Staging area 
📌 Working directory

There are three distinct states that a file in your working directory could be in.

📌 It can be STAGED. Indicating that although the files containing the updated modifications are tagged as committed, they have not yet been committed to the local repository.

📌 It can be MODIFIED. This indicates that the changed files have not yet been added to the local repository.

📌 It can be COMMITTED. This indicates that the local repository contains a safe copy of the modifications you made to your file.

## COMMON TERMINOLOGIES USED IN GIT:

📌 To add a file from the working directory to the staging area, use the git add command.

📌 The command "git commit" adds every staged file to the local repository.

📌 To add every commit made in the local repository to the remote repository, use the git push command. 

📌 Anybody having access to the remote repository can thus view all files and modifications there.

📌 The command "git fetch" is used to obtain files into the local repository from the remote repository, not the working directory.


Now that we are familiar with the basics of Git and its terminology, let's look at how to add a file to the repository. We're going to approach it both the hard and the proper ways. without using any graphical user interfaces.

## STEPS 

📌 STEP 1:

Create a GitHub account here [](https://github.com/) 

📌 STEP 2:

Download Git on your machine;

Open the terminal on your Mac and type the following command:

`$ git --version`

If you don't already have git, this will prompt you to launch an installer. So use the installer to configure it. It will simply display the installed version of git if you already have it.

In the terminal, type the following if you are using Linux (deb):

`$ sudo apt install git-all`

If you are on windows:

Go to this link [](https://gitforwindows.org/)

📌STEP 3:

Tell Git who you are;

```
$ git config --global user.name "YOUR_USERNAME"

$ git config --global user.email "your e-mail"

$ git config --global --list # To check the info you just provided

```

📌 STEP 4:
Create a repository [](https://github.com/new)

`$ cd Desktop/Name of your repo`

and initialize Git;

`$ touch README.md    # To create a README file for the repository
$ git init           # Initiates an empty git repository
`

Now you edit the README.md (markdown) file to provide information about the repository.

ADD FILES TO THE STAGING AREA FOR COMMIT:

Now to add the files to the git repository for commit,

`$ git add .  
# Adds all the files in the local repository and stages them for commit

OR if you want to add a specific file

$ git add README.md 
# To add a specific file`

Then you check which files are staged. To do so, you type in:

`$ git status # Lists all new or modified files to be committed`

Now you need to commit changes you made to your Git Repo;

`$ git commit -m "First commit"
# The message in the " " is given so that the other users can read the message and see what changes you made`

📌 Add a remote origin and PUSH

Your files will no longer be automatically updated on GitHub every time you make changes to them and save them. Every modification we make to the file is current in the local repository. To update the modifications to the master now:

`$ git remote add origin remote_repository_URL
# sets the new remote`

You can establish, examine, and remove connections to other repositories with the git remote command.

`$ git remote -v
# List the remote connections you have to other repositories.
`

The changes in your local repository are now being sent to the remote repository you designated as the origin using the git push command.

Now, if you check your repository page on GitHub, you will see the change.

## Seeing the Changes You Made to Your File:

The file won't match the most recent version that was committed to git once you begin making changes to it and save them. View the modifications you recently made.

`$ git diff # To show the files changes not yet staged`

Return to the most recent version that was pushed to the Git repository.

You can now opt to go back to the most recent version that was committed by typing:

```
$ git checkout .

OR for a specific file

$ git checkout -- <filename>
```

View Commit History: To view the history of the commits you have made to your files, use the git log command:

`$ git log`

The most typical command flow is as follows, which you should use each time you make changes that you want to appear on GitHub:

```
$ git add .
$ git status # Lists all new or modified files to be committed
$ git commit -m "Second commit"
$ git push -u origin master
```

We can now determine whether the commit was successful by visiting our repository and examining the commit messages associated with each file.

This is just a summary on how Git basically works. I hope you learnt something from this post, cheers!