Thursday, December 19, 2013

Git most used features

Cache credentials if you need to enter credentials only once and they are cached for 900 seconds
git config credential.helper 'cache --timeout=3000'
Create branch from an existing branch and switch to it
git checkout -b new_branch develop
Create branch from commit and switch to it
git checkout -b new_branch d9410d98f69d08
Diff between local and remote branch
git fetch
git difftool origin/develop develop
# to refresh develop branch
git pull
Delete branches that don't exist on remote repo - sync remote and local repo
# git remote prune origin
* [pruned] origin/branch_name
# git branch -d branch_name # manually remove orphan
comparing files in two revisions
# this will launch diff tool for one file in two revisions
$ git difftool 344c f8cc proba.txt

# this will launch diff tool for one file in commit and working directory
$ git difftool  a9a5f2a ./component.ts

# Usage of difftool for comparing all files in two revisions
git difftool 8c6a dd6b
Compare current commit with previous one
git difftool HEAD..HEAD~1
Put in zip archive files from single commit
git archive --format=zip 1615b > myfiles.zip
My favorite format for log
git log --pretty=format:"%C(#cd9a00)%h %C(#0080ff) <%an> %C(#17b062)(%cr) %d %C(#c0d6de)%s"
remove ajax.php from version control but not from file system
git rm --cached ajax.php 
git commit -m 'removed ajax.php from git'
put file from past commit to index and view diff.
git checkout 42cf1ad -- package.json
git difftool --cached
Search every branch in Git Repository and find commit containing messageb or regex search fin.*me in specified time range and print hash, line number and lines around matching line.
# in PowerShell and Linux
git grep -in -C 1 messageb $(git rev-list --all)
git grep -in -C 1  fin.*me $(git rev-list --all --since="2021-05-10" --before="2021-05-26")
another useful when looking for source code in all branches
# search in all files in all branches
# only Linux
git rev-list --all | (
    while read revision; do
        git grep -F 'yourWord' $revision
    done
)
to find branch with SHA
$ git branch -a --contains 08d70aa131ded0ca84f9d31b7
newmain
search all commit messages in all branches
git log --all --oneline | grep "yourWord"
How single file changed in past by time and between commits
git log --follow -p  --since="2 hours ago"  -- README.md
git log --follow -p  HEAD~3..HEAD -- README.md
List commits for my_branch since it's creation
git log --pretty=format:"%C(#cd9a00)%h %C(#0080ff) <%an> %C(#17b062)(%cr) %d %C(#c0d6de)%s" master..my_branch
Solve conflict, merge and refresh feature branch from master
git checkout -b my-branch # create new branch from master
# modify line, commit & push
# in GitLab modify same line add commit on master 
# create merge request, try to merge it - it fails
git checkout master
git pull # refresh local copy
git checkout my-branch
git merge master
# merge, commit & push
# merge my-branch into master in GitLab using merge request
Install diff and merge tool for Windows: 1.Install KDiff3 2.Place this in .gitconfig file
[difftool "kdiff3"]
    path = "c:/Program Files/KDiff3/kdiff3.exe"
    trustExitCode = false
[difftool]
    prompt = false
[diff]
    tool = kdiff3
[mergetool "kdiff3"]
    path = "c:/Program Files/KDiff3/kdiff3.exe"
    trustExitCode = false
[mergetool]
    keepBackup = false
[merge]
    tool = kdiff3

Saturday, December 7, 2013

jQuery animate and queue

Simple HTML with two div elements that shows jQuery's animate and queue effects.

Yellow paragraph simultaneously changes height and width.
Grey paragraph first changes height and then width.

Monday, November 11, 2013

Nesting with columns named id and parent_id

Create table mytable :

CREATE TABLE IF NOT EXISTS `mytable` (
  `id` int(11) NOT NULL,
  `parent_id` int(11) NOT NULL,
  `name` varchar(256) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `mytable`
--

INSERT INTO `mytable` (`id`, `parent_id`, `name`) VALUES
(1, 0, 'item 1'),
(2, 1, 'item 2'),
(3, 2, 'item 3');

And then run following script

Sunday, August 18, 2013

Connecting Two Derived Table

You want to join results from two queries - the derived tables are solution for you.
Look at code below.Yellow highlighted lines are queries that you want to join and green highlighted

Thursday, July 18, 2013

Execute Linux Commands from Browser

If you need to execute a shell command using browser here's how to do it. To understand this tutorial you need to have advanced level of understanding of Linux and PHP.

Wednesday, July 10, 2013

PHP debugging with JavaScript Console, Error and Access Log

You can use JavaScript console to output global and other variables from your PHP file. That way you can use JS console to track what is is going on in PHP backed files that are called by $.ajax. Also you should be familiar with PHP's functions utf8_encode and utf8_decode

Raw outuput of JSON string and Base64 encoded JSON string

Sunday, July 7, 2013

JSONP Tutorial

This is an advanced tutorial with working example you must know jQuery and JSONP foundations. JSONP bypass browser's "same origin" policy.

Tuesday, July 2, 2013

Parse JSON generated by PHP

This tutorial shows how to parse a JSON string generated by PHP. Place job.php and index.html in same folder.

Wednesday, June 26, 2013

Top 3 Apache Rewrite Tips

Maintenance

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/maintenance.html
RewriteRule (.*) /maintenance.html [R]

Serve page without 302 redirect

RewriteEngine on
RewriteBase /
RewriteRule ^oldpage\.html$ newpage.html [L] 

Serve page with 302 redirect

RewriteEngine on
RewriteBase /
RewriteRule ^oldpage\.html$ newpage.html [R,L]

Friday, June 21, 2013

Serve Pages from Subfolder on Apache

Multiple site hosting on one hosting account in subfolders

Directory structure for domains example.com and otherexample.com

[Folder] public_html
[File  ]    .htaccess
[Folder]    example.com
[File  ]         index.php
[Folder]    otherexample.com
[File  ]         index.php

Content of .htaccess

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} example.com
RewriteCond %{REQUEST_URI} !example.com/
RewriteRule ^(.*)$ example.com/$1 [L]


RewriteCond %{HTTP_HOST} otherexample.com
RewriteCond %{REQUEST_URI} !otherexample.com/
RewriteRule ^(.*)$ otherexample.com/$1 [L]

index.php file could be a WordPress index.php that resides in web root folder.

Thursday, June 6, 2013

Basic APC Tutorial

APC is very powerful caching system for PHP. Here are some basics. First run test_apc.php and if you in 5 seconds click on delete link the key "str" will be deleted from cache. If you click on delete link after 5 second the key "str" will expire from APC caching system.

Saturday, June 1, 2013

Serve Multiple Sites from Singe IP Address on Apache

If you want to run multiple sites on one Apache web server say several PHP frameworks - this is how you need to set up the XAMPP server. For WAMP server you need to keep in mind that there are two httpd.conf files and two httpd-vhosts.conf files so you need to modify the right ones.
Don't forget to include DirectoryIndex index.php line in your virtual hosts 'cause some frameworks will not work without it.

Most Frequent WordPrees actions and shortcode

To follow this tutorial you need to know WordPress basics about plugins and shortcode.
Just replace the Hello Dolly plugin with this code:

Friday, May 31, 2013

Friday, April 19, 2013

Selecting HTML elements with similar ID

You want to select only certain HTML elements that have id starting with some string? This is the right tutorial for you.
Look at first highlighted portion of code - it's jQuery selector that selects all elements starting with 'q'.
That means that only they will be transformed.

Tuesday, April 9, 2013

Delete row with jQuery and PHP

Do you want to delete a row in HTML and MySQL table with fancy fade out effect? This is the right tutorial for you assuming that you are familiar with basics of jQuery and PHP.
You make an asynchronous call to PHP script called delete.php so end user doesn't leave page when a row is deleted, instead he gets an alert box that confirms that row is deleted and the row gradually disappears.

jQuery shopping cart

Your manager told you to put shopping cart into an existing site? And told you to use jQuery and AJAX in order to prevent page reload as customer click on products in catalog? And add a couple of effects?

Create Web Widget in iFrame

Do you want to create widgets that can deliver different content based on visitor's browser and location? It is not that difficult. In this tutorial I'll assume that you have a basic knowledge of JSON, XML and jQuery.
This tutorial assumes that you have cURL and simpleXML functions enabled on your web server.

Saturday, April 6, 2013

jQuery's on function tutorial

This tutorial show how to work with table element. It uses jQuery function on() to highlight both tables. The one that will be inserted in page DOM after AJAX call to file ajax.php and the other table that is part of initial DOM.

Parse XML using jQuery

In this tutorial I'll show you how to parse XML file using jQuery. PHP will generate a XML file and jQuery will parse it and create HTML code for web page.

jQuery and PHP generated form

Say you want to enable disable text fields in a form that is generated by PHP script. The script pulls data from a MySQL table, and you want to reduce source code you maintain to minimum. You want JavaScript to work regardless of number of fields in table only condition is that billing fields are enabled when shipping data is different from billing data. And vice versa.

Prepare site for upload to production server

When you prepare your web site for production you need to enable compression in order to save bandwidth and get higher rank in Google SERP. You can check the results of your compression using free tools like Google Web Master Tools

Search and Highlight a Portion of Article

If you want to show only a few sentences in you search results page and you want to be different sentences you will find this tutorial very useful.

Sunday, March 31, 2013

Cities in Rectangle using PEAR

If you want to show cities in certain rectangle this is the right tutorial for you. However you need to install PEAR and GeoNames package first since this script relies on GeoNames API.
You set coordinates for rectangle, language and number of cities in the array.

Twitter API 1.1 Tutorial

Twitter has launched a new API version 1.1 so you need to update your PHP scripts. If you use Abraham Williams - @abraham twitteroauth.php script you need to update it like this: find row public $host = "https://api.twitter.com/1/"; and replace with public $host = "https://api.twitter.com/1.1/";. Download the script at http://github.com/abraham/twitteroauth.

If you don't update your code like this some functions will not work, like Twitter search. You need to register your application at http://dev.twitter.com/apps/new. Once you register your application get following keys:Consumer key, Consumer secret, Access token secret, Access token and put them in your code. You must make sure that your host supports cURL.

OK here is the directory structure:
index.php
twitteroauth
- OAuth.php
- twitteroauth.php
and here is index.php:
<?php
$consumer_key = "";
$consumer_secret = "";
$access_key = "";
$access_secret = "";

require_once('twitteroauth/twitteroauth.php');

// create an object from TwitterOAuth
$connection = new TwitterOAuth ($consumer_key ,$consumer_secret , $access_key , $access_secret );

$content = $connection->get("statuses/user_timeline",
                             array('screen_name'=>'ibm',
                                   'count'=>'20') );

foreach( $content as $item )
{
  echo $item->text."<br/><br/>\n";
}
?>
Yellow highlighted is the method name and green highlighted text are parameters
for the API call. This
script shows last 20 tweets from IBM. Full info is at statuses/user_timeline
API page.
<?php
$consumer_key = "";
$consumer_secret = "";
$access_key = "";
$access_secret = "";

require_once('twitteroauth/twitteroauth.php');

// create an object from TwitterOAuth
$connection = new TwitterOAuth ($consumer_key ,$consumer_secret , $access_key , $access_secret );

$content = $connection->get("search/tweets",
                             array('q'=>'bar',
                                   'geocode'=>'37.781157,-122.398720,100km',
                                   'count'=>'5',
                                   'lang'=>'en'));

echo"<pre style='border:dotted;padding:5px;background:lightgrey'>";
var_dump($content->statuses[0]->text);
var_dump($content->statuses[0]->user->id);
var_dump($content->statuses[0]->user->screen_name);
echo"</pre>";

echo"<pre style='border:dotted;padding:5px;background:lightgrey'>";
print_r($content);
echo"</pre>";
?>
This code shows first tweet, user_id and screen name. print_r($content) shows first five results for keyword 'bar' 100 km around San Francisco (37.781157,-122.398720) in English. Complete reference is at search/tweets API page.