Installing and using Git and GitHub on Ubuntu: A beginner's guide

GitHub is a treasure trove of some of the world's best projects, built by the contributions of developers all across the globe. This simple, yet extremely powerful platform helps every individual interested in building or developing something big to contribute and get recognized in the open source community.

This tutorial will be a quick setup guide for installing and using GitHub and how to perform its various functions of creating a repository locally, connecting this repo to the remote host that contains your project (where everyone can see), committing the changes and finally pushing all the content in the local system to GitHub.

Please note that this tutorial assumes that you have a basic knowledge of the terms used in Git such as push, pull requests, commit, repository, etc. It also requires you to register to GitHub here and make a note of your GitHub username. So let's begin:

 

1 Installing Git for Linux

Download and install Git for Linux :

sudo apt-get install git

 

2 Configuring GitHub

Once the installation has successfully completed, the next thing to do is to set up the configuration details of the GitHub user. To do this use the following two commands by replacing "user_name" with your GitHub username and replacing "email_id" with your email-id you used to create your GitHub account.

git config --global user.name "user_name"

git config --global user.email "email_id"

The following image shows an example for my configuration with my "user_name" being "akshaypai" and my "email_id" being "[email protected]"

 

3 Creating a local repository

Create a folder in your system. This will serve as a local repository which will later be pushed onto the GitHub website. Use the following command:

git init Mytest

If the repository is created successfully, then you will get the following line:

Initialized empty Git repository in /home/akshay/Mytest/.git/

This line may vary depending on your system.

So here, Mytest is the folder that is created and "init" makes the folder a GitHub repository. Change the directory to this newly created folder:

cd Mytest

 

4 Creating a README file to describe the repository

Now create a README file and enter some text like "this is a git setup on linux". The README file is generally used to describe what the repository contains or what the project is all about. Example:

gedit README

You can use any other text editors. I use gedit. The content of the README file will be:

This is a git repo

 

5 Adding repository files to an index

This is an important step. Here we add all the things that need to be pushed onto the website into an index. These things might be the text files or programs that you might add for the first time into the repository or it could be adding a file that already exists but with some changes (a newer version/updated version).

Here we already have the README file. So, lets create another file which contains a simple C program and call it sample.c. The contents of it will be:

 
#include<stdio.h>
int main()
{
printf("hello world");
return 0;
}

So, now that we have 2 files

README and sample.c

add it to the index by using the following 2 commands:

git add README

git add smaple.c

Note that the "git add" command can be used to add any number of files and folders to the index. Here, when I say index, what I am refering to is a buffer like space that stores the files/folders that have to be added into the Git repository.

6 Committing changes made to the index

Once all the files are added, we can commit it. This means that we have finalized what additions and/or changes have to be made and they are now ready to be uploaded onto our repository. Use the command :

git commit -m "some_message"

"some_message" in the above command can be any simple message like "my first commit" or "edit in readme", etc.

7 Creating a repository on GitHub

Create a repository on GitHub. Notice that the name of the repository should be the same as the repository's on the local system. In this case, it will be "Mytest". To do this login to your account on https://github.com. Then click on the "plus(+)" symbol at the top right corner of the page and select "create new repository". Fill the details as shown in the image below and click on "create repository" button.

Once this is created, we can push the contents of the local repository onto the GitHub repository in your profile. Connect to the repository on GitHub using the command:

Important Note: Make sure you replace 'user_name' and 'Mytest' in the path with your Github username and folder before running the command!

git remote add origin https://github.com/user_name/Mytest.git

 

8 Pushing files in local repository to GitHub repository

The final step is to push the local repository contents into the remote host repository (GitHub), by using the command:

git push origin master

Enter the login credentials [user_name and password] .

The following image shows the procedure from step 5 to step 8

So this adds all the contents of the Mytest folder (my local repository) to GitHub. For subsequent projects or for creating repositories, you can start off with step 3 directly. Finally, if you login to to your GitHub account and click on your Mytest repository, you can see that the 2 files README and sample.c have been uploaded and are visible to all as shown in the follwoing image.

Share this page:

Suggested articles

79 Comment(s)

Add comment

Comments

From: nwnpallewela

This is great... Thanks for your help :)

 

From: Feragon

Thanks a LOT!!! Very usefull tuto, and very simple. Thanks again.

From: Deepak Kanavikar

This was precisely what i was looking for... Thanks

From: ashutoshh

thanks a lot

From: honlulu

<h1> very helpful xD Thanks a lot </h1>

From: karan

awesome , thank you

From: Chris

Thank you!

From: auchomage

Thanks for this, it is very clear and helpful.

From: vartika

thank you very much for such a clear and concise tutorial! :)

From: saurabh

Thanks a lot, really very helpful.

From: Rajani

Very helpful. Thank you very much

From: Kishor

Best..

From: goldie

awesome... spent so much time but did not get... but with this tutorial... its really easy.. thank u

From: jeet

awesome easy to understand.....

From: swayne

Very nice, concise beginners tutorial!  FYI... one little typo found... In section 5: git add smaple.c  (should be "sample.c")

From: maitreyee

Extremely helpful. I followed it step by step and I got exactly what I wanted

 

From: Jerry

Bravo!!!

From: neil

Awesome 

From: Bilal

Thank you so much

From: Bilal

one more thing friends, suppose someone get any issue in  git push origin master

please use this git push origin master --force its work for me

From: jorge8979787

thanks!

From: Luna Das

nice article that what I was looking for :)

From: Murat Ersin

It's work so great.Thanks for this tutorial.

From: om

This is very cool tuto, love it.

From: Chichio

Thanks. But i must run command: git pull origin master before git push origin masterI'm newbie

From: Chatchai Saratakij

Wow, Thank you^_^

From: luan

how to integrate postgresql to github? 

From: Shradha

Thank you so much..This is the most precisely explained tutorial...:)

From: Kautsya Kanu

Best Tutorial that I found.. Thanks a lot!! :) You are really great..

From: yohannes

Thanks that will help for starters like me.

 

From: Jacob

 This was great, but I initially received a error when I tried the git push origin master. 

The error looks something like this: fatal: unable to access 'https://github.gatech.edu/jc89x0/SevFiewk.git/': server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none

I believe it is a certificate trust issue, but the fix for me was to enter the following commands (ref: http://stackoverflow.com/questions/21181231/server-certificate-verification-failed-cafile-etc-ssl-certs-ca-certificates-c)

export GIT_SSL_NO_VERIFY=1 #or git config --global http.sslverify falseAfterwards, I am presented with an option to provide my username and password.   

From: Shashikant Singh

Very good tutorial . Very clean explaination. 

From: Devashish Kumar Jaiswal

Thanks a lot..This is very helpful for everyone

From: Tingu

Nice One Article

 

From: Tingu

 Masaalla article

From: Zakki

Excellent tuto!

From: Graham Newman

Brilliant - many thanks!

From: Ravi Kumar

That's great...Thankyou sir

From: Ishadi

Thanks a lot!

From: subhajyoti

too good

From: Nisal

This is great... Thanks for your help :)

From: Kevin

That's amazing. The best hands on beginner's guide to git. It isn't much of a big deal. Thanks

From: Nikhil Chavda

this tutorial is very usefull for me thank you so much.

From: ahahah

This was soooo helpfull. Thank you soooo much. :))

From: Victor

Thanks for this , I never thought it was so simple, you helpme so much

From: WRONG!

remote add orgin, not add remote origin

From: Irishologram

I just followed this tutorial and create a new github repository, thank you!

From: KC

Thank You! This has been of real help.

From: murali

Thanks for the usefull information....

From: Shrikrishna

Thanks mate exellent article.

From: Eslam Ezzatneshan

Hi,

Thanks for this helpful article.

I am trying to install the following open-source package on my virtual-box Ubuntu:https://github.com/Par4All/par4allTo be honest, I am almost new in Ubuntu and don't know so much about git etc.Could you please help me what should I do step-by-step to install that package? Really appreciate your time. Cheers

Eslam

From: Shivam

This is simply helpful.. Can you also cover for rest ??

From: nullbyte

Thanks a lot.

From: annie

Too good!

From: Sagar PAtil

Great man cool article. Very easy and clear steps provided.

From: Zachary

Basic but very important! Great!

From: Alok Patra

Great detailed explanation. Helped a lot. Thanks

From: martineskobg

Cool Thanks !! :)

From: p81061473525

This is great...

From: Jona

SO EASY!!!!!!

From: Antz411

Excellent work!

From: Umer

Many thanks for this wonderful how-to...cheers :)

From: Manjesh

Dude, Your Tutorials are Awesome .

From: Ajay

Thanks  

Steps is fine but if some one want to add all directory  can use

git add .

command 

before doing this change mode of file if required

 

Thanks

From: Wayne B

Hello, very easy and simple instructions for us noobs. Thank You very much for this simple and easy to understand tutorial!

From: vishnu

Thanks very useful 

From: anvesh

Thanks bro

From: MX1

Verry cool. Thanks for the awesome tutorial. Helps me get a jump start on installing the new Cuda tool kit on all my machines. Thanks again awesome!

From: prasad

very good.

From: pk

This is great help! thanks

From: John

Great teaching.

From: dhanyesh

Its very useful and effective. Easy to use it

From: rano

Unable to push. NO enter login credentials prompted after running git push origin master.

fatal: unable to access 'https://github.com/myusername/myproject.git/': Failed to connect to 127.0.0.1 port 8888: Connection refused

From: bluescreen777

Thank you for the help!

From: Sravan Chithari

Best article and very helpful :)

From: Mike Paget

Good job! i like it

From: mikesmith123456

thank you, thank you thank you.

From: Tasos

Dude, you are the only one who managed to put an actual useful guide for first time users. Thanks a lot!

From: Brandon

Perfect tutorial, thank you so much!