A Beginner's Guide To GIT & GITHUB

A Beginner's Guide To GIT & GITHUB

New to Git? This article will very soon make you familiar with this version control system and its importance in every programmer's life.

Beginning with an anecdote

Let's talk about your laptop first. In any case:

Gif description

Gif description

Wouldn't it hurt when you lose all your project works made with heart and soul?

Gif description

DON'T WORRY! Version control system is there to save your life. We have got this tool come into the picture.

What is a version control system?

Version control is a system that keeps a track of changes made to a file or set of files over time.

Why do we need it?

Keeping a track of changes to the code is very important in a programmer's life. It allows you to do a variety of things such as:

  • To maintain different versions of the same software (like different versions of an app) that we're working on its development.

  • To ensure that we can recover code changes at any point of time, in case of any damage or downtime of our machine or we want to go back and have a copy of the previous version of our app.

  • To collaborate with others who are working on the same app, without stepping on each other’s toes. Otherwise, if people write code onto the same file, whoever saved first will lose everything he/she wrote because whoever saved last overwrites the file. This doesn’t happen if you use git since it's a version control system.

Gif description

If we say it aloud: GIT & GITHUB. I know what is the first question that crosses your mind.

Gif description

What's the difference between Git & Github?

The answer:

Gif description

  • Git and Github are not the same thing. Git is a tool/software that controls a version control system whereas Github is a website/platform hosting cloud service for git repository.

  • Git application (GUI) is installed and maintained in your local system, gives you a record of your ongoing programming version. You can use it with an interface, and also there is the command line tool (so you can run git commands and use its features from the terminal).

  • Github is exclusively a cloud-based solution giving software developers the power to make revision or edits on an individual's repositories which can be remotely accessed any authorized person. There are other Git repository hosting services also exist; GitLab, Bitbucket, Sourceforge.

Note that: We do not need Github to use Git but, we cannot use Github without Git.

Can we collaborate on a git repository without a hosting system like Github?

Git is used independently on our local machine keeping track of our repos. Using it, yes, we can alternatively collaborate with others on the repo without sharing it on a hosting system like Github since Git is a distributed version system which means anyone can share the code with anyone else without a server. I hope, this makes no confusion now between Git and a web-based hosting service like Github, bitbucket.

Getting started with performing some basic operations in Git

Setup

Click on the download and create a free GitHub account.

Git Configure

After the git installation, we need to enter our username and email address. In the below code, you can remove global keyword in case you want to set the username and email address only for the current repository.

git config --global user.name "yourusername"
git config --global user.email "youremailaddress"

Initialize a git repository

We will run the git init.

git init

Add file to repo

We will run the git add command to add one or more files to staging area.

git add
  • git add <filename> is used to add one file.
  • git add . is used to add all files.

Git status command

git status

This command tells the current state of the repository. Now, we will run the git status command to see if git has added the file to the staging environment. If the files are in the staging area, but not committed, the git status will be "Changes to be committed". And if there are no changes, it will show the message "no changes to commit", working directory clean.

Create a commit

This command saves the changes to our local repository. Now, we will make our first commit and write the message which serves as the purpose for the commit done.

git commit -m "Your message about the commit"

Create a new branch

git branch master

Branches allow you to move back and forth between 'states' of a project. This command is used to determine the current state of the local repository. By default, every git repository’s first branch is named master.

Create a new repository on Github

We will go to the GitHub home page now where we can find the “New repository” option under the “+” sign in the top right corner of the navbar. After clicking the option, we will name our repo and provide a brief description, press the 'Create repository' button to make our new repo.

Push a branch to Github

Since we've already created a new repo locally, we want to push that onto GitHub. So, Git push command will let us transfer the commits or push the content from the local repository to the remote repository on github.

git push -u origin master

If we refresh the Github page now, we'll see note saying a branch with our name has just been pushed into the repository. We can also click the 'branches' link to see our branch listed there. There, we have our project and the commits visible to anyone who can access our repository.

Conclusion

Git and Github are essential tools for every programmer to work with in their everyday lives. There are advance operations in Git that we have not covered yet. However, I hope the introductory part and the basic operations was helpful enough to you for getting comfortable with these tools initially. Thank you for reading, everyone! 🤓

Gif description