Monday 6 May 2013

Staging and Unstaging area in GIT

In this article, i am going to explain what is mean by staging area and unstaging area in git.

Staging area means, whatever files you have changed in your working tree and added into index using git add (command) . Thats called as "staged" area(but not yet committed). Simply we can tell "changes that are staged but not committed".

If you want to see the list of files you have added into the index, use the following command
git diff -- staged

Unstaging means, changes that not yet added into the index. simply we can tell as "changes that have not been added into the index". To view the unstaged files do the following commands

git diff (Will give you the overall files whatever you changed/modified as well what changes you have done in each and every files in your working tree).

Sunday 7 April 2013

How to create a patch in git

In this article, i am going to explain how to create a patch in Git Version control. Before look into that how to do that, we need to ask a question our self why we have to go with "patch" instead of merge option is available in git.

What a need of patch?

If you have master branch with bug free(just example), in later if you want to work with any enhancement/future kind of stuff you can create a new branch called "future". So once all enhancements are done you can merge "future" branch into "master" branch. For example in your future branch shared with multiple developers so obviously your "future" branch will have good amount of commits. So while you merge with "master" later, you can do it in two ways one is "merge" and another one is "patch"(create a patch and apply that patch into master branch). If you go with "merge" option, reverting back to previous version is little bit headache( because git will creates so many merge commits). suppose if you choose patch style, it will be single commit for your entire "future" branch changes. Lets see how to do it in git as command.

just assume you are in future branch and you have done all necessary changes on that branch, just we want to create a patch

git checkout future (if you want create a patch from "future" branch)
git diff master >> my_patch_name.patch

Now go to your master branch and then apply created patch from future branch.

git checkout master
git apply my_patch_name.patch
git add .
git commit -m"future branch merged with master"
git push origin master

It will create a single commit in master after you applied patch, just delete that patch file which the file has extention .patch. Good thing is if you want to revert back to your previous commit too easy.

Hopefully you can enjoy this article!!!.

How to checkout other branch exact files in git

Here i am going to explain you, how to checkout other branch particular file in your working tree or current branch. Just assume your current working directory is "future"(git branch name is future).
You have one more branch called "master". Suppose if you want to checkout particular file from master branch into your future branch. Then do the following command it will work out well.

Lets move to your current branch, here "future" using following command

git checkout future

Now you shifted into "future" branch. Then do the following command, which we need exactly for our need(checkout particular file from master branch).

git checkout master -- file_1_name.txt,file_2_name.txt
(Just for examples file_1_name.txt and file_2_name.txt. According to your need you can change into your desired filenames list).

The above command will override your current branch(future branch) files you mentioned above command. So your local changes will be ignored.

Hopefully now you come to know how to checkout other branch files in your current branch!!. 

Monday 18 March 2013

Most useful linux crontab with examples

Linux crontab is very useful utility program, if you plan to do schedule a routine in background with specific hour/time or day basis. Mainly this program will used by linux sysadmins because they knows how this utility will helps in Maintenance some background scheduler routines in their daily activities. Let's see that in detail in this article.

Linux crontab syntax/format as follows

00-59 00-23    00-31                 01-12                           0-6              (Command)

MINS -- Minutes identifier(Range is 00-59)

HOURS -- Hours identifier(Range is 00-23 format)

DAYOFMONTH -- Day of month identifier(Range is 00-31)

MONTH -- Month identifier (Range is 01-12)

DAY OF WEEK -- Day of the week (Range is 0(Sunday)... 6(Saturday))

CMD -- shell script going to execute on specified time on crontab.  

Let's see various time specific examples using crontab

 *) How to run specific task on particular time - Example (March 18th 2013 at 5.30 PM)

   30 17 18 03 * /home/madhan/<shell>
   Note: Hour format uses 24hrs, so if you want to specific 5.30 AM use 5 in HOUR tab.If    you want to run at 5PM use 17 in HOUR tab.

 *) How to run multiple times on same day(for example i want to run the task at March  18 8am and 10PM)
   00 8,22 18 03 * /home/madhan/<shell>
   8,22 -- 8AM and 10PM  

 *) How to set every ten minutes will run the task on same day(for example March 18 in  between 11-12 but only for ten minutes interval)
   */10 11-12 18 03 * /home/madhan/<shell>  

 *) Suppose if you want to run everyday and every month at 2.30PM, just set the following time stamp on cron tab field.
 30 14 * * * /home/madhan/<shell>

 *) How to run task in between timings for example everyday at 9am-6pm (office       working hours)   00 9-18 * * * /home/madhan/<shell>  
  9-18 -(9am,10am,12pm,1pm,2pm,3pm,4pm,5pm and 6pm)

 *) How to run the above task only on weekdays(Monday-Friday only) 
    00 9-18 * * 1-5 /home/madhan/<shell>
    9-18 -(9am,10am,12pm,1pm,2pm,3pm,4pm,5pm and 6pm)
    1-5 (Monday,Tuesday,Wednesday,Thursday and Friday)

 *) How to view cron tab as logged-in user
    crontab -l (Will show you the list)

 *) How to view root user crontab list
    Login as root user and do "crontab -l"

 *) How to view other user crontab list
    crontab -u <username> -l  

 *) How to edit crontab item
    crontab -e  

 *) How to schedule task for every minutes
    * * * * * /home/madhan/<shell>

 *) Crontab has some special keywords for very frequent stuff just like daily,monthly yearly and reboot once stuff. Lets see
  @yearly equals to 0 0 1 1 * 
  @daily equals to 0 0 * * * 
  @monthly equal to 0 0 01 * *
  @hourly equals to 0 * * * *
  @reboot equals to Run at startup.(It will execute only once the machine got  booted everytime)  

 *) How to disable/redirect crontab mail output Normally crontab will send an output to who schedule the job. so if you dont want that email output just simply make it as empty MAIL variable
   MAIL = ""
  (You can redirect to log file something like > /tmp/cron_output/output.log) @yearly /home/madhan/ > /tmp/cron_output/output.log)

 *) How to specify path variable in crontab The above examples we are specifying absolute path of the shell script file to avoid that you can define that absolute path once in PATH variable.
 PATH =  /home/madhan After that you can specify just like @yearly <shell> Thats it.

Hopefully you can enjoy this article!!!

Sunday 17 March 2013

Scheduler using linux crontab

Suppose if you want to do some scheduler kind of tasks in ubuntu/linux platforms, crontab utility is very useful for that. Here i am going to explain you how to use this crontab for scheduler stuff.

* How to view what are the cron tasks available for you, just do the following command in your terminal/console.

madhan@ubuntu:~$ crontab -l (It will list of what are the scheduler tasks available for current user)

output of the above command is just like this

madhan@ubuntu:~$ crontab -l
*/5 12 17 03 0 bash /home/madhan/my_first_linux_shell_script > /tmp/mdn_cron1.log

detail about the above crontab command

*/5  ---> every 5 mins
12   ---> 12pm
17   --->  Day of the month
03   --->  Month identifier
0     --->  Day of the week( Normally 0(sunday)-6(saturday) )

"bash /home/madhan/my_first_linux_shell_script" ---> (command going to execute on exact time what we mentioned above. "my_first_linux_shell_script" is shell script file which has real commands going to run on scheduled time.)

>  /tmp/mdn_cron1.log (means output redirection to that log file, if anything fails we can just look into that file for further investigation ).

Ok Let's see How to add your tasks into crontab.

1) Let's create a shell script (I am ruby on rails developer, so i am just writing one small shell script which will run one rake task according to my project, so my shell script contains commands related RoR).

#! bin/bash
# This is my first linux shell script
echo "WoW shell script is working fine"
cd /home/madhan/abl/CyncAbl-R3
bundle exec rake cync:load_dilution_history

Just save this file in the extension of ".sh"(But recent ubuntu distributions its not required to save the file with extension of ".sh". So i saved the file without .sh extension)

2) Lets create one text file for your entire cron items.
*/5 12 17 03 0 bash /home/madhan/my_first_linux_shell_script > /tmp/mdn_cron1.log

<cron item 2>(as your wish if you have more that one scheduler)
<cron item 3>(as your wish if you have more that one scheduler)
<cron item4>(as your wish if you have more that one scheduler)

Just save the file with the extension of just ".txt". I save this file "my_cron_tab.txt"

3) Let's add that txt file into cron tab utility. The following command will use

 crontab /home/madhan/my_cron_tab.txt

You are almost done!!!

Lets see whether it will display correct cron item or not using this command

crontab -l (Will list out all scheduler task items available in "my_cron_tab.txt" file)

How to edit crontab, just do the following command
crontab -e

NOTE: if you stuck with executing the scheduler crontab, always you can view the log file in  /var/log/syslog. It has some entries, from there you can investigate further. In short just type it in your console "tail /var/log/syslog" it will show few last lines on that file).

Hopefully you can get little bit clear picture about linux crontab stuff.

Sunday 17 February 2013

group concat usage in MySQL

In this article, i am going to explaining usage of group_concat in MySQL with few example tables. Hope you people enjoy this concept, probably one who learn MySQL concepts. I am introduction two tables one is called "employee" table and "Department" table.

Structure and Data about employee table.


Structure and Data about department table.


Suppose if you want to query how many employees each and every department, you can use this following MySQL query.

SELECT AS department_name,COUNT(*) AS department_total_employee FROM department
JOIN employee ON employee.department_id =
GROUP BY employee.department_id

 Result of the above query


Just think about, now you got it how many employees available in each and every department.
If you want the detail, what the people belongs to which department in aggregate manner, you come know the real usage of GROUP_CONCAT method.

" It is used to concatenate multiple column values into single string". If you won't use this, you have to lookup multiple rows and then concatenate them.

* In side group_concat method, you can override default seperator comma using the following command. Example group_concat( SEPARATOR '--')

* You can change the order by also example
 group_concat( order by DESC)
The following query will give you the result of each and every department employee count as well as what are the employees on the particular department.

SELECT AS department_name,COUNT(*) AS department_total_employee,GROUP_CONCAT(employee.emp_name) AS employees_under_department FROM department
JOIN employee ON employee.department_id =
GROUP BY employee.department_id

  Result of the above query


Thursday 14 February 2013

How to implement bulk insert in Rails

Yeah we can implement using "ACTIVERECORD-IMPORT" library, in rails 3.x (It will not support for rails 2.x versions).

Just look at the following code snippets

Class User < ActiveRecord::Base
  ######## schema information about user ##########
  ######## schema information ended here #########
  has_many :posts
  has_many :comments
Suppose if you want to store/save 5-10 users into database table, normally you can do the following way

test_name= "user_name"
10.each do |number|
  User.create!(:user_name => test_name + number,:login_name => "login"+number,..etc fields)
The above code works fine without any issues. Just think about if you want to save 1000 or 2000 records, that time the above code snippets will eat huge time to save the records. So avoiding/handle that huge time difference Ruby on Rails, We can use the library called "activerecord-import".

Now we can insert 1000/2000 records at one shot using activerecord-import library. Here is the small code snippet implementation for your reference

users = []
test_name= "user_name"
1000.each do |number|
  users << => test_name + number,:login_name => "login"+number,..etc fields)
User.import users

Thats it. In single shot your entire 1000 records inserted into your database.

Note: This library only will work on Rails 3.x versions only not 2.x versions, so keep it this point before your implementation.

Thursday 7 February 2013

How to clone a git repository without history

Suppose if you have one git repository, if you are planning to share that repository to your working partner but it has some sensitive data/files. That time better create a new branch and edit those sensitive files and then get a clone of the repository without history then give to your parter. Using that cloned repository without history, your partner could not able to push that repository to anywhere else. But still you can get work done from your partner as "PATCHES".

Using the following command you can create a shallow repository with truncated/limited history.

git clone --depth 1 url_of_your_remote_repository

Limitation of shallow repository

* you can't clone or fetch from the shallow repository(That means you can't make a new repository using this shallow copy)

* you could not able to view whole history using git log command.

* Its workable solution, your partner can make some changes and make it as "patches" and send it to you partner if you wants your partner changes. Then apply that patches in your current working tree/ whatever branches you want to apply.

Saturday 2 February 2013

How to install gems group specific in your rails application

In Ruby on Rails application Gemfile, while you defining gems we can make that gem in a particular group. For example some gems needs to be loaded only on test environment so on that time we can mention those kind of gems into one specified groups. so your gemfile should like this

gem 'aws-s3'
gem 'paperclip'
group :test do
  gem 'rspec'
  gem 'waitr'
  gem 'faker'

If you are planning to define just one gem into a particular group only you can use without blocks like this

gem 'rest-client', :group => :development
gem 'cucuber-rails', :groups => [:development,:test]  (cucuber-rails gems comes under both group)

Similarly you can make it :development,:default(group name) or give a meaningful names whatever you want as group name.

Finally you have to tell while running the "bundle install" command just ignore what are the groups to be ignored using --without.

bundle install --without development
bundle install --without development test (ignore both groups and install others)
The above command ignore whatever gems we mentioned in the development group. So it will install rest of the default group(if you are not mention any group  those gems comes under defaults group)gems and test group gems.

bundle install --without development (ignore development group gems)
bundle install (still bundle remembers --without development so result is still ignore development groups it will not install all gems)

bundle install --without nothing (just clearing cache, now all the gems to be loaded into the ruby loadpath) 

Wednesday 30 January 2013

How to undo your local git commit

Suppose if you are planning to do undo changes your local commit this article will help you about that process.

If you committed some files and you made some commit messages after that if you are planning to do undo those commits because you did something wrong in the commit(may be you added unwanted files or changes which raise some errors). Before doing undo your local commits you have to think which ways you want that.

If you are planning undo local commit entirely whatever you changes you did on the commit, if you dont worry anything about that just do the following command.

git reset --hard HEAD^1 (This command will ignore your entire commit and your changes will be lost completely from your local working tree).

If you want undo your commit but you want changes you did on the commit into staging area(before commit just like after git add called staging area) then do the following command

git reset --soft HEAD^1

Now your committed files comes into staging area suppose if you want to unstage the files because you need to edit some wrong conent than do the following command

git reset HEAD

Now committed files comes into staged area into unstaged area now files are ready to edit so whatever you changes you want go edit and added it and make a fresh/new commit.

Hopefully this article helps you about how to undo your local git commit (not committed into remote repository).

Monday 21 January 2013

undefined method to_i in rails 3.2.11 upgrade

I created my rails application using Rails 3.2.3 version. After some period i come to know Rails 3.2.3 having some security issues, Here i have mentioned what is the issue

What is the exact security issue in Rails 3.2.3

People told that if we upgrade rails 3.2.3 into rails 3.2.11 issue will resolve otherwise we have to do some code tweaks for supporting rails 3.2.3 code base.

So we upgrade into rails 3.2.11. Now security issue was over But found some other issue called "undefined method to_i" error in few of the places. After search in Google, we come up with solutions

In rails 3.2.3
doctor = Doctor.first
patient =
patient.doctor_id =doctor (Right side Object assignment is valid)! (valid)

In rails 3.2.11
doctor = Doctor.first
patient =
patient.doctor_id = doctor (Right side Object assignment is invalid)! (invalid)
NoMethodError: undefined method 'to_i' error we got this kind of assignments. = doctor (is valid both left and right side are object reference)

So conclusion is we must use wherever _id column definition have to use INTEGER type only not just Object assignments. 

Sunday 20 January 2013

How to open multiple files in Vi editor

Suppose if you want to open multiple files in Vi editor than do the following commands

CTRL + W + s - That means it will open the same file(whatever file you opened currently for example if you open one file currently) again and the window split into Horizontally.

CTRL + W + v - That means it will open the same file in vertical manner.

After that if you want to open the new file on splinted window than do the following commands

:e <path of the file name> - It will open new file whatever you mentioned in the path.

Switch between multiple files:

ctrl + w + w - means it will go to next file(next tab). If you keep on pressing, you can easilly navigate which file you want to work.

ctrl+w+(h j k l) - It means it will navigate through you desired position either LEFT DOWN UP RIGHT manner.

How to close the opened multiple files

ctrl - w - o - It means it will close all windows other than current file(cursor located/pointing file).
ctrl - w - c - It means it will close only current window file not rest of them.

Tuesday 15 January 2013

How to delete remote branch in Git

Suppose if you are planning to delete remote branch do the following command might be very useful

git push origin :future_enhancement (Here future_enhancement is my remote branch name)

Note: If you are planning to remote master branch in git repository, might be faced some difficulties while deleting because of master branch is made it as "DEFAULT BRANCH".

Better come out from your master branch, just create any temporary branch/ if you have any other branch switch into that branch. Goto your loggin with your credentials

Click =>"Account settings" link on top right corner on the than Click =>"Repositories" tab from the left side menu than select your repository, there you can see "DEFAULT BRANCH" dropdown from there you have to select some other branches as default than save. Thats it from

Than you come to your local machine, now try to run the following command,

git push origin:master (Now master branch is deleted from the remote repository).

Thursday 10 January 2013

How to rename your local branch name in Git

Suppose if you are planning to rename your local git branch after sometime checkout. The following command will be useful. For example if you checkout previously "master" branch now you decided want to rename your local branch name than do this

git branch -m master working_copy
Immediately your local branch name is renamed into "working_copy".

NOTE: If you edited,added some files in "working_copy" branch and than you did commit those changes and than if you try to push into master do the following command.
 git push origin working_copy:master

If you do "git push origin working_copy" means it will create a new branch "working_copy" in remote repository with your recent changes(not pushed into Master branch).

Tips: git remote origin show - this command will tell you what are the local branches  linked into which branches in remote repository. It will show the permission on push,pull, merge command specific.

Checkout a branch from github

If you planning to checkout git repository, you have to use git clone command like this

git clone <either ssh url /http url>

If you do the above command,checkout all the branches but normally master branch only will gets initializes. If you want to checkout other branches you have to tell like this...
git checkout -t origin/future_branch (for example) - This command checkout the remote branch and your local branch name will be same as remote branch.

Suppose if you want to override your local branch name while you checkout, do the following command

git checkout -t -b enhancement origin/future_branch - Now your local branch name is "enhancement" but your remote branch name is "future_branch".

Wednesday 9 January 2013

Linux Vi editor basic commands

 This article will help you, if you are very beginner in Vi editor in Linux platform.

Here is the list of commands for your reference.

* Cursor movement commands

h - left side single character move
j  - down by one line from current cursor position
k - Up one line above from the current cursor position
l  - Right side single character move

* file Edit commands
  * vi +5 <file name> - It will open the file then the cursor position will stay on line no 5( 5 is just example only)

* Search related commands
  * vi +/string - It will open the file and search the given string after that cursor will stay on first occurrence of the given string.

 *  /<string> - It will find a string in the forward way. If you want to next find then press "n"
 * ?<string> - It will find a string in backward way. similarly if you want to find next then press "n"

* Insertion commands
  o(alphabetical letter) - It will insert one line below the current cursor position.
  O(alphabetical letter) - It will insert one line above the current cursor position.

* Delete commands
  dd - It will delete one line from your current cursor position.
  5dd - It will delete 5lines from your current cursor position.(5 is just example only)

* Copy  commands
  yy - It will copy current cursor line.
  5yy - It will copy 5 lines from current cursor position line.(5 is just example only)

* Paste command
  p - It will paste whatever you copied.

* Word jumping/movement
   w - it will move one word to next word (cursor position will be at starting of the string)
    b - Back to previous word (cursor position will be at starting of the string only)
    e - Move to next word (cursor position will be end of the next word)

* Goto Line no :
 ESC then type :<line no>