Learn Git from Scratch

Learn Git from Scratch

Posted 4 years ago by Ryan Dhungel

Category: Github Git

Viewed 5837 times

Estimated read time: 8 seconds

What is git?

is a best friend of Web Developers because It lets you Version Control. Learn Git From Scratch with these useful examples and basic commands for everyday use. 

What can you do with git?

  • Lets say you are working on a project in your local computer. You want your code to be online so that you can access it from anywhere. Git lets you do that in just few seconds.
  • What if you are working in a project and you reached a point where you wish you could go back in time and start from there. What if you added a function but it did not work so you want to go back into that exact time where you were just about to start that function. Yes with git you can go just there and start writting that function all over again.

Lets keep it simple, I say git is like a Time Machine at least in terms of version control. I wish I could time travel with it!

Install git

Lets get started with the basics of git. The first thing you need to do is install it in your computer. Just go to git website and simply install it.

Now you can go to terminal in mac or command line in windows and start using it. But dont rush yet, lets go to github instead.

Open account in github

It's time to create an account in github if you havent already done so. It is a wonderful place for web developers, trust me on this. Here you can host code repositories for free.

Once you are in, click on the new repository button to get started. just type in the name you want and click create. I am naming it project.

You will reach a new window with the following url https://github.com/you/project . For the sake of clarity I will assume that your name is you and your project name is project
Now, Leave it empty, dont do anything yet. Let git do the work.

Create a project in your local computer

Now its time to create your dream project in you local computer. But lets keep it simple this time alright? And yes you got it, We are not creating anything manually at this point, we will use git instead.

Open up your terminal and type:

git --version

If you have already installed git successfully, you will see the git version which appears something like this

git version 2.5.4 (Apple Git-61)

Basic git command line

There are a lot of command lines to use with git. you can type

git --help

To see a bunch of commands. Lets focus on the most used basic commands for the moment.

Find out where you are


By the way pwd stands for print working directory

Create a project called my-project

mkdir my-project

stands for make directory, it just created a new folder called my-project

Get inside the project

cd my-project

Now you are inside my project

Create a file

touch index.php

has been created

See the list of files


List of basic git command line

Now that you are in the right spot. you can do a lot of cool things. I give you a short list of some of the cool git commands that commonly use.

  • git config – Configure Git
  • git init – Initialize Git repository
  • git status – Check the status of a Git repository
  • git add – Track files
  • git commit – Commit tracked files
  • git push – Upload files
  • git pull – Download files
  • git log - see the recent commits log
  • git reset --soft - Soft reset to the given commit
  • git reset --hard - Hard reset to the given commit

Configure your git account

git config --global user.name "Firstname Lastname"
git config --global user.email [email protected]

Initialize Git repository

git init


You will see the notice saying the repository is initialized. great! 

Hook up with the git repository

It is time to hook up the local project with the git repository that we created earlier

git remote add origin https://github.com/you/project

You will not get a big notification or anything but be sure it has been added

Check the status of your local project

git status

You will see status of your project files weather there are untracked files to be committed etc

Add all the files to the repository

git add .

Now you can see that all the files has been added and ready to commit

Commit local project files to the master branch

git commit -am "my first Commit"

Give your commit a name for example my first commit

See the logs of the recent commit

git log

Now you will see the list of commits, since we did just one commit and named it my first commit. you will see only that one. This is what it looks like:

commit b73302914ea1d51aa6c051ade17aea65ec604608
Author: your name <[email protected]>
Date: Wed Aug 31 22:33:27 2016 +1000


Going back and forth to the particular commits


This is the best part of git. each time you commit you will get the commit id. you can use the commit id to reset your project so that you can go to that particular stage. Now we have only index.php which is empty.

lets imagine you wrote some code and commit again and named it second commit (make sure you git add . first then make commit). Then if you hit git log you will see two commits made to your project.

Now if you want you can just hit

git reset --hard b73302914ea1d51aa6c051ade17aea65ec604608

to bring your project to the earlier stage where it was all empty (first commit) or jump to second commit again. Pretty cool right, This is version control!

Push the local project to the github repository

It is time to push the local project to the github repository that we created earlier

git push origin master

You will have to enter your username and password. It will look like nothing is happening but just hit enter after you enter your password. Now dont be surprised to see your local project live on the github repository. Just refresh the page and smile.

Push your github project to the live server

Now that we have pushed our local project to github, how about pushing it to the live server so the whole world can see our awesome project? cool right?

The first thing you need is ssh access to your hosting account. if you dont have access just ask your hosting company.

ssh [email protected]

Follow up with the username and password (usually your cpanle username and password)

Configure your git account

This is the same procedure that we performed earlier remember?

git config --global user.name "Firstname Lastname"
git config --global user.email [email protected]

See the list of your git settings

git --list

Know your location


Initialize git repository

git init

Add git repository to the orign master branch

git remote add origin https://github.com/you/project.git

Pull from origin master

git pull origin master

Now if you knew what you were doing this far, you must have your project live on the web.

If not dont worry you will get there. The most important thing is you know the basics of git and this is pretty much all you need to know to begin with.

Just make yourself comfortable with git init git add . git commit -am "write here" git log git push origin master etc

Update: I decided to add the following lines here. They are super useful.

Once you run git reset --hard "0123456789" you will still have the old untracked files and folders present in your directory. To remove them run 

git clean -f -d

use cd ~/ to get back the root folder to begin with...

use sudo for super user permission such as sudo cd/secretproject

use git remote rm origin for error that says remote origin already exists


Switch to old git commit


You can list all your old git commits and switch to particular one you like:

< show all old git logs in online format for easy to read >
< I named my old commits one two three four and five >

git log --pretty=oneline

< the result will be similar if you have made 5 commits before >

2fffe7232ece53277754c455b2f386b27612a6f3 (HEAD -> master, origin/master) five
1bea81f5e9483370ef0bc592aa0c28f7574ffdb0 four
bf5c93e6ab9e2d16a08e822638c1f872e710dae9 three
8c3efaefc24bffdd34dae0dcbb6a157c9a9e64f3 two
740180fb07cb863f34f758fb63204744a4c073ed one

< to go to particular commit, use git checkout  >
< below each line will take you to one/two/three/four/five named old commits >

git checkout 740180fb07cb863f34f758fb63204744a4c073ed
git checkout 8c3efaefc24bffdd34dae0dcbb6a157c9a9e64f3
git checkout bf5c93e6ab9e2d16a08e822638c1f872e710dae9
git checkout 1bea81f5e9483370ef0bc592aa0c28f7574ffdb0
git checkout 2fffe7232ece53277754c455b2f386b27612a6f3



In this short article, we learned how to use git from scratch, make commits, resets and even push our project to the github repositoy. You also learned the most popular commands that gets used in everyday basis. Ever since I started using git, I fell in love with it. You can't really work on a large project unless you have some sort of backup. Git is perfect for that.

I hope my article was helpful to all you folks out there. Leave your comments below. Thanks for stopping by.