#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 72 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. 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


28. 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...


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

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


30. 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.


31. 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.


32. 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 :-)


33. 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?


34. 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.


35. 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


36. 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.


37. Andreas Oct 10, 2008 at 06:00

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


38. 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)


39. David Jul 06, 2009 at 09:02

One small suggestion to Pete's suggested script. I move the find script that inserts .gitignore into blank dirs BEFORE running git init. This avoids placing .gitignores into the .git directory. Harmless but not very clean.
Here's the gist
http://gist.github.com/141509


40. Michael Jul 11, 2009 at 21:04

How do you automatically add new files? What is the point of adding a directory if the directory isn't monitored for new files?


41. şarkı dinle Nov 03, 2009 at 00:59

How do you automatically add new files?


42. oyun gemisi Nov 03, 2009 at 01:00

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...


43. sinema Nov 03, 2009 at 01:01

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


44. moda Nov 03, 2009 at 01:02

Better Git support is built into msysGit:


45. hisse tahmin Nov 03, 2009 at 01:03

A note to those using Autotest: you might want to add an exception to ignore your '.git' folder


46. bölüm izle Nov 03, 2009 at 01:03

Simple question for noob.,


47. jude Nov 20, 2009 at 02:25

How to use git in eclipse


48. earth eternal gold Dec 24, 2009 at 18:27

I like computer.I want to learn more about it.You help me at the computer language.Thank you.Have a good time.Merry Christmas!


49. james Dec 27, 2009 at 17:56

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...


50. Hermes bags Dec 27, 2009 at 18:00

Try Try Try


51. Michael McCracken Jan 05, 2010 at 13:26

I had installed git a while back, but hadn't done anything with it. However, after fussing with Subversion branching/merging - I had fresh initiative to use git. This screencast really demonstrates ease in which to setup a rails project under git.

Thanks Ryan, you are providing a valuable service with RailsCasts!


52. borsa forum Jan 31, 2010 at 10:58

thanks admin.


53. dizi izle Mar 18, 2010 at 03:56

Enjooy!


54. Chris Mar 18, 2010 at 04:14

The new Rails 3 binary will actually automagically create all the bare necessary .gitignore and .gitkeep files you may need. It does this by default, but you can opt-out of creating them by passing -G as an option on the command line.


55. supra shoe May 04, 2010 at 00:26

fantastic


56. wholesale jerseys May 19, 2010 at 18:23

Hey guys,
I hope this allowed, I have never used this website before so I wasn't really sure what this was going to do. So this is just a test post. I really like this forum, it has some excellent discussions that take place.


57. http://ambienbuy.net May 30, 2010 at 09:50

This is an excellent article, thank you very much for it,
I have found here much useful information in
http://ambienbuy.net/VALIUM.html


58. Matt Di Pasquale May 31, 2010 at 19:08

If I'm repeating someone above, feel free to delete this, but instead of creating .gitignore files in the directories you want to add to the repo, you can just add them with:
git add -f tmp log vendor


59. Aydin Jun 03, 2010 at 04:03

Thank you How do you automatically add new files? What is the point of adding a directory if the directory isn't monitored for new files?


60. Formal dress Jun 04, 2010 at 00:58

Thanks for the heads up. Google are definitely planning something even bigger than just an internet connection and additional smart-phones.


61. discount nfl jerseys Jun 29, 2010 at 00:55

THANK YOU FOR YOUR POST


62. Detroit Lions jerseys Jul 01, 2010 at 18:51

hey buddy,this is one of the best posts that I’ve ever seen; you may include some more ideas in the same theme. I’m still waiting for some interesting thoughts from your side in your next post.


63. cerly Jul 02, 2010 at 19:58

<a href="http://www.gucciusaoutlet.com" target="_blank"><strong>designer handbags Gucci</strong></a>

http://www.gucciusaoutlet.net/index.php?main_page=index&cPath=77&zenid=aa7cf3c1c03ba16b9ac00c8cd2a23ef2


64. cerly Jul 02, 2010 at 19:59

<a href="http://www.gucciusaoutlet.com" target="_blank"><strong>designer handbags Gucci</strong></a>

http://www.gucciusaoutlet.net/index.php?main_page=index&cPath=77&zenid=aa7cf3c1c03ba16b9ac00c8cd2a23ef2


65. ghd straighteners uk Jul 14, 2010 at 05:22

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I’m sure to all of the commenters right here! I bet you had enjoyable scripting this article, didn’t you?


66. rowing machine Jul 17, 2010 at 00:26

Nice website! Truly impressive and nice information. Thanks for sharing.


67. ed drugs online Jul 25, 2010 at 06:28

Thank for you


68. chanel series Jul 27, 2010 at 00:39

Buy classic and cheap chanel shoes from us is the most right choice for you.Free shipping and fast delivery.Welcome.


70. folding bike Aug 09, 2010 at 18:27

I am happy to find a beautiful post for me. Now I am planing to leave for sleeping. I hope you will add that. Thanks


71. price strip Aug 09, 2010 at 18:29

Thanks for this great post, i like this article very informative.


71. digital thermometer Aug 09, 2010 at 18:31

Thanks for the great topic. It was enlightening.


72. infrared forehead thermometer Aug 09, 2010 at 18:33

Wow thanks for the article! Please keep writing article's like these, I really liked it!


73. forehead thermometer Aug 09, 2010 at 18:35

Great solution ,That sounds very interesting .


74. rattan furniture Aug 09, 2010 at 18:37

I am totally agree with your oppinion.this blog post is very encouraging to people who want to know these topics.


75. free directory list Aug 11, 2010 at 22:25

If God would exists it will be you... very thanks for this screencast.


76. cheap nfl jerseys Aug 12, 2010 at 20:13

 it is interesting and informative article. This has been very helpful understanding a lot
of things. I’m sure a lot of other people will agree with me.

 lots of good advice on your post. and as a return,i will buy a Cheap nfl jerseys to you for my thanking.


77. Air Jordan Spizike Aug 22, 2010 at 23:03

Awesome post! Thanks for sharing such nice information with us. I am really interested in this learning about this. , I feel strongly about information and love learning more on this. Thanks for the information. This is a wonderful post!!


78. omega watches Aug 23, 2010 at 02:26

 winni2078 08 23
<a href="http://www.juicycouture4u.com/">juicy couture </a>
http://www.juicycouture4u.com/
[url=http://www.juicycouture4u.com]Juicy Couture [/url]


79. louis vuitton shoes Aug 26, 2010 at 21:10

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


80. iPhone battery replacement Aug 26, 2010 at 22:22

Unfortunately this happens a lot. I my self have been a victim of abuse from TFL staff a number of times. I prefer to pay for my travelcards useing the automated machines so i won't have my oyster card and change thrown at me.


81. allen Aug 29, 2010 at 07:17

Wholesale cheap prom dresses
Wholesale Wedding Dresses
Wholesale Prom Dresses
Wholesale bridesmaid Dresses
Welcome to visit my website: http://www.sistersdress.com
--- Online wholesale shop offering discount designer prom dresses, cheap designer wedding dresses, cheap bridesmaid dresses,plus size wedding dresses, short prom dresse. You can choose any dress you like.


82. wholesale wedding dresses Aug 29, 2010 at 07:18

Beautiful dress! Welcome to visit my website: http://www.sistersdress.com
--- Online wholesale shop offering discount designer prom dresses, designer wedding dresses, cheap bridesmaid dresses,plus size wedding dresses, short prom dresse. You can custom any dresses you like.


83. Cheap Supra Shoes Aug 31, 2010 at 21:14

When I saw the title of this article, I was deeply attracted.What's more, the content of this article is more wonderful and hope to see more articles from this writer.


84. GHD Hair Straighteners Aug 31, 2010 at 21:15

Great artciles.


85. GHD Australia Aug 31, 2010 at 21:16

Perfect content and excellent topic


86. louis vuitton sunglasses Sep 01, 2010 at 21:31

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets

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
Give Back to Open Source