Git And GitHub — A Beginner’s Guide

Madhur Vashistha
Software Incubator
Published in
10 min readApr 15, 2020

“Success is just an outcome of exploring the endless possibilities.”

Git is one of the most popular version control systems out there. Proper knowledge of git helps you become more efficient and work conveniently especially when working in a team. It is also one of the essential skills for a good software engineer.

A bank is utilized to keep your finances and maintain it likewise Git and Github is the bank of world developers. This is the very step towards professionalism in the world of code.

So, if you are new and are looking to understand Git and GitHub, this is the right place you have landed. So, let’s get started.

What is Git and GitHub?

Git is a distributed version control system for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows [source — wikipedia].

First, let’s understand what a version control system is?

In simple words, version control can be defined as the track of amendments in a document, information, a computer program, etc.

A distributed version control(VCS) is one in which the complete code or project and its history (including the commits and changes) is present on every developer’s computer.

Thus, in simple words Git is a software to keep track of your project, which means you can keep track of the changes you made in your code or any file, you can add or remove files you created and maintain the full history of your work.

Other features of Git include:

  • Allowing many people to work on the same project and let them see the changes made by others.
  • Data integrity.
  • Easy access to the source code
  • It helps to find bugs as it keeps the history of the changes to the code.

Now, many times the names Git and GitHub are used interchangeably. But actually, they are two different things. Git is a tool to create a local repository on your system and make changes to it whereas GitHub is a website to host a repository on a remote server. Git is what you will locally use on your system to commit changes to a remote repository which will be present on a remote server and which you can access through your GitHub account.

So this now, that you’ve got a simple idea about what Git and GitHub are. So let’s now understand what a repository is:

A repository (shortly pronounced as repo) is a space where you keep your project or where your project is actually present. It can be a local folder of your computer or a space on GitHub. You can keep any kind of file in a repository, including your codes, images, text files or any other kind of file. Repositories are mainly of two types:

i) Local Repository — The repository on your machine is called a local repository.

ii) Remote Repository — The repository on the remote server or the one on your GitHub account is called a remote repository.

So after understanding Git and GitHub now let’s move on and understand some basic Git commands. Now let’s understand some basic Git commands and how to use them. Follow the steps given below, so that you can completely understand the flow and working.

Step 0: Setting Up Git and GitHub

First, install Git to your system and create a GitHub account. Then, you need to configure your git with your GitHub account.

Open terminal or git bash and type the following commands:

git config --global user.name “Your GitHub username”git config --global user.email “Your email address”

Step 1: Create a local Git repository

  • First, create a folder where you will store your project.
  • Then open the terminal or Git Bash to use the following commands for creating a local repository.
cd <your-project-folder-name>    // To move to the project folder.git init           // Initializes a local repository to the project.

The “git init” command initializes your project folder with a local git repository.

Add Some Files To Your Project

Now to understand the following steps, first add some files to your project folder. For example, let’s add a text file to the folder, say file1.txt.

Before Moving Forward!

Before moving further, first, let’s understand what is staging and what is a commit.

A commit is just a track of the changes you make to your project along with a message that you give in relevance to the changes you made. A commit helps you get back to any state of your project. Simply, it is a record that at what time, what was the state of your project.

Before you add your files or code to a commit, it needs to be in the staging area. The staging area keeps track of the files which need to be committed or which are changed but not added to the repository. This gives you the control to select which files are to be committed.

Step 2: Staging

Now let’s add our “file1.txt” file to the staging area. To do so, use:

git add file1.txt

If you want to add all the files present in the folder to the staging area then simply use:

git add .

Step 3: Committing

Once you have added your files in the staging area, you can commit them now.

To commit the files present in the staging area use the following command:

git commit -m “Your commit message”

Written in the inverted commas is the commit message which you write in relevance to the changes you made to the code or files in a particular commit.

Step 4: Setting Up The Remote repository

Until now you have just added your files or code to the local repository. No one will be able to see your files or code in any other system than yours. So we need to add the files or code to the remote repository so that others can view them, clone to their systems and add files to your repository if you want them to do so. This is where GitHub comes into action.

On the GitHub homepage, you will find a New Repository option. Click on it and create a new repository. Give a name to your repository.

  • Now you can sync your local repository with this remote repository.
  • This remote repository allows others to view your files and clone them to their system.
  • It stores your project on a remote server, so if you ever lose the project folder from your system, you can get all your files back from this repository.
  • For this, you need the URL of the remote repository, which you can find as shown in the image below.

Copy this URL and to point your local repository to the remote repository use the following command:

git remote add origin <remote-repository-url>

Step 5: Pushing

To push the code means to add the code to a branch in the remote repository. To do so, use the following command:

git push -u origin master

After the first commit, you can simply use

git push

to push the changes to the remote repository.

So, the normal workflow can be understood as:

  1. Do some programming
  2. Check what you have changed.
  3. Add the files to the staging area.
  4. Commit the files.
  5. Finally, push them to GitHub.

Branches

Now, let’s move to a bit advanced topic of Git, i.e, branches.

Branching is one of the most important and useful concepts of git. Every repository contains one branch by default, which is the “master” branch. Up to this point, you’ve been working on the master branch, which is the only branch in your repository.

Essentially, a branch allows you to create different states of your project without affecting one another.

For example, you want to add a new file or make some changes to your code, but don’t want to affect the work you have done till now. This is where branches are used. You can create a new branch from any commit on any branch. The new branch contains all the files of the branch from which it has been created. You can do your stuff on the new branch. This will not affect your code on any other branch, say the “master” branch. Generally, the main code is kept on the master branch and new features or changes are implemented using new branches and then finally merging these branches into master after the work has been done.

Branching

Branches are useful when two or more people are working on a project or in a repository. They can simply make branches of their own, do some work and then merge branches into a single branch that contains the contents of both the previous branches.

To get the list of all the branches in your repository use:

git branch

To create a new branch from any branch, you first need to be on the branch from which you want to create a new branch. For simplicity, let’s say we want to create a new branch from the master branch.

Since we are already on the master branch we can directly use the following command:

git branch <branch-name>    (without angular bracket)

This creates a new branch that will contain all the files of your master branch, but any further changes done on this branch will not affect the files on the master branch.

Now, to start working on the new branch you need to switch from master or previous branch to the new branch. For that, use:

git checkout <branch-name>

You can now make changes to the files on this branch without affecting the files on the master branch.

You can also use below shorthand command to do the above two mentioned tasks in a single step i.e create the new branch and switch to it:

git checkout -b <branch-name>

Merging

After you implement a new feature on a branch you’d like to merge this branch with any other branch, say the “master” branch. You can do this with the help of a git merge.

To merge a branch into another, you first need to checkout to the branch in which you want to merge the other branch.

For example, suppose we want to merge a branch into master. So first we need to checkout to the master branch, i.e

git checkout master

And then we can use the following command:

git merge  <name-of-branch-to-be-merged>
Merging

Now, when we merge two branches git first checks for the differences in the files of the two branches, line by line. These differences sometimes lead to what is known as a merge conflict.

For example, consider you have file1.txt on the master branch. Suppose the first line of file1.txt contains “This is the first line”. Now, let’s say you created a new branch and changed the content of the first line to “This is the second line”. Now, when you merge these two branches, it will create a conflict because git doesn’t know what should be the content on the first line. Hence, it will generate a merge conflict and will not complete the commit. Otherwise, if there is no conflict found, git will simply merge the files and create a commit.

Merge Conflict

In case of conflict, git inserts appropriate conflict markers to the files having a conflict. You can resolve the merge conflicts with the help of these conflict markers. Open the file in any editor of your choice and it will look like:

Now, you need to decide the content of the lines where the conflict has occurred. Keep the required content and remove all the remaining lines and markers and finally commit the changes.

After the commit, your branch will get merged to the master branch.

Few More Commands:

Git Status

If you want to get the details about what files have been modified and what files are in the staging area then use this command as:

git status

Git Log

To know all the commits which have been done yet, use:

git log

Git Clone

You can clone an existing repository to your system, you can use the following command:

git clone <remote-repository-url>

Git Pull

If you want to get the latest changes from the remote repository to your local repository use the following command:

git pull origin master

This is generally used when you work in teams and other developers commit changes to the repository regularly.

What’s Next?

And that’s the basics of how to work with Git and GitHub. But there is much more ahead you need to learn. Now explore more about Git and also how to use it efficiently. Having a good knowledge of a version control system helps you a lot in better development as it makes the development process faster, convenient and gives you more control over your code.

Also one thing about both Git and GitHub — the community behind them is extremely supportive and generous in the sheer volume of information contributed. You’ll be in good hands. Now, it's your time to explore and step forward as an expert in the field of Git and GitHub.

--

--