UPDATE, this is now a gem!
As I’ve mentioned before, I’m a huge fan of using rsync for deployment. When working on smaller sites and projects, I find that the other deployment method that I use; capistrano, is a bit of overkill, I rarely, if ever need to rollback revisions or share assets across deployments on simple websites. Since I use git, I can rollback revisions and ignore certain files using .gitignore. With that said, I think that I’ve come up with the most simple and perfect way to deploy small projects. This assumes that you use the following:
- OSX or any *nix variety OS (haven’t tested it on ubuntu, but I assume this’ll work the same)
- rsync / ssh
- git
- and of course ruby
- textmate or any other editor that accepts textmate’s bundle snippet system
- Add the following snippet to your bundles:
- Assign the word
deploy for the snippet expansion.
- Add a file called
deploy.rb to the root of your project, make sure and chmod 755 it.
- Type
deploy and expand the snippet and fill in your deployment details.
- Once you are ready, just run
./deploy.rb in the terminal.
The great thing about this is that you ignore files on deployment simply by using .gitignore and rollback using git. Why attempt to re-do those things when git does such a good job of it itself? Enjoy!
I recently needed to grab a git repository, but I had no interest in cloning it or putting it into my current project as a submodule. What I wanted was a simple way to export the repository like you can with svn using svn export. I looked around for a solution, but couldn’t find one that I felt behaved exactly like svn export. So I decided to write my own git-export command, once installed you can simply run:
git-export [git-repository-url] [optional destination]
What it does is actually pretty simple, it just clones the repository and removes the .git directory. That’s all I needed it to do, no more, no less, you can add this to your git arsenal by grabbing a set of git utilities that I forked at : http://github.com/vanntastic/git-utils , install instructions are provided on the readme.
Ever need to get the current branch name in a ruby app?
b = `git branch`.split("\n").delete_if { |i| i.first != "*" }
b.first.gsub("* ","")
Or maybe you might want to put it into ~/.irbrc:
def branch
b = `git branch`.split("\n").delete_if { |i| i.first != "*" }
b.first.gsub("* ","")
end
I recently had to compile the latest version of git on CentOS 5, here’s what worked for me.
## Compiling and installing git 1.6.x on Centos5
# if you need to get dependencies
sudo yum install curl-devel
sudo yum install expat-devel
sudo yum install gettext-devel
sudo yum install openssl-devel
sudo yum install zlib-devel
sudo yum install wget
# you are using /usr/local/ right?
cd /usr/local/src/
wget http://kernel.org/pub/software/scm/git/git-1.6.5.1.tar.gz
tar zxvf git-1.6.5.1.tar.gz
cd git-1.6.5.1
make configure
./configure --prefix=/usr/local
sudo ./configure --prefix=/usr/local
sudo make all
sudo make install
So when you do:
git add .
It automatically adds files to your index ready to be committed right? But when I did:
git rm .
I assumed it would remove files/folders that you have removed from the filesystem, kinda like the inverse of how git add . should be. But it doesn’t, it gives you something that about not removing the directory you are currently working in. Here’s what it says in the git manual about git rm:
Remove files from the index, or from the working tree and the index. git-rm will not remove a file from just your working directory. (There is no option to remove a file only from the work tree and yet keep it in the index; use /bin/rm if you want to do that.) The files being removed have to be identical to the tip of the branch, and no updates to their contents can be staged in the index, though that default behavior can be overridden with the -f option. When —cached is given, the staged content has to match either the tip of the branch or the file on disk, allowing the file to be removed from just the index.
Notice they say use bin/rm to remove a file from the work tree and still keep it in the index… what? Ok, but for me I’ll usually just want to remove a file/directory from both the work tree and the index, it just puts more work on the developer to manually have to remove it from the git index when you have just removed it from the filesystem. Granted it’s a simple git rm whatever/file/you/deleted/ but once you multiply that by a bunch of files in directories you have removed, it becomes tedious. So what do we do?
Sed, Grep and Bash to the rescue!
By using the power of sed and grep you can now freely delete your files and directories as you wish without worrying about manually git rm-ing everything. I decided to write a quick bash function to give me that behavior, drop this in your ~/.bash_profile or ~/.bashrc and you’ll be good to go:
# removes all deleted files / directories
# kinda like what git rm . should do
git_rm_all (){
git st | grep deleted | sed -e 's/deleted: *//' | sed 's/# *//' | xargs git rm
}
Using git and rails together? Need some git love in your console? Here’s a list of command git commands that I often use, abstracted into a method, just throw this in ~/.irbrc :
# git hook
def git(*args)
if args.length == 1
# EX : git :st
# git "push origin master"
cmd = "git #{args.first.to_s}"
elsif args.length > 1
# EX : git :st, :origin, :master
# git :push, :origin
cmd = args.first.to_s
args.delete_at(0)
arg = args.join(" ")
cmd = "git #{cmd} #{arg}"
elsif args.blank?
cmd = "git [*cmd] or git.[st|ci|push|config]"
end
cmd.instance_eval do
# git.st
def st
system("git st")
end
# git.ci 'msg'
def ci(msg)
system("git add .;git commit -m '#{msg}'")
end
# git.push 'args'
def push(args=nil)
args.nil? ? system("git push") : system("git push #{args}")
end
# git.url
def url
system 'cat .git/config | grep url'
end
# git.config
def config
system 'vi .git/config'
end
end
args.blank? ? cmd : system(cmd)
end
Now you can do things like:
# git status
>> git.st
>> git :st
# git push 'msg'
>> git.push 'msg'
>> git :push 'msg'
# you can even modify the config in vi from the console
>> git.config
Go ahead and fork the gist at : http://gist.github.com/162857
Themed by
langer with slight modifications by me, powered by
Tumblr.