Git and GitHub Tutorial — Aryan Irani

Welcome to the Git and GitHub tutorial. In this tutorial, we are going to go through the basics of git and GitHub. Also, we will be covering all the main concepts of git and GitHub that will get you started on your GitHub journey.

We are going to start out by

  • talking about git,
  • followed by installing git and setting it up.
  • After setting up git we are going to create a repository from our terminal
  • followed by adding some files, staging them, and committing them.
  • Next, we will be working with branches where we will be using git merge and git checkout.
  • After learning about all the git commands, we will be working with GitHub,
  • by starting out with a short introduction, followed by creating a repository,
  • pushing codes, pushing codes to branches.
  • In the end, I will show you how to fork, clone and make a pull request.

So let’s get started.

What is Git?

Git is the most commonly used version control system. Git tracks the changes you make to files, so you have a record of what has been done, and you can revert to specific versions should you ever need to. Git also makes collaboration easier, allowing changes by multiple people to all to be merged into one source.

Install Git

Now that we know what git is let’s go ahead and install git. To do that follow these steps:

  1. Go to the official website or click here and install git.
  2. Select the appropriate operating system and click on install
  3. After clicking install, accept all the terms and conditions.

Yayy !! You have passed the first step to learning git! Now let’s go ahead and open up the terminal.

Setting up Git

Before we get into the details of Git, let’s check whether git has been installed on our system. To do that open your terminal and put in the following command.

git — version

On entering this command you should see the following output:

Next, we need to set up some personal details such as your name and email address, so that we can track all the changes, commits made to the codes later. To do that you have to use the following commands:

  1. First, we are going to set the name. To do that use the following command.
  2. git config — global user.name “YourName”
  3. Next, we are going to set the email address using the following command.
  4. git config — global user.email “Your email address”

Here I have set my name and email address. Just to confirm whether the details have been saved, run the following command.

git config — global user.name

On running this command, if your name gets printed, then your details have been saved.

Now that everything is set up, let’s create a new directory where we are going to create some files and publish them to GitHub.

We are going to start off by creating a new folder using the mkdir command, followed by the name of the folder. After creating the folder, change the directory to the folder created using the cd command.

I have done the following here :

  1. Changed my directory to Desktop using the cd command.
  2. Next, I created a new folder using the mkdir command.
  3. In the end, I switched to my new folder using the cd command.

Now that we have everything ready, let’s go ahead and create our first repository.

Creating our First Git Repository

Before we create some code files in the folder, let’s go ahead and create our first git repository. To create a repository, follow these steps:

  1. To create a repository we use the git init command. This command creates an empty repository.

2. Now I am going to add some code files to the folder that we previously created.

3. Go ahead and create a file called new.py in the folder and just put in some python code.

Now that we have made some changes to our folder, we are going to use the git status command. This command displays the state of the repository and the staging area.

What is the Staging Area?

The staging area is the files that are going to be committed to the repository. It is the buffer between the working directory and the project history.

Git Status

On using the git status command we can see that the file that we created is still untracked. This means that we have still not added it to the staging area, after which the file will be committed. To add the file to the staging area, we are going to use the git add command.

Git Add

This is how we add a file to the staging area. Now if we use the git status command once again we can see the following :

Here you can see that the file has been tracked, or in other words, the file has been added to the staging area. At the top you can see that it says No commits yet, this means that the file has been added to the staging area, but not committed yet. To commit a new file to the repository, we are going to use the git commit -m “Commit Comment” command.

Git Log

Now that we have committed the file let's go ahead and use the git status to check the status of the repository.

Here you can see that there are no files left to commit.

Now that we have committed the file, let’s use a new command called git log. The git log command is a tool that lets you review the history of your repository. Using this command you can get the details of all the commits that you have made in your repository.

On running the git log command, you can see the following details :

  1. Hash code [you can use this go back and forth in commits]
  2. Branch name in which the changes are made
  3. Author of the commit [this contains the details that we initialized previously]
  4. Data of commit
  5. The message of the commit

Recap

Now that we have gone over the basics of how to create a repository followed by creating a .py file, adding it to the staging area, checking the status and more. Let’s do the same thing one more time with another file.

Now I have created a new file called multiply.py that displays the product of the elements of a list.

Let’s use the git status command and check the changes made.

Here you can see that the product.py is created but listed in under untracked files. So we are going to send this file to the staging area using the git add command.

Using the git add command we have added the file to the staging area, followed by using the git status command. The git status command tells us that the changes have been added but need to be committed. To commit the file, we are going to use the git commit command.

Here you can see we have committed the file to the repository successfully. In the end, we are going to use the git log command to check all the commits that have been made.

Here you can see, two commits have been made with all the required details.

Git Checkout

The git checkout command is used to switch between branches and commits. It lets you navigate through branches. We will be covering branches in git a little later in this tutorial. The checkout command lets you change the content of the files to those of the specific commit.

git checkout <commit hash/branch name>

After the git checkout command, we either need to specify the commit hash or the branch name in which we want to switch. To get the commit hash we can use the git log command that will list out the details.

Here I have used the git log command to get the details of the commits made. Next, I have used the git checkout command followed by the hash code to which I want to switch.

Now if I go back to my VsCode editor, the product.py has been deleted.This happened because I switched back to the previous commit, where I had only created one file. Using this you can go around commits and check previous versions or previous types of codes that you have written.

To switch back to your original state, you have to use the following command.

git checkout main

Here we have switched back to the original branch. Now if you go back to your VsCode editor, you can see that the product.py file has come back successfully.

Until now, we learnt how you can use the git checkout command to move through commits. Now we are going to take a look at branches followed by using the git checkout command to move through them.

Git Branch

Until now we have been making commits in the main branch. The main branch generally represents the stable version of your code, that contains the code that is released or published. But you would not want to trouble your main code when you are working on a new feature, as it messes the code up. Say you want to add a new feature to your application, but don’t want to trouble your main code, you can create a separate environment to try out this feature. After the feature is ready for release, you merge it into the main branch, thats’ where branches come in. Instead of making a number of commits, you can create a separate environment where you can work on the new feature.

To work on the specific feature we create a new branch using the git branch command which will create a separate environment where you can work on your feature. After your feature is ready and good to go, using the git merge command you can merge the changes into the main branch.

To check the branch you are currently working in, we are going to use the git branch command.

On running the git branch command, we can see that we are currently working in the main branch.

Here I have created a new branch using the git branch command followed by the name of the branch made. Now to check whether the branch is created, we are going to type in git branch, and you should see something like this.

Now that we have created the devcode branch, it’s time to switch to the branch and start making changes to it. To do that we are going to use the git checkout command followed by the branch name.

Extra tip : Want to create a branch and directly checkout into it ? Yes that’s possible using the git checkout -b followed by the name of the branch.

In this branch, I am going to create a new .py that finds the largest element in a list.

Now that we have created the file and made the changes let's go ahead and add the file followed by committing it to this branch. Before adding it I am going to run the git status command to see what changes have been made.

Here you can see that the largest.py file has been created but not yet added. To do that we are going to use the git add command followed by committing the file using the git commit command.

Here I have added and committed the file in the devcode branch. Now using the git checkout command we are going to switch to the main branch.

Now if we come out of this branch and go to our main branch, we can see that the largest.py file no longer exists.

So this tells us that the main branch does not know what’s happening in the devcode branch. But you can solve this using the git merge command, which will merge the two branches.

Git Merge

The git merge command is used to merge the features of the branch to the main branch. To merge the two branches we need to first shift to the new branch that we had created using the git branch command and then merge the two using the git merge command.

Here we have successfully merged the two branches. Now if you go back to your main branch, you can see that the largest.py file has successfully come in.

Now if we do a git log we can see that the file has been added to the main branch from the devcode branch that we created.

What is the use of Branches?

A branch represents an independent line of development. Branches serve as an abstraction for the edit/stage/commit process. If you are working on projects with other people, each one of you can create separate branches and work on the code that is there in the main branch. Once you are done working on your individual codes you can merge them into the main branch.

Introduction to GitHub

Now that we have gone through the main tools of git, it’s time to checkout GitHub. In this part of the tutorial, we will be going through GitHub and how you can push these codes to GitHub.

What is GitHub?

GitHub is a web-based interface that uses Git, the open-source version control software that lets multiple people make separate changes to codes at the same time. It is a simple and easy to use the tool. On GitHub, you can work on projects, keep track of your codes, contribute to opensource and more.

This is my GitHub profile, where I upload my codes, projects and websites that I am working on. You can check it out by clicking here.

Create a repository

To create an account on GitHub just go to GitHub.com or click here. After you have created your account, just go over to your profile and click on New Repositories.

On clicking New repository, you have to fill in some details about the repository that you are creating.

Here you have to fill in the following details :

  1. Name of the repository [Enter the same name of the repo that we had previously created]
  2. Description of the repository [optional]
  3. Private or Public repository

For now, just ignore the other checkboxes and go ahead and click on Create repository.

Pushing Code to GitHub

On creating the repository you will be shown some instructions on how to push and fetch code from the repository that we just created.

Before we start pushing the code, we need to run the git remote add origin command, so that our Git tool knows where exactly we have to push and fetch code from. So we are going to copy the first command and paste it in our Command Prompt.

Here I pasted the first command given, followed by the git remote -v command. This command is just to check whether the origin set is correct or not. Now that we have got the origin it’s time to push the code to GitHub.Go ahead and copy the second command given.

Here I have run both the commands and the codes have been pushed to the GitHub repository.

Here you can see that the three files of code have successfully been pushed to the repository that we created.

Congratulations !! You have successfully created your first repository, followed by using git commands to work through GitHub and in the end pushed your code to your repository.

Pushing Branches to GitHub

All the files in the repository have been successfully added, but the branches that we had created have not been pushed yet.

Here you can see only the main or the main branch has been pushed to GitHub. Using the git push -u origin command we are going to push the second branch (devcode) to GitHub. Before we push the branch, we have to switch to the branch using the git checkout command.

Here I have done the following :

  1. Used the git checkout command to switch to the devcode branch.
  2. Next, I have pushed the devcode branch to GitHub using the git push -u origin command.

Now if we go back to our repository on GitHub, we can see that the devcode branch that we created has successfully come in.

Collaboration on GitHub

Now that we have learnt how to work with Git and GitHub, let’s move towards collaboration on GitHub. There are three ways in which we can collaborate on GitHub:

  1. Make an Open Source. [forking a repository]
  2. Add Collaborators to your repository.

We will be covering both of these ways in-depth in a few minutes. Before that let's take a look at what is Open Source?

What is Open Source?

Open source is a term that originally referred to open source software (OSS). Open-source software is code that is designed to be publicly accessible — anyone can see, modify, and distribute the code as they see fit.

Add Collaborators to GitHub

To add collaborators to GitHub that can make add codes, make changes and more, follow these steps:

  1. Click on Settings and navigate to Manage Access.

2. On opening Manage Access, you can see the following:

3. Here you can add Collaborators by clicking on Add people.

4. On clicking Add people you have to put in the collaborator’s username on GitHub and then click on Add to this repository.

Forking on GitHub

Forking creates your own copy of a repository in a remote location (for example, GitHub). Your own copy means that you will be able to contribute changes to your copy of the repository. To fork a repository follow these steps:

  1. Go to the repository you want to fork and click on the fork button. If you want you can fork my repository by clicking here.

2. On clicking Fork, it will create a new repository with the same code that was there in the source repository. This will be created in your GitHub, where you can make changes, add more codes and do anything you want since this won’t affect the main repository.

Clone a GitHub Repository

Now that we have forked the repository it’s time to clone it on our Desktop and start writing and pushing codes to it using git.

What is git clone?

git clone is a Git command-line utility that is used to target an existing repository and create a clone, or copy of the target repository. We are going to use the git clone command to clone the repository on our Desktop. To clone the repository follow these steps:

  1. Click on Code and copy the link.

2. Go to your command prompt and copy the following command

3. After cloning the repository, I have switched to the repository using the cd command, followed by using the dir command to get the contents of the folder.

Now you can make all the changes you want and add and commit them to the repository that we just created. To add and commit you to have to use the same commands that we have previously talked about.

Making a Pull Request

Create a pull request to propose and collaborate on changes to a repository. These changes are proposed in a branch, which ensures that the default branch only contains finished and approved work. Until now we are making changes in our clone GitHub repository, by creating a pull request we can send a request to the main repository to accept the new changes. To create a pull request follow these steps:

  1. I have made some changes in the file that I have committed.

2. Currently I am one commit ahead of the original repository.

3. To create a pull request click on the Contribute button.

4. Click on Open pull request. On clicking Open pull request, put in the details of the changes made, followed by reviewing the changes you made.

5. After making the changes click on Create Pull request. This will send a request to the owner of the repository.

6. Now if I go back to my main repository I can see that there is one pull request made.

Here I can review the changes made by the user and if I like the change I can merge the changes by clicking on the Merge Pull request button.

Now the changes made by the new user will get merged in the main repository. In other words, this is known as Open Source Contribution. This is how you can contribute to open repositories or work on projects with friends.

Conclusion

This is the end of the Git and GitHub tutorial. We have covered the following:

  1. We started out by talking about git followed by installing and setting up git on your Desktop.
  2. Next, we created our first GitHub repository using the git commands, followed by talking about staging areas and various git commands that helped us in the process of learning git.
  3. Next, we created a repository on GitHub followed by pushing code to it using the git push command.
  4. Then we created separate branches to work on and push the branches to GitHub.
  5. Collaboration on GitHub.
  6. Forking a repository on GitHub
  7. Cloning a repository on GitHub
  8. In the end, after making changes, we created a pull request to the main repository.

I hope you have understood how to use Git and GitHub.If you have any questions you can email me at aryanirani123@gmail.com.

--

--

--

Currently pursuing B Tech at MPSTME. Technical Blogger, interested in Google Workspace. Hope to remain a life long student.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Hosting a blog on Skynet

BeanShell Processor Tutorial 1: Basic Usage

Scotty Beam To Use Unmarshal Infrastructure To Query NFT Data

Are you a product developer or a project developer?

How To Join Data in MongoDB

Converting swagger.json to API on stand-alone

My OSCP Journey

Blogs: Week of 28 Feb — 6 Mar

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aryan Irani

Aryan Irani

Currently pursuing B Tech at MPSTME. Technical Blogger, interested in Google Workspace. Hope to remain a life long student.

More from Medium

A simple D&D Terminal App

Deploy your Nodejs + Auth0 REST API to Cyclic.sh under 4 minutes

How to share our first API

How to use version control to clone & host your Github Repositories.