DEV Community

Cover image for Git & GitHub basics in one post
NDANG ESSI Pierre Junior
NDANG ESSI Pierre Junior

Posted on

Git & GitHub basics in one post

What is Git ?

Git is a distributed version control system (VCS) that helps developers track their codebases, collaborate with others and manage multiple versions of a project.

It was invented and developed by Linus Torvalds, also known for inventing the Linux kernel, in 2005.

Git Distributed means, 2 or more developers can have access on the codebase easily and operate changes on it with ease. The system knowing exactly who did what.

Git Version control means, Versions of the codebase are stored lightly and accessible with a simple command. By then, no matter what happens to the project, we can revert to a specific version that worked fine and continue the development with it. (we'll go through this in the advanced level).

For better summarization, we have organized the understanding at 3 levels; Basic, Intermediate and Advanced. In this post we make emphasis on the basics.

Prerequisite

  1. Download Git on your machine. (https://git-scm.com/downloads)

After installing, open a terminal and type

git --version
Enter fullscreen mode Exit fullscreen mode

The version you installed should be displayed, if not, comment in this post, and we will figure out. πŸ˜„

  1. Configure Git with your credentials (Name and email)

Name:

git config --global user.name "Your name here"
Enter fullscreen mode Exit fullscreen mode

Email:

git config --global user.email "Your email"
Enter fullscreen mode Exit fullscreen mode

That's it ⭐, you're ready to use Git, and therefore follow this tutorial.


Git banner with info of the poster - Essi Junior


1. Git understanding

View Git system as this...

Git banner

To use git in the project, and have this virtual representation set, you should initialize git.

git init
Enter fullscreen mode Exit fullscreen mode

Workspace

It is where you edit and manage your source code. Your code editor or Integrated Development Environment (IDE).

Here you do your coding as you know so well 😎, in JS, Python, Go...

Stagging area

It is where the files supposed to be in a commit (version of the code) are stored.

When you decide to validate a version, you can store the files here. View it like this

From workspace to stagging area

This id done with the command

git add <file name>
Enter fullscreen mode Exit fullscreen mode

or add all files newly created or modified

git add .
Enter fullscreen mode Exit fullscreen mode

When you have your code in the stagging area, you can either remove a file, or validate it to be saved as a version (commit).

To remove a file from stagging area, hit

git restore <file name>
Enter fullscreen mode Exit fullscreen mode

or

git restore .
Enter fullscreen mode Exit fullscreen mode

Local repository

Where all the versions of the source code are stored.

To create a version, save the code in the stagging area to the local repository (do a commit).

Local repository

To do a commit, run

git commit -m "Commit message"
Enter fullscreen mode Exit fullscreen mode

You can view all the commits (versions) by using the command

git log
Enter fullscreen mode Exit fullscreen mode

or to have a better view on the versions, you can run

git log --oneline
Enter fullscreen mode Exit fullscreen mode

This can be helpful for advanced practices.


2. GitHub

GitHub

GitHub is a Git hosting platform (remote repository). This helps other developers in the team to access the codebase and apply changes on it.

There exist other popular hosting platforms, like GitLab and BitBucket. We put emphasis on GitHub as it is the most popular Source

GitHub home page

You need to create your account in order to create remote repositories.

A remote repository is an online space where you store your local repository. This will permit the entire team to have access to the source code.

Local to remote repository

To send the code commited from your local repository to remote, you must first create the repository on GitHub. (Note that you have to login first)

Create remote repo

After creating, you should fill the form with the repository information

Usually name, description, and visibility is okay.

Enter repo info

Then scroll and validate

Validate repo creation

You will be presented steps to follow inorder to connect your local and remote repository.

So depending on your situation, choose your path πŸ‘Œ

Method to connect

If you followed the tutorial, you will follow the second path in your local repository, that is running these commands...

git remote add origin "Your remote url here"
git branch -M main
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

If you refresh your page, you will see your local files on GitHub πŸš€.

Let's break down these commands;

  • git remote add origin "Your remote url here": This sets the connection between your local and remote repository. With that you can send your code to remote.

  • git branch -M main: This renames your local branch name to main.

What a branch ?
It is the path through which your code goes, for it to reach online. The default branch is "main", but can be "master" if you have an old version of git or have specified it at installation.

In this basics, we will consider the branch being main.

You can view it by running

git branch
Enter fullscreen mode Exit fullscreen mode

We'll dive deeper into branching as we will be talking of Git and GitHub at an intermediate level. For know, let's work with the default branch.

  • git push -u origin main: This pushes your code to remote as in the figure below.

Local to remote

The -u flag sets the branch upstream. This means, the next time you will have to push code to remote repository, you will just type git push.

The final part of this blog post is, getting code from remote repository to local.

Remote to local repo

To do that, run

git pull origin main
Enter fullscreen mode Exit fullscreen mode

Note that if you have used the -u flag above, you can archieve this by just typing git pull.

Final though

You can't dev in this era without using Git, practice and get confortable using it, as it is a prerequisite for all devs nowadays. Visit the official documentation for more understandings.

I hope you learned from the post 😊.

If it was basic for you, don't worry, the intermediate level will be done next, and finally the advanced.

Thanks to @javascriptmastery, this article follows a tutorial done by them.

Thanks for reading, see you. 🀞

Top comments (0)