⤆ Go Back

Git It! All you need to know about Git and GitHub

3 min read ☕

🎧   Listen blog while reading   🎧


You must have heard the term git and github, or maybe you've been using it for a while, but it still seems confusing?

Just for the record, Git and Github are two different things. Git is primarily a version control system that can be integrated literally in any software development pipeline. In contrast, Github is an online platform that provides hosting for software that uses Git as version control.

Git can be tricky. There are times when you just try to push your code, but your branches are out of sync, and you end up wasting much time there.

Let's see how to use Git properly to focus your time on writing productive code.

Install & Setup

Install Git using the following command :

1sudo apt install git

If you are on windows, you show install git bash. Click Here to install git bash in windows.

Setup for newly installed Git

1git config --global user.name "User Name"
2git config --global user.email "email"

Basic version control

Go to your project directory and run git init to make this project as a git repository. This will create a .git folder in your project, which will be used to store all the versions.

To start with version controlling for an existing project, you can add those files and do an initial commit.

1git add <file>
2git commit -m 'first commit'

This version is still a local repository; you can go to Github and create a new repository online and link it to your local git repository.

1git remote add origin \
4### An example with a repository of mine
5git remote add origin \

Then you can push the code to online Github repository by just running this command,

1git push origin master
voila! You’ve code is on Github!

Knowing the Git

Checking the status

To determine which file are in which state, we use git status. It allows you to see which files in your project are changed, which files are staged, and which files are committed.

If you create a new file which is not in your project and run this command, see this

1$ git status
2# On branch master
3# Untracked files:
4# (use "git add <file>..." to include in what will be committed)
7nothing added to commit but untracked files present (use "git add" to track)

This command is probably the most used in Git.

Adding file in git

There are few ways to add files in Git. There are cases when you want to add files manually, but most of the time, we tend to add all the changed files. There are more efficient ways to do so instead of adding all the files manually.

1# Adding files one by one
2git add filename
4# Adding all files in the current directory
5git add -A
7# Adding all files changes in the current directory
8git add .
10# Choosing what changes to add (this will get through all your changes and you can 'Y' or 'N' the changes)
11git add -p

Committing your work

Usually, people commit using basic command git commit -m "<message>, but there are some excellent ways to commit your changes.

1# This is typically used for shorter commit messages
2git commit -m 'commit message'
4# Add file and commit in one shot
5git commit filename -m 'commit message'
7# Add file and commit the staged file
8git commit -am 'insert commit message'
10# Changing your most recent commit message
11git commit --amend 'new commit message'
14# Combine a sequence of commits into a single one
15# You might use this to organize a messy commit history
16git rebase -i
18# This will give you an interface on your code editor:
19# Commands:
20# p, pick = use commit
21# r, reword = use commit, but edit the commit message
22# e, edit = use commit but stop for amending
23# s, squash = use to commit, but meld into the previous commit
24# f, fixup = like "squash", but discard this commit's log message
25# x, exec = run command (the rest of the line) using shell

Branch and Merge

The master branch always contains working and stable code. There are some cases where you want to work on new features, or you want to solve some bugs or just want to keep the backup of the current stable version.

Branching helps you to work on a separate copy of your code without harming the master branch. When you first create a branch, it will create an exact copy of the current version; then, you can make changes independently in the new branch. Once you are confident enough and your new feature is ready, you can just merge it in the master branch.

Just remember, it's not a good practice to do the changes directly on the master branch. Always create a branch, make the changes, and merge the code into master.


1# Create a local branch to work on
2git checkout -b branch_name
4# Switching between 2 branches
5git checkout branch_1
6git checkout branch_2
8### Pushing your new local branch to remote as a backup
9git push -u origin branch_2
11# Deleting a local branch - this won't let you delete a branch ### that hasn't been merged yet
12git branch -d branch_2
14# Deleting a local branch - this WILL delete a branch even if it ### hasn't been merged yet!
15git branch -D branch_2
17# Viewing all current branches for the repository, including both ### local and remote branches. Great to see if you already have a ### branch for a particular feature addition, especially on more significant ### projects
18git branch -a
21# Viewing all local branches
22git branch
24# Viewing all remote branches
25git branch -r
27# Rebase master branch into the local branch
28$ Git rebase origin/master
30# Pushing local branch after rebasing master into the local branch
31$ git push origin +branchname


Now, let's see how to merge the branch to the master branch.

1# First make sure you're looking at the master branch
2git checkout master
4# Now merge your branch to master
5git merge branch_2

There are some cases when you can get a CONFLICT error while merging the code. Git will show you the exact file where CONFLICT occurred, and after resolving the CONFLICT, you have to commit the current version.


Things go wrong sometimes, and we should know how to fix it. Git has everything you need to make a mistake with the code you push, overwrite something, or just want to correct something you pushed.

1### Switch to the version of the code of the most recent commit
2git reset HEAD
3git reset HEAD -- filename # for a specific file
5### Switch to the version of the code before the most recent commit
6git reset HEAD^ -- filename
7git reset HEAD^ -- filename # for a specific file
9### Switchback 3 or 5 commits
10git reset HEAD~3 -- filename
11git reset HEAD~3 -- filename # for a specific file
12git reset HEAD~5 -- filename
13git reset HEAD~5 -- filename # for a specific file
15### Switch back to a specific commit
16### Where the '23FE2RE' is the commit ID
17git reset 23FE2RE -- filename
18git reset 23FE2RE -- filename # for a specific file
20### The previous commands were what's known as "soft" resets.
21### Your code is reset, but Git will still keep a copy of the other code
22### handy in case you need it. On the other hand, the --hard flag
23### tells Git to overwrite all changes in the working directory.
25git reset --hard 23FE2RE
26### Think before using this command

Tips for Git

If you are working in the team, and because of some noob developer, your code gets corrupted or you end up having issues. For cases like this, git provide a tool called blame, which will tell you precisely whom to blame for the issue you are facing.

1### Show alteration history of a file with the name of the author
2git blame 'filename'
4### Show alteration history of a file with the name of the author
5### and the git commit ID
6git blame 'filename' -l

Git logging

I use git log most of the time. It will show you all the commits in your branch and will show you the current status of other branches too.

1### Show a list of all commits in a repository. This command shows
2### everything about a commit, such as a commit ID, author, date and
3### commit message.
4git log
6### List of commits showing only commit messages and changes
7git log -p
9### List of commits with the particular string you are looking for
10git log -S 'something'
12### List of commits by author
13git log --author 'Author Name'
15### Show a summary of the list of commits in a repository. This
16### shows a shorter version of the commit ID and the commit message.
17git log --oneline
19### Show a list of commits in a repository since yesterday
20git log --since=yesterday
22### Shows log by author and searching for a specific term inside the
23### commit message
24git log --grep "term" --author "name"