Git: How to Delete a Branch with an Invalid Name

If you’ve named a branch beginning with two dashes “–”, you’re sort of in trouble because git interprets your branch name as a switch/flag. You can skip switches all together
by supplying two dashes before your branch name

git branch -d -- --index_for_suppliers

and your branch will be deleted!

How to Remove Your Last Git Commit

Remove your last commit (if you haven’t pushed yet)

git reset --hard HEAD~1

To see changes that have been committed and their position in HEAD

git reflog 

And to undo your previous reset and advance the cursor to the reference immediately behind the current state

git reset --hard HEAD@{1}

If you have already pushed you can

git revert HEAD

which will reverse your last commit by creating a new commit

Backup and Rotate MySQL Databases Simple Bash Script

Make a directory ( it can anywhere ) called baks/mysql

mkdir -p /baks/mysql

Create a file (it can be anywhere) called /root/mysql_backups.sh and put this script in it

#!/bin/bash

# modify the following to suit your environment
export DB_BACKUP="/baks/mysql"
export DB_USER="root"
export DB_PASSWD="your-mysql-password-goes-here"

# title and version
echo ""
echo "Backup and rotate all mysql databases"
echo "--------------------------"

rm -rf $DB_BACKUP/04
mv $DB_BACKUP/03 $DB_BACKUP/04
mv $DB_BACKUP/02 $DB_BACKUP/03
mv $DB_BACKUP/01 $DB_BACKUP/02
mkdir $DB_BACKUP/01

echo "* Creating backup..."
mysqldump --user=$DB_USER --password=$DB_PASSWD --all-databases | bzip2 > $DB_BACKUP/01/mysql-`date +%Y-%m-%d`.bz2
echo "----------------------"
echo "Done"
exit 0

Install it via cron and have it run at 3:10 am every morning.

crontab -e

10 3 * * * /root/mysql_backups.sh  > /baks/status.log

This script will save the last 4 days of data.

Ruby Rand Range

I assumed that rand would take a range as an argument. Something like rand(10..20), generating a random number between 10 and 20. Seems like you’d do this fairly often when working with random numbers and therefore, included. However, it doesn’t work. But the solution is almost as easy.

10 + rand(11) #=> produces a random number between 10 and 20

Since rand starts at 0 (like array indexes), we need to add an extra 1 to get what we’re expecting.

Free Online Design Tools and Resources for Programmers

I’m a programmer. At least I want to be. But the reality of web development requires wearing multiple hats. This includes from time to time, working on design. After all what is a web app if it is poorly designed? Both UI and aesthetics are important from customer/user perspective.

Fortunately, there are many great designers who are also developers (or who have developer friends) and they release cool tools to help out. So here is a list of resources I’ve found useful when wearing the designer hat.

http://0to255.com/ – when working with color
http://framebox.org/ – rapid prototyping and sharing

http://subtlepatterns.com/ – spice up the background with some subtle patterns

http://www.html-ipsum.com/ – lorem ipsum text wrapped in different html elements

http://placehold.it/ – placeholder image spaces
http://flickholdr.com/ – placeholder images from flickr
http://placekitten.com/ – turns any project into a success

http://www.copypastecharacter.com/ – copy and paste special characters/lookup
http://www.cssbuttongenerator.com/ – make nice css buttons
http://www.colorzilla.com/gradient-editor/ – css gradients
http://www.red-root.com/sandbox/holmes/ – clean up, css debugger
http://gridulator.com/ - make a grid layout

http://warpspire.com/talks/designhacks/ Not necessarily a tool, but a great presentation on design geared at developers

Link to jQuery Source from Google’s CDN

https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js

That is the link to the jQuery source hosted by Google on their CDN. It’s probably already cached on client machines so it should be as fast as is possible! You can read more/use other Javascript libs from Google’s CDN here: http://code.google.com/apis/libraries/devguide.html#jquery

Simple String Concatenation of a Collection Written as a Helper for Rails

At Railsconf last week I took Greg Pollack’s online course Rails Best Practices. The interface is gorgeous and the instructions are excellent. One of the lessons involved taking a partial and moving it into a helper. I was reminded how difficult such a simple task can be. I have written about this before Yield a Block Within Rails Helper Method with Multiple Content Tags. However, that post aims to solve a slightly different problem, where the helper method takes the captured text from a block passed as an argument which essentially acts as a wrapper.

The difficulty isn’t with the logic itself and or the complexity/verbosity that the code is likely to produce. Rather, it is difficult because you have to endlessly concatenate strings and this something somewhat uncommon when programming with ruby. We have to remember that we’re working with a buffer of text about to be flushed. Here is a simple code snippet that shows how to write a helper method that loops over a collection of objects.

  def bookmarks_for(user=nil)
    content_tag(:div, :id => "bookmarks") do
      user.bookmarks.each do |bookmark|
        concat(
          content_tag(:strong) { bookmark.member_name } +
          excerpt(bookmark.body, '', 100)
        )
      end.join
    end
  end

Notice the use of the concat() method the “+” sugb and .join() method. All three of which bring these statements together into one final piece of html called in the view.

<%= bookmarks_for(@user) %>

How to Copy and Paste to/from the Global Register with Tmux on Mac OS X

Using the system clipboard with tmux on OS X is broken. I really like tmux but having copy and paste is kind of important for me. Here is my attempt to get it back with a simple bash script until I find something better. The approach is to take a named pipe and feed it the contents of “tmux showb”. A bash script will manage the pipe and because this script is initialized from a normal session it will write to the system clipboard just fine.

In my .bash_profile…

pipe4tmux=/tmp/pipe4tmux
alias tcp="tmux showb > $pipe4tmux"
if [[ ! -p $pipe4tmux ]]; then
~/pipe4tmux.sh &
fi

And in pipe4tmux.sh…

#!/bin/bash
pipe4tmux=/tmp/pipe4tmux
echo "Starting named pipe $pipe4tmux"
trap "rm -f $pipe4tmux" EXIT

if [[ ! -p $pipe4tmux ]]; then
  mkfifo $pipe4tmux
fi

while true
do
  pbcopy < $pipe4tmux
done

echo "Quitting pipe4tmux $pip4tmux"

Reusing Scopes (Formerly Named_scope) In Rails 3

You can easily chain scopes together in your models.

class Article < ActiveRecord::Base
scope :ordered, order('position ASC')
scope :published, ordered.where('published = ?', true)
scope :for_homepage, published.limit(3)
end


Article.for_homepage.to_sql
# => SELECT \"articles\".* FROM \"articles\" WHERE (published = 't') ORDER BY position LIMIT 3

Installing MatPlotLib on OS X for Python Version 2.6.1 with PIP and VirtualEnv

If you thought you had installed matplotlib only to find this

File "/Library/Python/2.6/site-packages/matplotlib-0.91.1-py2.6-macosx-10.6-universal.egg/matplotlib/numerix/__init__.py", line 166, in
__import__('ma', g, l)
File "/Library/Python/2.6/site-packages/matplotlib-0.91.1-py2.6-macosx-10.6-universal.egg/matplotlib/numerix/ma/__init__.py", line 16, in
from numpy.core.ma import *
ImportError: No module named ma

It is because the package being installed is version 0.91 and you need at least version 1.0 .

If it’s already installed pass pip the upgrade flag and specify the package location with the “-f” flag

pip install --upgrade -f http://downloads.sourceforge.net/project/matplotlib/matplotlib/matplotlib-1.0/matplotlib-1.0.0.tar.gz matplotlib

If not installed

pip install -f http://downloads.sourceforge.net/project/matplotlib/matplotlib/matplotlib-1.0/matplotlib-1.0.0.tar.gz matplotlib

Resources:

http://stackoverflow.com/questions/3555551/why-does-pip-install-matplotlib-version-0-91-1-when-pypi-shows-version-1-0-0