Understanding the Basics of Git.

maame-codes

Maame Afia Fordjour

Posted on March 17, 2024

Understanding the Basics of Git.

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

πŸ“Œ 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

πŸ“Œ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

Enter fullscreen mode Exit fullscreen mode

πŸ“Œ STEP 4:
Create a repository

$ 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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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!

πŸ’– πŸ’ͺ πŸ™… 🚩
maame-codes
Maame Afia Fordjour

Posted on March 17, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related