Daniel Gafitescu Freelance web developer focused on open source solutions

17Sep/120

Git Cheat Sheet

I have been working with Git for few months and even if at the start was kind of difficult to grasp it after switching from SVN.

Here is a list of most used commands :

Configuring

Cloning the repository
> git clone git://url

>  git config --global user.name “Firstname Lastname”

> git config --global user.email “user@email.com”

> git config --global color.ui true

> git config --global alias.st "status" -  define an alias to git status : > git st

List the configuration

> git config --list - list all the configuration

Setup a project in the root directory

>  git init

Look inside the git file

> tree .git

Add all files to repository

> git add .

Commit files to the repository
> git commit -m “First import”

Commit all changes that have done to the project ( ask for a message)

> git commit -a

Add a single file to the commit (is qued up for commit)
> git add {filename} - add the file
> git rm {filename} - delete the file
> git mv {first_file} {second_file} - rename

See prev change ( DIFF between changes)
git diff {filename} - see the difference between changes

get information about the tracked files , changed or new files  
> git status

See log
> git log
> git log --until=2012-06-14
> git log --since=2012-05-12
> git log --author=”James”
> git log --grep=”Init”
> git log --online

HEAD - last state of the repository , what was last checked out

git show {sha} - show the commit information
git diff {sha} - show a diff from a previous commit
git diff {sha1}..{sha2}  show a diff between 2 commits
git diff {sha1}..{sha2}  {filename} - show a diff between 2 commits on a file

git diff --stat --summary
git log --graph --oneline --decorate --all

Branches

- master is the main branch ( ~ trunk in the svn)

Branch listing

> git branch

Create a new branch

>  git branch human => (create the “human” branch )

To switch to the human branch we need to log out from the master branch

> git checkout human ( swich to the human branch )

If I want to get the master changes in the human branch

> git rebase master

Merge human to master

First you want to see the changes

1) git checkout master

2) git diff master .. new_feature - compare 2 branches

3) git merge new_feature - merge the branch with the name new_feature ( I am in the master branch)

To check if the branch is merged you can do a diff between branches git diff master..new_feature

If we want to go back before the merge

4) git reset --hard ORIG_HEAD

If we have conflicts on merge make changes and then commit

> git branch --merged - show which branches are merged

> git branch -m {old_branch_name} {new_branch_name}- rename the branch
> git branch -d {branch_name} -
delete a branch

> git push origin :{branch_name} - delete a remote branch

> git push origin --delete {branch_name} - also deletes a remote branch

Repositories

When you work on the public server you need to push the commit to live server.

> git push

Donwload the last changes

> git pull

Git Architecture:

2-tree architecture - repository & working copy (svn use)
3-tree architecture - repository , stagging index & working copy (git use)

working copy -> git add file.txt ->  staging index -> git commit file.txt -> repository

Undone changes

- undo changes to the working directory

git checkout {filename}  or
git checkout -- {filename}

- unde changes to the staging directory

git reset HEAD {filename}

- change the commit message or add new change to a file that is already on staging directory
git commit --amend -m “Change the files  already staged”

- changes to the older commits
git checkout {hash} -- {filename}

- revert a commit
git revert {hash}

- rever multiple commits ( move the HEAD pointer)
git reset
-- soft (move the pointer but doesn’t change the staging index or working directory)
-- mixed (default) = move the head pointer to the specified commit and change the staging index but doesn’t change the working directory
-- hard - move the HEAD , change the staging index and working directory

git clean -f   - remove the untracked files

 

Stashing

- store changes temporary , only visible for you
git stash save “config changes” -
save the files to stash
git stash list
- show the stash lists with the name of the changes
git stash show stash@{0}
- show the file changes in specified stash (to see more add -p option)
git stash pop {stash_item}
- get and remove changes from stash , if a stash item is not specified get the last one
git stash apply {stash_item} -
get the changes from the stash

git stash drop stash@{0} - delete a stash item
git stash clear - remove all stash items

Ignoring files

/proect_dir/.gitignore - add all the files and directories that will be ignored by git.

Sample file:
# comment
test.txt - ignore file test.txt
*.tmp   - ignore all the files that have the extension .tmp
*.gz
log/*.log
assets/photoshop/
assets/video
!assets/video/tour_*.mp4

If you want to ignore specific files you need to alter .gitignore_global file located in the user directory

If you want to track an empty directory , create a empty file .gitkeep in that specific directory

17Sep/120

Using the Last.fm API on http://phpmaster.com

I wrote another article for PhpMaster.com about LastFM API .Check it out!

18Aug/120

Lynda.com PHP 5.4 New Features review

Another great title from Lynda.com on the Php server side programming :
PHP 5.4 New Features .

What can you learn from this title :

  • How to install Php 5.4 on Windows , Unix & Mac OS X
  • Built-in web server
  • Improvements on array handling
  • New features like JsonSerializable ,closures , http_response_code
  • Access class members on instantion
  • Traits
3May/120

An Introduction to Redis in PHP using Predis on Phpmaster.com

I wrote an article for PhpMaster.com on using Redis .Check it out!

 

1May/120

Quick comparison between Symfony 1.4 and Zend Framework 1.1

Quick comparison between Symfony 1.4 and Zend Framework based on my personal experience.


MVC structure
Both framework are using the MVC pattern but they have a different structure of the code organization

Symfony : Project > Applications (Frontend/Backend) > Modules > Actions (controllers) > Views

Zend Framework : Application > Modules > Controllers > Views

Command Line
Symfony is using heavily  CLI to generate code and default structure on the other site ZF is using something similar ,  Zend Tool but it's not that powerful as the SF one.

Code organization
Zend Framework is a loosely-coupled architecture allows developers to use components individually like ( Zend_Search_Lucene ) on the other side Symfony is a full stack framework  that standardize the development of a web app.

Database (Model)
Symfony is using Doctrine/Propel ORM  as the Model and ZF is using Data Mapper classes to interact with the database, but as well can be extended to use Doctrine as well.

Testing enviroments

ZF as well as SF is using environments , most common configuration are local , dev , test and production. You can do a lot of changes from this perspective since you can log and debug easily and use different approaches to test the live site and see debug information only from a certain IP address.

The configuration files :
Symfony is using YML standard for configuration files and uses a hierarchy and same configuration can be found in many files, this means that's override.  There is also an inheritance of the environments settings that you can again override whenever you like. Zend framework on the other side uses by default .ini files but you can change that to use xml or php files.

Unit testing

From the unit testing perpective Zend framework is using PHPUnit the  de facto standard on unit testing on Php oppose to Symfony who is version 1.4 is using Lime. You can still use PHPUnit with Symfony 1.4 but you need to install a couple of plug-ins.

Scaffolding

Symfony is using scaffolding & admin generator to do CRUD operation on a database table structure , Zend Framework doesn't have that build in but can be extend to do something similar.

Extending the framework
Symfony can be easily extended using plugin and libraries , even use Zend framework stand alone components , on Zend Framework you can extend current component or you can as well use external libraries.

Community

Both framework have huge communities around them starting with forums, email lists and irc chats channels. Also they have dedicated conferences : For Zend Framework you have ZendCon and Symfony has Live Symfony .

Company behind it

The company the Zend Frameword is Zend who is a PHP company all the way from IDE, code protection & obfuscation with Zend Guard,  Zend Server  , certifications, training  and more recently the cloud computing solutions. All this play nicely with each other but in order to have them you need to pay.

On the other end Symfony is a Sensiolabs release started by Fabien Potencier. Sensiolabs offers trainings, organize conferences and offer support on Symfony projects.

On 28th July 2011 Symfony2 was officially  launch which take the framework to a next level embracing Php 5.3 namespaces , decoupling of modules which some of them are going to power Drupal 8, choose PhpUnit as a standardized testing framework, new templeting system (Twig) , MongoDB support and Doctrine2 for working with databases.

On the other hand Zend Framework 2 is still in beta3, so from my point of view are a step behind Symfony.

Airbus vs Boeing

Don't know why for me it's the first thing I got on my mind when I compare these 2 frameworks. The French have launched A380 in late 2007 and  USA rival Boeing 747-8 was launched in mid 2011 and its the French who win here.

Conclusion

This comparison is a subjective one each framework it's great if the right coder is using it and since everyone is different will be different conclusions.As for me I am going with Symfony ...even with Symfony 1.4

 

 

5Mar/120

MVC Frameworks for Building PHP Web Applications from Lynda.com

A great objective overview of the 4 most used and popular frameworks Zend Framework, Symfony, Codeigniter and CakePhp.

Drew Falkman goes and show each framework features, what patterns it uses and implements a single join email list application which uses all MVC concepts.

For a php developer it's a must watch tutorial so when you start a new project you can see which framework to use.

Go check it out!

22Feb/121

Manage a GitHub repository in Netbeans 7.1

GitHub create repository

A bit of introduction

As with latest Netbeans version 7.1 it comes with git support integrated inside the IDE, along with SVN and Mercurial.

Since GitHub has become such a popular site for keeping open source repositories as well as private projects ( many companies like Facebook , Twitter are using it to keep their private repositories ).

Since my IDE of choice is  Netbeans I couldn't clone a public repository that I have created on GitHub only using Netbeans ( hope there's a solution and maybe it will be added in a future version it will be added to the IDE but as far as I have tried I coudn't do it).

So in order to create the origin and master branch I had to download and use a separate tool which is easy to install and work with and you have the whole process explained  on GitHub's Set Up page .

If you are a Mac User I advise that you use GitHub for Mac which I have used and works great!

Setup the repository on GitHub

If you don't have an account on github go ahead and create one if you allready have click on the "New Repository" button .

GitHub create repository

GitHub create repository

You are going to be redirected to a form where you need to enter a project title and a description.

After you have created the repository you are redirect to a page where you are instructed how to create the general setting of your name and email , initialize the repository and create the origin and master branches.

Taking the next steps in command line

The steps that are listed in the repository detail page need to be done in command line. As mentioned before everything is explained in detail  on GitHub Setup page.

Since I am on windows I have downloaded the latest Git for Windows installed it , generated a ssh key and add it to my account settings. Once you read that you need to enter the commands listed on the repository detail page on the Git Bash.

 As you can see from the image I setup all in d:\www\codeigniter but you can put it anywhere you want because right after last command I am going to delete it completely so I can clone it again from Netbeans.

Now if you go to the repository detail page all you are going to see it's the readme file.

 

Netbeans setup

1) Team > Git > Clone

2) Clone the repository , you enter the repository url along with the user/password

3) Select master branch

4) Save to a destination directory. Please note that I have initialized the project from command line in d:\www\codeigniter . Before doing this next step I went and deleted all the files including the hidden ones.

5) Create project from cloned sources

6) Next the project will be created and I will add the new files to it , in my case I have downloaded Codeingiter 2 and add it to the project and I will commit the files using the commit command from the context menu

7) Next you will need to enter the commit message and select the files that you need to add to the commit.

8) After this commit if you go the github repository page you will see that nothing has changes this is because the git system works. Commit will be done one the local created repository

If you open the output view from the Netbeans windows you will see something like this :

9) Next step will be doing the Remote push to Github , and this is done by selecting from the context menu Git > Remote > Push

10) Next you need to select the remote repository from dropdown :

11) Select the Local Branch

12) Update local references which basically copies the master local branch to origin/master branch remote.

13) After this if you go to github repository page you will see all files listed.

And that's it. Again if someone else know how to do this without downloading the command line tool directly for Netbeans let me know.

It would be nice the guys from Netbeans would add this feature in the near future.