#96
Mar 10, 2008

Git on Rails

Git has been getting a lot of buzz lately, and for good reason. It's an excellent SCM which in many ways is more simple and powerful than subversion. See how to set up a Rails project with Git in this episode.
Tags: tools
Download (7.8 MB, 5:56)
alternative download for iPod & Apple TV (6.8 MB, 5:56)

Resources

# commands
git init
git status
git add .
git commit -a
touch tmp/.gitignore log/.gitignore vendor/.gitignore

# .gitignore
.DS_Store
log/*.log
tmp/**/*
config/database.yml
db/*.sqlite3

RSS Feed for Episode Comments 45 comments

1. jonuts Mar 10, 2008 at 02:56

heh i was scourging the entire internets for this exact screencast last night. thanks!


2. Matt Beedle Mar 10, 2008 at 03:03

Git does look pretty good, but so far subversion has done everything I have needed it to.


3. Senthil Nayagam Mar 10, 2008 at 04:03

Hearing good things about GIT for quiet some time but, lack of windows support is holding us back.

till we get some native binaries(not cygwin), we would remain with subversion.


4. Pete Yandell Mar 10, 2008 at 05:52

I use the following little "git-rails" script to take a new Rails project and initialise it as a git repository.

#!/bin/sh

git init

cat <<EOF > .gitignore
.DS_Store
log/*.log
tmp/**/*
db/*.sqlite3
coverage
doc/app/*
EOF

find . -type d -empty -exec touch {}/.gitignore \;

git add .
git commit -a -m "Initial import."
 


5. Jo Potts Mar 10, 2008 at 06:06

Thanks Ryan, another great screencast.

I'd been holding back for the same reason Matt gave. The try-something-new temptation is strong though... so time to give it a try me thinks!


6. Martin Ström Mar 10, 2008 at 06:11

Another great screencast.
One thing, you dont need to use `git add .` if you commit using the -a option. It will automatically add all modified files to the commit.


7. Claudio Caseiro Mar 10, 2008 at 06:21

Martin, I think you are wrong about using "git add .". To add new files to repository (index) you need to use "git add ." before commit.
"git commit -a" only adds modified files that are already part of the repository, not new files. So after "git init", or every time you add new files you should use "git add ."


8. Martin Ström Mar 10, 2008 at 06:33

Yes, sorry. I meant that that "git add ." is only needed the first time for new files and the following times (for existing files) should "git commit -a" be enough.


9. Carl Mar 10, 2008 at 07:52

A future screencast I'd love to see would be about how to do testing when you are using Restful Authentication. I'm missing something (I hope) because it seems to be a bear to do, and your sceencasts always help with those sort of problems.


10. Luis Lavena Mar 10, 2008 at 12:06

@Senthil:

Better Git support is built into msysGit:

http://code.google.com/p/msysgit/

Still, lacks git-svn support due perl bindings, but is usable :)

In any case, the steeped learning curve of Git (ala: the bad UI design it has) make me stay away from it and keep using bazaar as SCM.


11. Gav Mar 10, 2008 at 15:56

I feel a little silly asking, but what does ** mean?

Ryan and Pete Yandell (in a comment) used it in their .gitignore files

  tmp/**/*

Thanks


12. Nicolás Sanguinetti Mar 10, 2008 at 23:31

@Gav: something/**/* will go down all the subdirectories of 'something' and select all the files inside.

@Ryan, Pete: shouldn't you also ignore the schema.rb?

In my personal experience, git is the best thing that happened to my workflow since rails :)


13. Stephen Wooten Mar 11, 2008 at 01:08

Prior to getting my new mac, I was on windows and was playing around with a windows port of Git called 'Git Bash'

http://code.google.com/p/msysgit/

and a little installation walkthrough:

http://kerneltrap.org/Linux/Git_on_Windows

--Good luck guys!


14. Stephen Caudill Mar 12, 2008 at 11:19

I'd highly recommend staying away from braid for the time being. In my experience it's been very buggy and has out and out thrashed a particular project badly enough that I had to re-import it form SVN.

Piston has forthcoming git support, so I'll be waiting for that.


15. Jörg W Mittag Mar 12, 2008 at 18:20

Hi,

I just wanted to quickly point out that you are putting the ".DS_Store" into the wrong .gitignore file, IMHO. You put it into the ToDo app's .gitignore file, but actually it's not specific to the ToDo app, it is specific to your development environment (MacOS X). So, it belongs in your systemwide .gitignore, not the project-specific one.

jwm


16. Diego Mar 13, 2008 at 03:23

Git rocks!

as well as Linux and Linus Torvalds ;)

thanks for this screencast


17. Rajesh Duggal Mar 14, 2008 at 08:55

There's git support for script/generate in edge.

http://dev.rubyonrails.org/changeset/8772

e.g. script/generate -g

Cheers,
Rajesh.


18. James H Mar 14, 2008 at 16:08

A note to those using Autotest: you might want to add an exception to ignore your '.git' folder, lest you wish Autotest to slow to a halt.

See http://blog.davidchelimsky.net/articles/2008/01/15/rspec-1-1-2-and-zentest-3-8-0


19. Diego Viola Mar 17, 2008 at 02:24

This is also very nice to have it as a reference.

http://zrusin.blogspot.com/2007/09/git-cheat-sheet.html


20. Gavin Baker Mar 18, 2008 at 08:07

@Nicolás: how is that different to something/*/* ?

Gav


21. devon Mar 18, 2008 at 16:34

Should we add schema.rb in the .gitignore?

My .gitignore contains:
db/*
!db/migrate

and also

public/cache/**/*


22. Ryan Bates Mar 19, 2008 at 16:36

@Stephen, thanks for the input. I removed Braid from the list of links for now. Looks like Git support for Piston is coming along nicely! Can't wait!

@Jörg, I agree, excellent point!

@Gavin, I believe ** will traverse the directory to multiple depth, but someone please correct me if I'm wrong.

@devon, I like to keep my schema.rb file under version control so if someone downloads the projects they can just run db:schema:load rather than running all the migrations. The comments at the top of schema.rb file recommend doing this as well.


23. Caleb Mar 22, 2008 at 16:49

There's a pretty comprehensive git textmate bundle available:

http://blog.macromates.com/2008/git-bundle/

Make sure you set the TM_GIT textmate environment variable to the path of your git binary (/opt/local/bin/git for macports for example)


24. jujudellago Mar 28, 2008 at 02:32

Used to work with CVS, got really excited with SVN as replacement, but as linus said, they got it all wrong from the begining....

it's not a technical screencast, but I recomand that video http://www.youtube.com/watch?v=4XpnKHJAok8

a bit long, but very interresting, fun presentation at google talks


25. Ayyanar Mar 29, 2008 at 04:45

Any IDE supports GIT to check-in check-out directly like RADRAILS supports SVN


26. Abizer Nasir Apr 13, 2008 at 15:28

You might not always want to use the -a option with commits.

I like the power of the index just having the state of the files when they were added. Using the -a option means that the index is updated with the current state of the file.


27. Kino May 03, 2008 at 07:24

Why not left the old version?


28. slothbear May 03, 2008 at 22:02

I have to admit I was a little confused about adding vs. the "-a" option vs. the index. And comments (in the Peepcode) about git tracking "content", not "files". Whaaaaa.

An article by Ryan Tomayko cleared it all up for me. It's a little more advanced. You might not need his actual technique, but it illuminates the topic of "adding".

http://tomayko.com/writings/the-thing-about-git


29. sthapit May 18, 2008 at 13:41

anyone know if there is a plugin for git that i can use for eclipse? i've gotten used to right-click -> commit and team -> revert etc. and doing those things on the command line feels like a step backwards...


30. Manchester Rob May 23, 2008 at 01:00

Git looks great, but can it work with deprec/capistrano/mongrel as well as subversion does?


31. James B. Byrne May 29, 2008 at 06:50

As of this moment GiT users face the same problem that every earlier adopter does, a lack of compatibility and support in most ancillary tools. This, however will change over time and the decision of when to move to GiT may well be heavily influenced by what tool is considered most important.

In our case we recently moved from Trac to Redmine because we decided that the VCS was a more vital technology to our future efforts than the Project Administration System. As Trac support for GiT is very minimal and has not, to my recent knowledge, been slated for core implementation we decided to move PAS as well as VCS. Others will decide to stay on Subversion until their desired tools provide better support for GiT.

I have found GiT's UI, for the most part, simple and straight forward to use. I have zero experience with the the GUI's available but the command line instructions could ahrdly be simpler.

The whole structure of GiT is a marvel to consider. The insight of the designer is, for me, dumbfounding. One must consider that this entire project is really nothing more than an innovative combination of tools that have been present on most Linux distributions for years.

GiT's innovations have tremendous possibilities for OS fs security and data compaction in future Linux distributions. I believe that GiT is going to radically change computing. Consider the value of knowing simply by looking at the SHA1 value of the system whether or not any file in /etc or /var/share or /usr/local has been changed or needs to be updated!

The SVN folks themselves have conceded that the OOS community mind-share has committed to DVCS and that SVN will likely be the end of the line for centralized repository systems in OSS. Whether this means that GiT will be "THE" OSS DVCS is another issue.


32. ozgun Jun 09, 2008 at 21:10

Hi Ryan,

Thanks for your great rails screencasts.

I've small tip for the people who use "vim" as code editor. If you want to exclude ".swp" files, just add "*.swp" to your ".git/info/exclude" file.


33. nerb Jul 16, 2008 at 20:23

for those that use capistrano, and had followed this screencast, make sure you DO NOT add the log/*.log and config/database.yml to your .gitignore file.

Capistrano will spit out error messages when you begin to migrate your db.

Learn from me :-)


34. Steffen Jul 19, 2008 at 01:51

Hi all,

I've tried out some cases for the .gitignore file. How this file have to be if I would exclude all file except two?

# .gitignore
*
!days.txt
!directory

But if I add a file to directory I get a message, that I have to use git add -f to add to the staging area. Could anyone help me?


35. Peter Jackson Jul 20, 2008 at 11:13

@nerb -

If you use Capistrano, it pays to exclude the log files and database.yml from git, then create the files in the shared/ folder (that is shared between different releases) and then symlink them into your project.

The benefits of excluding those files from source control are huge when you have a handful of developers, each with a different machine configuration.


36. Peter Jackson Jul 20, 2008 at 11:58

@nerb - A quick follow up. Add this to your deploy.rb:

task :after_update_code, :roles => [:web, :db, :app] do
  run "ln -nfs #{deploy_to}/#{shared_dir}/config/database.yml #{release_path}/config/database.yml"
end


37. Dnew Oct 08, 2008 at 11:13

Simple question for noob.
I am trying to figure out the correct url for my capistrano deploy script for the local git repo this sc uses.

"git://localhost:3000/.git"?

Whatever i try gives me errors. I am on Leopard.


38. Andreas Oct 10, 2008 at 06:00

@Dnew: Try 'set :repository, "file:///Users/[path to project]/.git"'


39. Harry Seldon Nov 11, 2008 at 23:46

Thanks for this great screencast. After seeing it, some might want to go to this short tuto I wrote. It explains why git is so useful and how to use it along with github and an open source project:
http://harryseldon.thinkosphere.com/2008/11/08/grand-gardening-with-git

(My name contains the active link)


41. tiffany rings May 06, 2009 at 18:24

Why not left the old version


42. replica Links of London Jewelry May 07, 2009 at 01:21

Thanks for the Gits info!


43. ed hardy Jun 28, 2009 at 23:27

Very cool & useful gem!

Almost sent you a comment on the future dates ;-)


44. tiffanys Jun 30, 2009 at 23:31

GOOD


45. abercrombie Jun 30, 2009 at 23:32

GOD!


46. mortgage rates Jun 30, 2009 at 23:33

NICE

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player