Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

An Ultimate Guide to Git and Github

  • Difficulty Level : Easy
  • Last Updated : 02 Mar, 2021

Highlights of the article:

  1. Introduction to Git
  2. Git Repository Structure
  3. Accessing github central repository via Https or ssh
  4. Working with git – Important Git commands

Introduction to Git

For installation purposes on ubuntu, you can refer this article :

Git is a distributed version control system. So, What is a Version Control System?
A version Control system is a system that maintains different versions of your project when we work in a team or as an individual. (system managing changes to files) As the project progresses, new features get added to it. So,
a version control system maintains all your different versions of your project for you and you can rollback to any version you want without causing any trouble to you for maintaining different versions by giving names to it like MyProject, MyProjectWithFeature1, etc.

Distributed Version control system means every collaborator(any developer working on a team project)has a local repository of the project in his/her local machine unlike central where team members should have an internet connection to every time update their work to the main central repository.

So, by distributed we mean: the project is distributed.
A repository is an area that keeps all your project files, images, etc.

In terms of Github: different versions of projects correspond to commits.
For more details on introduction to Github you can refer :

Git Repository Structure

It consists of 4 parts:

  1. Working directory : This is your local directory where you make the project (write code) and make changes to it.
  2. Staging Area (or index) : this is an area where you first need to put your project before committing. This is used for code review by other team members.
  3. Local Repository : this is your local repository where you commit changes to the
    project before pushing them to central repository on Github. This is what is provided by distributed version control system. This corresponds to the .git folder in our directory.
  4. Central Repository : This is the main project on the central server, a copy of which
    is with every team member as local repository.

All the repository structure is internal to Git and is transparent to the developer.

Some commands which relate to repository structure:

git add 
// transfers your project from working directory
// to staging area.

git commit 
// transfers your project from staging area to 
// Local Repository.

git push
// transfers project from local to central repository.
// (requires internet)

Accessing github central repository via Https or ssh

Here, transfer project means transfer changes as git is very lightweight and works on changes in a project. It internally does the transfer by using Lossless Compression Techniques and transferring compressed files. Https is the default way to access Github central repository.

by git remote add origin http_url :
remote means the remote central repository.
origin corresponds to your central repository
which you need to define (hereby giving https URL) 
in order to push changes to Github.

Via SSH: connect to Linux or other servers remotely.

If you access Github by ssh you don’t need to type your username and password every time you push changes to GitHub.

Terminal commands :

ssh-keygen -t rsa -b 4096 -C ""
This does the ssh key generation using RSA cryptographic algorithm.

eval "$(ssh-agent -s)" -> enable information about local login session.

ssh-add ~/.ssh/id_rsa -> add to ssh key.
cat ~/.ssh/id_rsa (use .pub file if not able to connect)
add this ssh key to github.
Now, go to github settings -> new ssh key -> create key ssh -T -> activate ssh key (test connection) Refresh your github Page.

Working with git – Important Git commands

  • Git user configuration (First Step)
    git --version (to check git version)
    git config --global "your name here"
    git config --global "your email here"

    These are the information attached to commits.

  • Initialize directory :
    git init 
    initializes your directory to work with git and
    makes a local repository. .git folder is made


    git clone http_url 
    This is done if we have an existing git repository.
  • Connecting to repository :
    git remote add origin http_url/ssh_url 
    connect to central repo to push/pull

    pull means transferring the changes on the central repository to your local repository. push is the vice versa of pull.

    git pull origin master

    Always first pull contents from the central repo before pushing so that you are updated with other team members’ work.
    Here, master means the master branch (in Git).

  • Steps to add a file to central Repository:
    First, your file is in your working directory, Move it to the staging area by typing:
    git add -A (for all files and folders)
    #To add all files only in the current directory
    git add .

    git status: here, untracked files mean files that you haven’t added to the staging area. Changes are not staged for commit means you have staged the file earlier then you have made changes in that files in your working directory and the changes need to be staged once more.
    Changes ready to be committed: these are files that have been committed and ready to be pushed to the central repository.

    git commit -a -m "message for commit"
    -a: commit all files and for files that have been 
         staged earlier need not to be git add once more
    -a option does that automatically.
    git push origin master -> pushes your files to 
                             github master branch
    git push origin anyOtherBranch -> pushes any 
                          other branch to github.
    git log ; to see all your commits
    git checkout commitObject(first 8 bits) file.txt-> 
    revert back to this previous commit  for file file.txt

    commitObject can be seen via git log.

    HEAD -> pointer to our latest commit.

  • Ignoring files while committing.
    In many cases, the project creates a lot of log and other irrelevant files which are to be ignored.So to ignore those files, we have to put their names in“.gitignore” file.
    touch .gitignore
    echo "filename.ext" >>.gitignore
    #to ignore all files with .log extension
    echo "*.log" > .gitignore

    Now the filenames written in the .gitignore file would be ignored while pushing a new commit.

  • To get the changes between commits, commit and working tree.
    git diff

    ‘git diff’ command compares staging area with the working directory and tells us the changes made. It compares the earlier information as well as the current modified information.

Branching in Git

create branch ->
git branch myBranch
git checkout -b myBranch -> make and switch to the 
                                  branch myBranch

Do the work in your branch.


git checkout master ; to switch back to master branch

merge contents with your myBranch By:

git merge myBranch (writing in master branch)

This merger makes a new commit.

Another way:

git rebase myBranch

This merges the branch with master in a serial fashion.


git push origin master

Contributing to open source by fork a project and do some work (add new features) in your branch and then do a pull request on Github.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!