Sitemap

Introduction To Git & GitHub

8 min readJun 4, 2022

--

To understand Git we need to understand what is Version Control System?

Version Control System (VCS) is software that will have records of all the changes and versions of your software. It is like taking a screenshot of every single work you are doing so in case even if you mess up at some point you can go back and use the screenshot to start again from that point.

Types of Version Control System:

1. Centralized- There is a central repository to which all the users are connected and make all the changes to that centralized repository.
Example: Subversion & Perforce.

2. Distributed — There is no central repository. All the users can have a copy of the project on their local machine and work on it and make changes locally.
Example: Git & Mercurial

So, What is Git?

Git is distributed version control system where we can keep track of all the changes made in the form of snaps. Besides this, we can also use Git to work with different people on the same project even at a remote location.

Example: Google Docs is the practical implementation of Git where we can see all the changes made to the document.

Features of Git:

1. Track all the changes made to the project.
2. Contains a message to explain the changes
3. Include tags to distinguish between different versions
4. Move between any of the past version
5. Provides Total Security and Total Freedom

Reason to learn Git:

1. Git Tracks Changes
2. Must have skills for programmers
3. Git makes collaboration easier
4. Undo a mistake
5. Git aces open source experience

How to install Git

•For Windows:
You can visit the https://gitforwindows.org/. Download the git installer and then install it on windows.

•For macOS:
You run the following command:
$ brew install git

  • For Linux:
    Run the following command:
    $ sudo apt install git-all

Basic Terms about Git

Repository (repo): The top-level directory of a working tree where Git stores all project history and information. Also known as repos.

Commit: Whenever we make changes and we want to take a snapshot of those changes then we do it with the help of the commit command.

Branch: Branch is a kind of different line of development. Suppose you are working on the CSS features of the website and suddenly you need to work on the backend of the website but you don’t want to alter the current work, so you create a different branch and do all the backend work there and later on can merge both the branches.

Configuring the Git

To configure Git, we must define some global variables: user.name and user.email. This is necessary because with every commit the details of the author will be attached. So that in the future we may know who made these changes.

$ git config — global user.name “<USER_NAME>”

$ git config — global user.email “<USER_EMAIL>”

We can check the configuration using the command, git config — list

What is GitHub?

So far, we understand that with Git we can make changes to a project on our local machine But to inform others about the changes of the project there must be a central place where we can push all the changes so that others can see them. That’s where GitHub Comes in.

GitHub is basically a cloud platform where we can push our changes with the help of Git. GitHub offers a website, additional command-line tools, and an overall flow for developers and users to collaborate on projects.

Features of GitHub

1. Upload the project
2. Copy a project locally using the Fork command
3. Create an Issue on a project
4. Give a Solution to an issue in the form of a “Pull Request”
5. Use GitHub Actions for Various task

Git vs Github

Press enter or click to view image in full size
Git vs GitHub

Basic Git Commands Every Developer Should Know!

1️. Git Init

A git repository must be created before any changes can be made to it or commits can be made.
To create a new repository with Git, we’ll use the following command.

$ git init

This command will initialize the git repository with the default master branch. Here “init” refers to “initialize”. Due to naming convention main is now the master branch.

Press enter or click to view image in full size

2. Git Clone

We can easily clone any repository from a remote location like GitHub to our local machine using the following command:

$ git clone <path-to-repository-to-clone>

These commands operate on “The Three Trees of Git”. These trees are the Commit History (HEAD), the Staging Index, and the Working Directory. We will discuss these trees very soon!😃

3️. Git Status

This command will show all the changes that are made in the repository and from there we can commit these changes. i.e, take a snapshot of those changes.

$ git status

It shows us which changes are staged and which are not. Also, tell which files aren’t being tracked by Git.

Press enter or click to view image in full size

4️. Git Add

The git add command is used to move files from the Working Directory to the Staging Index

$ git add <File-Name>

For Git to track a file, it needs to be committed to the repository and for a file to be committed, it needs to be in the Staging Index.

Here working directory is the directory where we are currently working on and making changes while on the other hand staging index is the place where all the changes are listed and are ready to commit.

Press enter or click to view image in full size

5️. Git Commit

After adding the modified file to the staging area we will make a commit(snapshot) of the file.

$ git commit -m “<Message For the Commit>”
$ git commit -m “Added an Index.html file”

Get Abdul Raheem’s stories in your inbox

Join Medium for free to get updates from this writer.

In the above command, ‘m’ will assign a message to the commit(snapshot) so that in the future we want to know why this commit(snapshot) was made. We can know that by reading the message that we define while taking a commit(snapshot).

Output:

Press enter or click to view image in full size

Understanding Commit in Detail

Whenever we make changes in any file and we want to take a snapshot of those changes. Git can not take a snapshot of all the changes on its own. We need to specify which changes we want to save. Hence we take a snapshot of only those changes.

6️. Git Log

Git log command is used to know what changes are made and when these changes are made. It will give a brief detail of all the commits that have been made so far.

$ git log

Feature of Git Log:
✧Unique SHA value for each commit
✧ Author detail
✧ Date it is created
✧ A message that describes the detail of this commit.

Output:

Press enter or click to view image in full size

7️. Git log –oneline

The git log command has a flag that can be used to alter how it displays the repository’s information. One of the flags is — oneline.
— oneline flag will display all the information of the commit in a single line.

$ git log — oneline

Feature of Git log — oneline:
✧ Show the commit in oneline
✧ Give us the message of a commit
✧ Give the first 7 characters of the commit’s SHA

Output:

Press enter or click to view image in full size

8️. Git log — stat

Suppose you made changes to the index.html file and then commit those changes and now you want to know modified files in detail like how many are added and how many are deleted in the commit.
We can find out using the — stat flag.

$ git log — stat

Features of — stat flag in Git Log:
✧ Displays the file(s) that have been modified
✧ Displays the number of lines that have been added/removed
✧ Displays a summary line with the total number of modified files and lines that have been added/removed

Output:

Press enter or click to view image in full size

9️. Git log -p

Let’s assume I want to know how many changes are made during the commit and what exactly are those changes that I can find out using this command.

$ git log -p

The above command display all the lines that were added in the particular commit. ( + ) will show all the lines that are added and similarly ( — ) will show all the lines that are removed from the file during that particular commit.

Features of git log -p:
✧ Displays the files that have been modified
✧ Displays the location of the lines that have been added/removed
✧ Displays the actual changes that have been made

Output:

Press enter or click to view image in full size

How to search for a specific commit?

Supposed we have the SHA value of the commit and we want to see that particular commit only and don’t want to scroll all the other commits. We can do this using two methods:

1. Git log <SHA of commit>
2. Git show <SHA of commit>

We can also search for a commit using the commit message.

$ git log -p — grep=’Message’
$ git show — grep=’Message’

10. Git Reset

Git reset is a powerful tool used to undo local changes to the state of a Git repository. For example, you accidentally commit something or made changes that you don’t want to, So you can reset such changes with this command.

$ git reset

Types:

1. git reset — soft: keep your files, and stage all changes back automatically.

2. git reset — hard: It will completely destroy any changes and remove them from the local directory.

3. git reset — mixed: It keeps all files the same but unstaged the changes. This is the default approach.

11. Git rm

This command is used to remove files from the repository because if we try to delete it without git rm then git will store its history in its log and it will show there when we check the status of our repository.

$ git rm

Output:

Press enter or click to view image in full size

12. Git Branch

This command is used to create a new branch.

$ git branch <branch-name>

13. Git Checkout

Suppose we have many branches in our repository then we use the checkout command to move in between these branches.

But we need to commit all the changes in the current branch before checking out to another branch.

$ git checkout -b main

14. Git Merge

This will merge the two branches into one.

git merge <name-of-branch-you-want-to-merge-in>

--

--

Abdul Raheem
Abdul Raheem

Written by Abdul Raheem

Machine Learning Guy Learning 0's and 1's