Using the git gem to push a file to your GitHub project repository

Here are typical commands I execute from the command-line to push a file to a project repository on GitHub:

Here's how I would do the same thing using the git gem:

require 'git'

project = 'activity-logger'
g = Git.init('/home/james/gem_src/' + project)
#=> #<Git::Base:0xb9a288 @logger=nil, @working_directory=#<Git::WorkingDirectory:0xb9a1b0 @path="/home/james/gem_src/activity-logger">, @repository=#<Git::Repository:0xb9a180 @path="/home/james/gem_src/activity-logger/.git">, @index=#<Git::Index:0xb9a108 @path="/home/james/gem_src/activity-logger/.git/index">> 
2.0.0p0 :005 > 

g.config('user.name', 'James Robertson')
g.config('user.email', 'james@r0bertson.co.uk')

g.add('/home/james/gem_src/' + project + '/MIT-LICENSE')
message = 'added the MIT-LICENSE file'
g.commit(message)
#=> "[master da861d8] updated the gemspec to include the MIT license, homepage and email attributes\n 1 file changed, 18 insertions(+)\n create mode 100644 MIT-LICENSE"

g.push
#=> nil #observed the MIT-LICENSE file on my GitHub repository

Resources

Tags:
Source:
2040hrs.txt
Published:
27-07-2013 20:40