Beginner Tutorial: Git / GitHub

Introduction

“Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Git is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenientstaging areas, and multiple workflows.”  http://git-scm.com/

If you don’t already know what Git is, take a crash course.  Git (Wikipedia)

 

Installing Git

For installation tutorial of Git on:

  • Ubuntu click here.
  • Centos click here.
  • Windows click here.

 

Configure Git (one time only)

For setting your name with git, so that it can label the commits you make:

git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"

 

Using Git

Start a repository

Start your own local repository, in every folder, is very easy. goto a folder you would like to start repository inside (like inside your project in /var/www/html) and type at bash:

git init

this will create a .git folder inside your folder which saves all the data for your repository. you can delete the .git folder now to delete your local repository and return the folder to what it was before the git init command.

you can use the command:

git status

anytime you want to view your local git status (your need to be inside the project folder, or any subfolder of it, for this command to work)

Beside the social benefits of git, local repository helps you track versions of your files. compare them, and restore older version if needed. when git init command creating the .git folder in the first time, it doesn’t track no file. to add files for track you need to use the

git add <filename>

for example you can create an empty (or not) file called README.MD and type:

git add README.MD

you can use wildcard (such as git add *.conf). this way you can add files to the current commit, and when you ready type:

git commit

you need to provide a commit description. using vim by default (can be changed), and you updated your repository with you first commit.

You can also add the commit description as a parameter:

git commit -m "My initial commit"

to commit all changes you can use:

git commit -a

to see commits log use:

to commit all changes you can use:

git log

 

Clone a repository

the following example will cover this topic clearly.

git clone https://github.com/WordPress/WordPress

this line will create a folder wherever you are called WordPress and clone from wordpress repo all it’s files. you will a .git folder also, which mean you can continue now with your local repository, and even update the source if you have the permission to do so.

to update your local version with the server later:

git pull

 

 Clone local repository

You can easily create a clone of your local repo and git pull changes from it:

git clone --no-hardlinks /path/to/repo

 

Reset Changes from last commit

to reset all the changes you made from the last commit just use:

git reset --hard

 

Check Difference from last commit

to see the changes made in your git from the last commit use:

git diff

‘Q’ for exit. 😉

 

 

Use GitHub

Create repository

Create A Repo – goto New Repository page in github and create your new repo. this tutorial is for public & free repo.

you can set a README.MD file to be clone ready immediatly. the following is for a new repo without README.MD:

after you created a new repository you need to git it to a folder.

New Project

if you want to start your project now, follow inside new directory:

touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/<your-user-name>/<project-name>.git
git push -u origin master

Existing Source

if you already have a project your want to create the repo from just go inside your folder, git init it like above, git add neccesary files (you can use wildcard!) and then resyne the last three line above.

Existing Repo

if you have a local repo which you want to link to your new online repo just use the last two lines of the bash code above.

 

* more info: Create A Repo tutorial on github

 

Push/Pull from/to repository

to push your updates to the server:

git push

to update your local version with the server is the same:

git pull

 

 

Those are the basics of Git. you can do a lot more with version control and collaborate with others.

Happy Git-ing…

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.