DEV Community

Cover image for A Beginners introduction to Git & GitHub
Open Source Hub
Open Source Hub

Posted on

A Beginners introduction to Git & GitHub

Introduction

Imagine being faced with a situation where for whatsoever reason, after changing a file multiple times, you want to take the document back to the state it was at an earlier time. If you didn’t save that particular version separately, then you would have to edit our file again to return it to the point you need it to be. Git was created precisely to remove this problem completely. You can easily save versions of your files and also connect with an online repository where you can back up those files and also collaborate with other developers.

What is Git

“​​Git is a distributed revision control and source code management system with an
emphasis on speed. Git was initially designed and developed by Linus Torvalds
for Linux kernel development. Git is free software distributed under the terms
of the GNU General Public License version 2.”

In software engineering, version control (also known as revision control, source control, or source code management) is a class of systems responsible for managing changes to computer programs, documents, large websites, or other collections of information. Version control is a component of software configuration management.

Git is not a version control system but it has some similarities to them.

What is Github

GitHub, Inc. is a provider of Internet hosting for software development and version control using Git. It offers the distributed version control and source code management functionality of Git, plus its own features

Wikipedia

Why use Git/Github

You’ve been introduced to the general idea of git and github.

Great project management tool with easy access at anytime
Great collaboration tools as conflicts are handled well by git
Also, your project is open to almost everyone and other developers can contribute to the project.
It is free and open source.
It is fast and small

How to use Git & Github

To start using git, you need to make sure you have it running on your computer. If you are running a Linux system, you probably have it installed. You can check if it is installed by opening your terminal and entering the command below;

$ git –version
Enter fullscreen mode Exit fullscreen mode

This will return the version of git we have installed –if it is installed. If this doesn’t work, we then have to download git on our computer.

For a Linux system, you’ll do something like this;

$ sudo apt-get git
Enter fullscreen mode Exit fullscreen mode

On a mac or windows, you need to visit the git website to download it. Once downloaded and installed, you want to configure git globally. To do that, you need to type the following commands:

$ git config –global user.name “NAME”
$ git config –global user.email “EMAIL”
Enter fullscreen mode Exit fullscreen mode

Make sure to replace USERNAME & EMAIL with your name and email respectively.

After this is done, you need to initialize a local repository. To do this enter the following in the directory you want to have your files;

$ git init
Enter fullscreen mode Exit fullscreen mode

With this done, we have successfully initiated git in our directory.

Let’s proceed on to talk a bit about github now. With git as a wonderful tool for versioning, you certainly have an online repository to deploy these versioned systems to. To get started with github, you need to first create an account on https://github.com. Going through all the processes will bring you to a new page where you can create repositories.

Quick Note: “A repository or repo for short is a storage location for software packages.”

Connecting git with Github

After creating your repo, you should link your local repository with it. To do this, go back to your terminal and enter the code below.

Make sure you replace the user and repo names with yours.

$ git remote add origin https://github.com/<username>/<repo_name>
Enter fullscreen mode Exit fullscreen mode

Now, you can add one or more files to the repository. To make git aware of your file in its current state, you need to add the file(s) to the git staging environment. To do this, you enter the simple git add . command. This command adds all files in the working directory to the staging environment. To add a single file, you can do

$ git add README.txt
Enter fullscreen mode Exit fullscreen mode

You can check your staging environment by using the git status command. This shows the files in the staging environment and in the working directory(yet to be added to staging).

To commit your changes to the repo, you enter the git commit command followed by an m flag to input your commit message.

Here is an example of how to do that

$ git commit -m “My first commit”
Enter fullscreen mode Exit fullscreen mode

Now you can just push our commits in the local repositories to the online repository.

$ git push origin main
Enter fullscreen mode Exit fullscreen mode

With this, your files are backed up to an online repository and properly versioned. At any point in time where you change your files, you can easily commit and push them to github and that automatically saves a new and latest version of your file.

If you need to visit your file at some earlier version, you can easily view that without any hassle.

Conclusion

Having dealt with the basics of git and github, you would want to look at other topics like cloning, branching, etc. I’ll be making another post on that in a few.

Cheers!

Top comments (0)