How to resize Macintosh HD?
Please please make sure that you type in the correct partitions. Double-check and triple check before running the merge command. It’s really important that you backup all of your data. I’m not taking any responsibility if you manage to erase anything. You will not want to lose data if there’s a sudden loss of power mid-operation.


In Disk Utility, create a new partition from the unused space.
Open terminal.app, and from there you’ll run diskutil mergePartitions


First you need to find out which partitions you’ll want to merge:

disktuil list


And hopefully you’ll see something like this:

/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.1 GB   disk0
   1:                        EFI                         209.7 MB   disk0s1
   2:                  Apple_HFS Macintosh HD            499.2 GB   disk0s2
   3:                  Apple_HFS Untitled                 50.0 MB   disk0s3   
   4:                 Apple_Boot Recovery HD             650.0 MB   disk0s4


You’ll want to merge disk0s2 with disk0s3. So you’d run something like this:

diskutil mergePartitions JHFS+ DiskName disk0s2 disk0s3


Beware that the above will merge partitions between disk0s2 and disk0s3. Data on disk0s2 is preserved, data on all the partitions in-between is erased. In my example the two partitions are contiguous so there’s no problem. But if your recovery partition is in between it will be erased.

If you need help with the mergePartitions command:


  Usage:  diskutil mergePartitions [force] format name
          DiskIdentifier|DeviceNode DiskIdentifier|DeviceNode
  
  Merge two or more pre-existing partitions into one.  The first disk
  parameter is the starting partition; the second disk parameter is the
  ending partition; this given range of two or more partitions will be
  merged into one.
  
  All partitions in the range, except for the first one, must be
  unmountable.
  
  All data on merged partitions other than the first will be lost; data
  on the first partition will be lost as well if the "force" argument is
  given.
  
  If "force" is not given, and the first partition has a resizable file
  system (e.g. JHFS+), it will be grown in a data-preserving manner,
  even if a different file system is specified (in fact, your file
  system and volume name parameters are both ignored in this case). If
  "force" is not given, and the first partition is not resizable, you
  will be prompted if you want to erase.
  
  If "force" is given, the first partition is always formatted. You
  should do this if you wish to reformat to a new file system type.
  
  Merged partitions are required to be ordered sequentially on disk. See
  diskutil list for the actual on-disk ordering; BSD slice identifiers
  may in certain circumstances not always be in numerical order but the
  top-to-bottom order given by diskutil list is always the on-disk
  order.
  
  Ownership of the affected disk is required.
  
  Example: diskutil mergePartitions JHFS+ NewName disk3s4 disk3s7
           This example will merge all partitions BETWEEN disk3s4 and disk3s7,
           preserving data on disk3s4 but destroying data on disk3s5, disk3s6,
           disk3s7 and any invisible free space partitions between those disks;
           disk3s4 will be grown to cover the full space if possible.