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:

[code lang=”bash” inline=”yes”]git config –global user.name “Your Name”
git config –global user.email “your_email@example.com”[/code]

 

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:

[code lang=”bash” inline=”yes”]git init[/code]

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:

[code lang=”bash” inline=”yes”]git status[/code]

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

[code lang=”bash” inline=”yes”]git add <filename>[/code]

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

[code lang=”bash” inline=”yes”]git add README.MD[/code]

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

[code lang=”bash” inline=”yes”]git commit[/code]

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:

[code lang=”bash” inline=”yes”]git commit -m “My initial commit”[/code]

to commit all changes you can use:

[code lang=”bash” inline=”yes”]git commit -a[/code]

to see commits log use:

to commit all changes you can use:

[code lang=”bash” inline=”yes”]git log[/code]

 

Clone a repository

the following example will cover this topic clearly.

[code lang=”bash” inline=”yes”]git clone https://github.com/WordPress/WordPress[/code]

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:

[code lang=”bash” inline=”yes”]git pull[/code]

 

 Clone local repository

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

[code lang=”bash” inline=”yes”]git clone –no-hardlinks /path/to/repo[/code]

 

Reset Changes from last commit

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

[code lang=”bash” inline=”yes”]git reset –hard[/code]

 

Check Difference from last commit

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

[code lang=”bash” inline=”yes”]git diff[/code]

‘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:

[code lang=”xml” inline=”yes”]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[/code]

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:

[code lang=”bash” inline=”yes”]git push[/code]

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

[code lang=”bash” inline=”yes”]git pull[/code]

 

 

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.

*

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