changed .commit to .gcommit for consistency
[rubygit.git] / EXAMPLES
blob9d05965bfd2bbef2ca3c8d287a4877f934903dd9
1 require 'git'
3 # needs read permission only
5 g = Git.open (working_dir = '.')
6         (git_dir, index_file)
8 g.index
9 g.index.readable?
10 g.index.writable?
11 g.repo
12 g.dir
14 g.log   # returns array of Git::Commit objects
15 g.log.since('2 weeks ago')
16 g.log.between('v2.5', 'v2.6')
17 g.log.each {|l| puts l.sha }
18 g.gblob('v2.5:Makefile').log.since('2 weeks ago')
20 g.object('HEAD^').to_s  # git show / git rev-parse
21 g.object('HEAD^').contents
22 g.object('v2.5:Makefile').size
23 g.object('v2.5:Makefile').sha
25 g.gtree(treeish)
26 g.gblob(treeish)
27 g.gcommit(treeish)
29 g.revparse('v2.5:Makefile')
31 g.branches # returns Git::Branch objects
32 g.branches.local
33 g.branches.remote
34 g.branches[:master].gcommit
35 g.branches['origin/master'].gcommit
37 g.grep('hello')  # implies HEAD
38 g.blob('v2.5:Makefile').grep('hello')
39 g.tag('v2.5').grep('hello', 'docs/')
41 g.diff(commit1, commit2).size
42 g.diff(commit1, commit2).stats
43 g.gtree('v2.5').diff('v2.6').insertions
44 g.diff('gitsearch1', 'v2.5').path('lib/')
45 g.diff('gitsearch1', @git.gtree('v2.5'))
46 g.diff('gitsearch1', 'v2.5').path('docs/').patch
47 g.gtree('v2.5').diff('v2.6').patch
49 g.gtree('v2.5').diff('v2.6').each do |file_diff|
50   puts file_diff.path
51   puts file_diff.patch
52   puts file_diff.blob(:src).contents
53 end
55 g.config('user.name')  # returns 'Scott Chacon'
56 g.config # returns whole config hash
58 ***** IMPLEMENTED *****
60 g.tag # returns array of Git::Tag objects
65 # needs write permission
68 g = Git.init
69   Git.init('project')
70   Git.init('/home/schacon/proj', 
71                 { :git_dir => '/opt/git/proj.git', 
72                   :index_file => '/tmp/index'} )
73         
74 g = Git.clone(URI, :name => 'name', :path => '/tmp/checkout'
75         (git_dir, index_file)
77 g.config('user.name', 'Scott Chacon')
78 g.config('user.email', 'email@email.com')      
80 g.add('.')
81 g.add([file1, file2])
83 g.remove('file.txt')
84 g.remove(['file.txt', 'file2.txt'])
85                   
86 g.commit('message')
87 g.commit_all('message')
89 g = Git.clone(repo, 'myrepo')
90 Dir.chdir('myrepo') do
91  new_file('test-file', 'blahblahblah')
92  g.status.untracked.each do |file|
93   puts file.blob(:index).contents
94  end
95 end
97 g.reset # defaults to HEAD
98 g.reset_hard(Git::Commit)
101 ***** IMPLEMENTED *****
103 g.branch('new_branch')
104 g.branch('new_branch').delete
106 g.checkout('new_branch')
107 g.checkout('new_branch', :create_branch => true)
108 g.checkout_b('new_branch')
110 g.merge('new_branch')
111 g.merge(Git::Branch)
112 g.merge(Git::Branch, Git::Branch)
114 g.fetch
115 g.fetch(Git::Repo)
117 g.pull
118 g.pull(Git::Repo, Git::Branch) # fetch and a merge
120 g.tag('tag_name') # returns Git::Tag
122 g.pack