1 <?xml version=
"1.0" encoding=
"iso-8859-1"?>
3 PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6 <html xmlns=
"http://www.w3.org/1999/xhtml" xml:
lang=
"en" lang=
"en">
8 <title>File: EXAMPLES
</title>
9 <meta http-equiv=
"Content-Type" content=
"text/html; charset=iso-8859-1" />
10 <meta http-equiv=
"Content-Script-Type" content=
"text/javascript" />
11 <link rel=
"stylesheet" href=
".././rdoc-style.css" type=
"text/css" media=
"screen" />
12 <script type=
"text/javascript">
15 function popupCode( url ) {
16 window.open(url,
"Code",
"resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
19 function toggleCode( id ) {
20 if ( document.getElementById )
21 elem = document.getElementById( id );
22 else if ( document.all )
23 elem = eval(
"document.all." + id );
27 elemStyle = elem.style;
29 if ( elemStyle.display !=
"block" ) {
30 elemStyle.display =
"block"
32 elemStyle.display =
"none"
38 // Make codeblocks hidden by default
39 document.writeln(
"<style type=\"text/css\
">div.method-source-code { display: none }</style>" )
51 <table class=
"header-table">
52 <tr class=
"top-aligned-row">
53 <td><strong>Path:
</strong></td>
57 <tr class=
"top-aligned-row">
58 <td><strong>Last Update:
</strong></td>
59 <td>Mon Nov
12 10:
27:
43 PST
2007</td>
63 <!-- banner header -->
65 <div id=
"bodyContent">
69 <div id=
"contextContent">
71 <div id=
"description">
73 Here are a bunch of examples of how to use the Ruby/
<a
74 href=
"../classes/Git.html">Git
</a> package.
77 First you have to remember to require rubygems if it
’s not. Then
78 include the
‘git
’ gem.
85 Here are the operations that need read permission only.
88 g = Git.open (working_dir = '.')
97 g.log # returns array of Git::Commit objects
98 g.log.since('
2 weeks ago')
99 g.log.between('v2.5', 'v2.6')
100 g.log.each {|l| puts l.sha }
101 g.gblob('v2.5:Makefile').log.since('
2 weeks ago')
103 g.object('HEAD^').to_s # git show / git rev-parse
104 g.object('HEAD^').contents
105 g.object('v2.5:Makefile').size
106 g.object('v2.5:Makefile').sha
112 g.revparse('v2.5:Makefile')
114 g.branches # returns Git::Branch objects
117 g.branches[:master].gcommit
118 g.branches['origin/master'].gcommit
120 g.grep('hello') # implies HEAD
121 g.blob('v2.5:Makefile').grep('hello')
122 g.tag('v2.5').grep('hello', 'docs/')
124 g.diff(commit1, commit2).size
125 g.diff(commit1, commit2).stats
126 g.gtree('v2.5').diff('v2.6').insertions
127 g.diff('gitsearch1', 'v2.5').path('lib/')
128 g.diff('gitsearch1', @git.gtree('v2.5'))
129 g.diff('gitsearch1', 'v2.5').path('docs/').patch
130 g.gtree('v2.5').diff('v2.6').patch
132 g.gtree('v2.5').diff('v2.6').each do |file_diff|
135 puts file_diff.blob(:src).contents
138 g.config('user.name') # returns 'Scott Chacon'
139 g.config # returns whole config hash
141 g.tag # returns array of Git::Tag objects
144 And here are the operations that will need to write to your git repository.
149 Git.init('/home/schacon/proj',
150 { :git_dir =
> '/opt/git/proj.git',
151 :index_file =
> '/tmp/index'} )
153 g = Git.clone(URI, :name =
> 'name', :path =
> '/tmp/checkout'
154 (git_dir, index_file)
156 g.config('user.name', 'Scott Chacon')
157 g.config('user.email', 'email@email.com')
160 g.add([file1, file2])
163 g.remove(['file.txt', 'file2.txt'])
166 g.commit_all('message')
168 g = Git.clone(repo, 'myrepo')
169 Dir.chdir('myrepo') do
170 new_file('test-file', 'blahblahblah')
171 g.status.changed.each do |file|
172 puts file.blob(:index).contents
176 g.reset # defaults to HEAD
177 g.reset_hard(Git::Commit)
179 g.branch('new_branch') # creates new or fetches existing
180 g.branch('new_branch').checkout
181 g.branch('new_branch').delete
182 g.branch('existing_branch').checkout
184 g.checkout('new_branch')
185 g.checkout(g.branch('new_branch'))
187 g.branch(name).merge(branch2)
188 g.branch(branch2).merge # merges HEAD with branch2
190 g.branch(name).in_branch(message) { # add files } # auto-commits
191 g.merge('new_branch')
192 g.merge('origin/remote_branch')
193 g.merge(b.branch('master'))
194 g.merge([branch1, branch2])
196 r = g.add_remote(name, uri) # Git::Remote
197 r = g.add_remote(name, Git::Base) # Git::Remote
199 g.remotes # array of Git::Remotes
201 g.remote(name).remove
203 g.remote(name).merge(branch)
206 g.fetch(g.remotes.first)
209 g.pull(Git::Repo, Git::Branch) # fetch and a merge
211 g.add_tag('tag_name') # returns Git::Tag
236 <!-- if method_list -->
242 <div id=
"validator-badges">
243 <p><small><a href=
"http://validator.w3.org/check/referer">[Validate]
</a></small></p>