Getting Started with Git and GitHub: A Complete Tutorial for Beginner

Getting Started with Git and GitHub: A Complete Tutorial for Beginner

Riya Aggarwal's photo
Riya Aggarwal
·Aug 8, 2022·

9 min read

Subscribe to our newsletter and never miss any upcoming articles

Play this article

Table of contents

Hey techies 👋 . Looking to get started with Git and GitHub?

In this blog, We will discuss how to use Git and GitHub as a beginner, various git commands, and their use. We will learn to create a repository, push code, create branches, issues, etc. on GitHub. So let's begin.

Git and GitHub are very important development tools that every developer must know. So, without wasting any further time, let’s start learning it.

To get the exact knowledge of Git and GitHub, you first need to know about version control.

Let’s get started!

What is Version Control?

Let's first understand what is the Version Control system

Developers can freely contribute to open-source projects and check the latest version of codes with the help of a version control system.

Basically, it’s a system that permits you to record changes to files over time, thus, you can view specific versions of those files later on.

A version control system, or VCS, keeps track of the changes made while individuals and groups work together on projects. Teams may run tests, patch issues, and add new code as the project develops knowing that any version can be retrieved at any time.

Now, after, knowing about version control, it will be much simpler for you to comprehend Git.

Just before we move ahead, let's look at how Git and GitHub work and how they are different from each other.

Git vs Github

WhatsApp Image 2022-08-08 at 6.07.35 PM.jpeg

Similar to DropBox or Google Drive, GitHub is an online program that lets users host files, but it is made specifically to host Git Repositories. Programmers typically use Git, a command-line tool, to control the versioning (history) of software projects. Every project is housed within a Git Repository, which keeps track of all modifications.

Note - We can use Git without GitHub but vice-versa is not true. GitHub has several alternatives like GitLab, BitBucket, and more.

image.png

What is Git?

Git is designed for text-based data, for instance codes, books, papers, article, etc.

Git is a popular version control system. To make it more clear, let’s think of it as a caretaker that will take care of your project and this caretaker has many superpowers, let’s see some of them.

  • It is used for tracking changes in your code.
  • It is used for tracking who made those changes.
  • It is also used for code collaboration In short, it is a system in which your project is kept.

What is GitHub?

We may infer from the name that it is a Hub with initiatives, communities, etc. GitHub is a hosting service for Git repositories that offers a web-based graphical user interface. It is the world's biggest community. When a project is open-source, its specific repository is made public and receives numerous contributions.

We can access Github central repository via HTTPS or SSH.

The key benefits of GitHub are as follows:

  • It is easy to contribute to open source projects via GitHub.
  • It helps to create an excellent document.
  • You can attract recruiter by showing off your work. If you have a profile on GitHub, you will have a higher chance of being recruited.
  • It allows your work to get out there in front of the public.
  • You can track changes in your code across versions.

How to Install Git?

Git installation on your PC appears to be simple. However, it also relies on your operating system. To install git on your system go to the official website and install the latest version of git on your operating system or if you are a Linux user then, you only need to open their preferred terminal and run the script as follows:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install git

git-1.png

After downloading it,you just need to install the software and click next. Now, once you have installed the software you can check whether it is successfully installed or not by using the git version command.

After installation, Check the git version with the following command. Type this command in the Command line / terminal or gitbash.

git  -- version

The output of the above should look like this

git-2.png

This indicates that you have successfully installed git.

Now you’re ready to start using Git on your system!

How to use Git?

If you are using git for the first time , then you need to set up your email and username using config commands. To do so, use these commands in terminal/cmd .

git config -- global user.name “your_username”
git config -- global user.email “your_email”

Now, if we want to check whether our email and username have been set up or not, then use these commands

git config -- global user.email
git config -- global user.name

git-3.png

To use git, developers use specific commands to copy, create, change and combine code. Let’s learn and study these commands.

Git Initialization

git init initializes a new git repository. It adds a hidden subfolder within the existing directory that houses the internal data structure required for the version control. To use git commands, you have to use a command inside that project folder in the command prompt.

  • Create a folder “git-tut”. And open the folder in the cmd (if using Windows ) or terminal (if using macOS or Linux).

git-4.png

Tip💡- Always make sure you are inside the correct folder. You can also run git help to know more about the specific command.

✨Now to use Git, the command is:

git init

git-5.png

After using this command, you won’t notice any change in your git-tut folder but a hidden file has been added with the name .git.

git-6.png

Now, you can use any git commands in this project. Hence, in order to use git commands in your project, you first need to convert your project folder to a git repository by using the “git init” command.

Git States

git-7.png

So, from the above picture, we can see that there are 3 states in git

  • Modified
  • Staged
  • Commit

When you make a change in a file, the changes are saved in the local directory but they are not a part of the git development history. To create a commit, you first need to stage the changed files.
When the file is added to Git using git add "file_name" it's in the staged state. In this state, all the necessary changes have been made and it is ready to be committed. You can add or remove changes in the staging area.

When we make changes in a committed file it goes into the modified state.

Important Git commands that you'll use most frequently

git Add :

It stages a change. To make Git know about the files you have added to the folder, you have to add those files in Git as well.

git add .  // Used to add all your files to the stage (the place where you can pick which files to commit at once)
git add <filename1> //  Used to add the  particular filename mentioned

git-8.png

Here you can see that now Git recognizes that I have added a new file.

git Status

It shows the status of changes as untracked, modified, or staged. In short, we can say that shows the list of changed files or files that have yet to be staged and committed.

The tracked files are shown with green files whereas untracked files are shown in red color.

E.g : Tracked File

git-9.png

Untracked File

git-10.png

git Commit

Now, After adding all the files, we will create our first commit using the command. Commit command saves the snapshot to the project history. It functions like taking a photo.

To make it more clear, commit means to lock or finalize the changes.

Let’s create our first commit using the following command:

git commit -m "commit message"

git-11.png

git diff

What if I want to know what all changes have been made? So, in this situation, I’ll use the git diff command to track the difference in the changes made on a file.

git-12.png

Here, the + sign symbolizes the changes added.

git branch

Git branching is a very powerful feature and an important one to know. Git Branching is a series of changes.

Each developer has his/her task, and by using branches, they can work on the new feature without the interference of other teammates. Once the task is finished, you can merge new features with the main version (master branch). And this is known as the merging of a branch.

git-13.png

Now, to check branches in your repository,

git branch

It will list down all the branches contained in the repository.

  1. If we want to switch to a new branch, then we will use the following command:

git checkout -b "BranchName"

git-14.png

  1. To delete a branch, we use the following command
git branch -d "BranchName"

git-15.png

Creating A GitHub Account

git-116.png

Hop on to GitHub and create your own GitHub account that you will use to create remote repositories and get started with your Open - Source Journey.

Tip💡- If you're a student, do consider getting a GitHub Student Developer Pack which provides you with different tools and resources to head start your tech journey.

Creating a Repository On GitHub

  • Step 1

Click on the "+" And select "New Repository".

git-24.png

  • Step 2

Enter a name for your repository and click create a repository by leaving everything on default.

✨Your repo is created. Now, you can start working on it.

git-23.png

  • Step 3

Connecting your local repository to a remote repository

The GitHub repository we have created is empty, we have to upload our project into it. To do this we have to connect our remote repo(called origin) with our local repo. To do this, we need to copy the remote repo link.

git-20.png

To check if the remote is added successfully or not use the following command

git remote -v

git-21.png

  • Step 4

Now, it's time to push our code which will send the local commits to the remote branch of the repository.

git push origin main

git-22.png

Now, once you go to your remote repository and refresh the page you will see that the code is uploaded on GitHub.

Conclusion

GitHub is your friend. You must become knowledgeable about the fundamentals of using this effective weapon since it is here to stay and won't be going away any time soon. You cannot afford to put off acquiring this skill as a new developer, and the more you know now, the more prepared you will be for your future employment.

Git And GitHub are important for every developer. The aim of this blog was to get you started with Git and GitHub and teach you some basic git commands. After successfully completing this tutorial it's time for you to take off the training wheels and explore these tools on your own. Please leave a remark or question in the space provided below.

Welcome to the open-source community through Git and GitHub, where every idea takes the form of a Pull Request.

Happy Learning!

 
Share this