Introduction to Git and Github

Prateek Agarwal
ArIES IIT Roorkee
Published in
13 min readDec 26, 2020

A brief overview

  • Introduction and Installations
  • Git Introduction & Features
  • Git Commands
  • Github Introduction & Features
  • Conclusion

Introduction and Installations

Git is a distributed version control system (DVCS), where each developer has their own copy of the project and periodically pulls in other users changes and pushes their own. Github is a website that hosts git repositories and offers ways to interact with the repository.

Install and configure Git on your PC, and make an account on GitHub. To install Git on your PC, head over here and run the installer with the default components selected and just step through the installation. This means you don’t need to change any of the options or selections.

Full reference is available: here

Git Introduction and Features

What is Git?

It is an Open Source Distributed Version Control System.

  • Control System: This basically means that Git is a content tracker. So Git can be used to store content — it is mostly used to store code.
  • Version Control System: The code which is stored in Git keeps changing as more code is added. Also, many developers can add code in parallel. So Version Control System helps in handling this by maintaining a history of what changes have happened. Also, Git provides features like branches and merges, which I will be covering later.
  • Distributed Version Control System: Git has a remote repository which is stored in a server and a local repository which is stored in the computer of each developer. This means that the code is not just stored in a central server, but the full copy of the code is present in all the developers’ computers. Git is a Distributed Version Control System since the code is present in every developer’s computer. I will explain the concept of remote and local repositories later in this article.

Why a Version Control System like Git is needed

Real-life projects generally have multiple developers working in parallel. So a version control system like Git is needed to ensure there are no code conflicts between the developers.

Additionally, the requirements in such projects change often. So a version control system allows developers to revert and go back to an older version of the code. Finally, sometimes several projects which are being run in parallel involve the same codebase. In such a case, the concept of branching in Git is very important.

Using Git in a Project

After you initialize an empty git repository in any directory, it divides your project into three parts: 1. Working Tree 2. Staging Area 3. Git Repository.

  1. Working Tree- This is the project directory that we see. Git will track all the files we add/edit/delete.
  2. Staging Area- The stage area is for reviewing files to prevent from committing unnecessary files. We move files that we actually want to save in a commit, to the staging area before being finally committed in the git repository. Think of it as an intermediate state.
  3. Git repository- This is the .git directory where git stores all the project information. This directory simply contains all the implementation details as to how git works under the hood. If you delete this .git directory, you’ll lose all your project history

Git Configuration

  1. Open Git bash terminal on your desktop by right-clicking and select ‘Git Bash Here’ or you can search ‘Git Bash’ and open it.

2. Enter the following commands (remember to enter your own name and email ID).

You have successfully configured Git and are good to go!

Git Commands

  1. Make a new directory (aka folder)- mkdir foldername
  2. Move into that folder- cd foldername
  3. Make a new file say testfile.txt- touch testfile.txt. The ‘touch’ command is used to create a new file. Here we created a text file with name ‘testfile’.
  4. Open the file in some text editor installed on your PC-
  • For Notepad use- notepad.exe testfile.txt
  • For Visual Studio Code use- code testfile.txt
  • For Atom use- atom testfile.txt
  • For Sublime Editor use- subl testfile.txt

5. Add some text in this file and save it and then go to the terminal and use Ctrl+C to exit the command and enter a new line.

Now let's actually start implementing some Git Commands!!

1) git init

  • This would initialize a git repository in your ‘git_intro’ folder i.e convert your normal directory ‘git_intro’ into a Git Repository.
  • A git repository is basically a hidden folder “.git”, which is required to log every commit history and every other information required for your repository, version control, commits, etc. Just think that this hidden folder stores data about your project like its history, past versions, the latest changes that you have made, etc.
  • git init: Transform the current directory into a git repository.
  • git init <repo_name>: Makes a directory with the name repo_name and converts it into a git repository.

See the (master) at the end? This tells you that you are working in a folder tracked by Git and that you are currently in the ‘master’ branch of your project.

2) git status

  • This command returns the current state of the repository. ‘git status’ will return the current working branch. If a file is in the staging area, but not committed, it shows with git status. Or, if there are no changes it’ll return nothing to commit i.e. working directory is clean.

Here, it tells us that:

  • We are on branch ‘master’ (written in blue colour).
  • We have an untracked file (written in red colour).

3) git add

  • This adds untracked files to the staging area for Git. Before a file is available to commit to a repository, the file needs to be added to the Git index (staging area). There are a few different ways to use git add, by adding entire directories, specific files, or all unstaged files.
  • To ‘add’ all the files in a git-tracked folder to the staging area, do the following: git add .
  • If we wanted to add just one certain file to the staging area, we could have done: git add filename.txt

Now, our file is in the staging area. If we hit the command ‘git status’ now, we get the following output:

It tells us that we have a new file in the staging area, and that it is ready to be committed now. It also gives us a command to unstage (remove from staging area) our file which we just staged. Now let us finally commit our file!

4) git commit

  • Git commit command takes a snapshot representing the staged changes, i.e. it commits the staged files in our git repository.
  • After running the git commit command, you need to type in the description of the commit in the text editor, aka. the commit “message.” Or, you can do it in one go by using the message tag (-m).
  • The commit message is supposed to be a short, crisp, and clear description of the work that is being committed. It is also a convention to keep the commit message in the present tense. A meaningful commit message in our case would be:

5) git diff

  • This command shows the file differences which are not yet staged.
  • Let us make some changes in our testfile.txt file which we just committed. Let us write more in that file and see what git diff tells us.

Save the file. Then in the terminal, enter the git diff command:

Voila! Git noticed that we have made a change in our file, and it even shows us which line was modified! That is pretty awesome!

Let us stage our recently modified file by using git add. Now, a git diff command would give no output now, since we have already staged our file, and there are no differences between the staged version and the current version of the file.

Now, to see the differences between a staged file and the previous version of the file (which was committed,) we use the same git diff command, but with a --staged flag at the end.

Using git status now would tell us that the staging version of the file and committed version of the file have some differences.

6) git reset

To unstage an already staged file, we use the command git reset. It would bring the file back to its previous state, which is, with untracked changes.

Run git status and git diff commands. These two commands state that our file was unstaged, and now is modified, and those changes have not been staged yet:

  • git reset helps to undo repository to any particular state present in the history of the repository.
  • Soft Reset: Resetting repository to a given commit and adds all the changes to the staging area. Command: git reset --soft
  • Hard Reset: Resetting repository to a given commit and deleting all the changes. Command: git reset --hard

7) git log

This command is used to list the version history for the current branch. This would tell us about all the commits that have been made in the project.

Let us ‘git add’ and ‘git commit’ our file and then see what git log shows:

Wonderful! git log clearly shows us that we have made two commits in our project, who made them, when they were made and also the commit messages that we added! There it is! Our mini project’s version history!

  • View a complete and descriptive commit history: git log
  • To view commit history in a summarized form: git log --oneline
  • To view commits related to a file: git log --oneline <file_name>
  • git log --since=”1 month ago”

8) git branch

git branch operates branching operations such as list, delete and creates branches in the repository.

  • git branch: This command written in a working repo displays the local branches associated with the repo and the branch in green is the current working branch.
  • git branch <branch_name>: This creates a new branch branch_name locally sourced from previous branch.
  • git checkout <branch_name>: To go to the branch branch_name.
  • git checkout -b <branch_name>: The above two steps can be performed using this single step. b is an alias for branch

Note: All the changes have to be commited before moving to a new branch.

  • git branch -d <branch_name>: it deletes a branch that is no longer needed. Git will warn you if you are deleting an unmerged branch.

Let’s add a file in our newly created branch and commit the changes in it:

9) git merge

Once you’ve made isolated changes in a branch and tested, and are confident to merge the changes in the some branch say master, you can merge. The merge command always merges into the currently checked out branch.

Github Introduction & Features

Until now, we have been working only in the local repository. Each developer will work in their local repository but eventually, they will push the code into a remote repository. Once the code is in the remote repository, other developers can see and modify that code.

GitHub is a code hosting platform for version control and collaboration. It lets you and others work together on projects from anywhere in the world. Git is essentially at the heart of GitHub.

Git is a version control system which tracks the changes when working with computer codes while GitHub is a Web-based Git version control repository hosting service. It provides all of the distributed version control and source code management (SCM) functionalities of Git while topping it with a few of its own features.

Git != GitHub

GitHub adds extra functionalities on top of git, such as

  • Great UI
  • Pull Requests
  • Documentation
  • Bug Tracking
  • Feature Requests and many more…

You do not need GitHub to use git but to use github you can use git.

10) git clone

If you want to get a copy of an existing Git repository — for example, a project you’d like to contribute to, or would like to make your own — the command you need is git clone.

Open a fresh Git Bash terminal from your PC.

  • git clone <url_copied_from_clipboard>

Now you can work in this cloned repository.

Creating a repository on GitHub

Now, enter the name you would like to give to your repository.

Then click on Create Repository button. You will be greeted with this page:

Copy the URL on the top from clipboard, in my case it’s https://github.com/prinsusinghal/dsa.git

11) git remote

  • The git remote command helps you to manage connections to remote repositories.
  • It allows you to show which remotes are currently connected, but also to add new connections or remove existing ones.

How to connect a remote GitHub repository with a local git repository?

To know which remote repositories are connected with your local repository, use the command: git remote -v

Suppose we want to connect our local git repository git_intro to the newly created github repository whose url we just copied, then use: git remote add remotename {url_to_your_remote_repo.git}

Here, prateekremote is just a name we gave to this remote repo. You can also give it any other meaningful name.

  • Generally we use: git remote add origin remote_repository_URL

12) git push

  • The git push command is used to upload local repository content to a remote repository. Pushing is how you transfer commits from your local repository to a remote repo.
  • Remote branches are configured using the git remote command, as discussed previously. Pushing has the potential to overwrite changes, caution should be taken when pushing.
  • To push the files and code in your local git repository to your newly created GitHub repository, use the following command: git push remotename master. Enter your GitHub username and password if prompted.

Great! Now the files and the modifications you made have been uploaded to GitHub! Head over to your GitHub repository page to view those.

  • Generally we use: git push origin <branch_name>

13) git pull

Before you start working on your files for the day, you will want to pull down changes that other people have committed. You don’t have to, but you will be required to before being allowed to push your changes to the remote repo:

Generally we use: git pull origin <branch_name>

Some other git commands:

14) git revert

Git is designed to never loses version history, so to revert a change, git will modify the files in question back to their previous state, and then recommits them as a new commit. The process to do so is fairly simple. Use git log to obtain the id of the commit you want to undo, and then revert it:

git log --oneline
git revert 3e14adc
git push

or

git log --oneline
git revert -n 3e14adc
git commit -m “Commit Message”

15) git checkout/git clean

To change back to last commit (undo untracked changes):

git checkout -- file_name
or
git checkout -- .
or
git clean -f

16) git fetch

Fetches all the changes from the remote repository (here GitHub) from the specified remote branch. In contrast to ‘git pull’ it does not merge changes to the existing local code base.

  • git fetch
  • git fetch origin: Fetches all the branches from remote.
  • git fetch origin <branch_name>: fetches <branch_name> from remote.

GitHub’s functionality doesn’t end here.

It provides following 3 extraordinary features which makes it so powerful:

  1. fork: Or commonly known as forking is copying a repository from one user’s account when you don’t have the write access to it. So you can just copy it and modify it under your own account.
  2. pull (pull request): When you have made the changes in codes that you have copied and want to share them with its original order. Then you can send a notification called a “pull request” to them. A pull request is not to be confused with ‘git pull’.
  3. merge: Now the user who is the owner of those codes, if, finds your changes relevant can merge the changes found in your repo with the original repo, by just button click.

Conclusion

Congratulations, you made it through!

You gained invaluable knowledge about two of the most essential and important tech tools, Git and GitHub, that developers absolutely cannot live their lives without!

--

--

Prateek Agarwal
ArIES IIT Roorkee
0 Followers
Writer for

Currently a sophomore at IIT Roorkee.