Connecting Your Ruby Application to Git

In order to initiate your Ruby application under source control such as git on github you need to start with the initialization of your local git repository inside your Ruby on Rails application. You use git in order to collaborate with other developers as well as being able to track and roll back your project to any point in the past. In order to initialize your local git repository, you need to type

>  git init

This command will produce “Initialzied empty Git repository in /folder/.git/” message. We need to add our project files into this local git repository. Note that we are adding files that are located inside physical folder. It is the same folder where we initialized out git repository. We simply adding files of our project to this repository.

> git add .

The above listed command has a dot right after add which signifies adding of all the files, subfolders that we have in our parent project directory. Next we need to commit our files which freezes our files in a given time.

> git commit -m “This is our project baseline commit”

You can notice that we have –m variable which simply helps us to set a commit message.

Our next logical step is to have online git repository set up and our files  to be pushed to this repository. Please register an account with the site called github.com. Go ahead and create a new repository on github.com after you create an account. You will need to provide a repository name, description and select if you want private or public. You will be provided with the next step guide upon creation of this repository. However, we just need to issue the following command in order to add our local git repository to the remote on github.com.

> git remote add origin https://github.com/yourname/yourproject.git

We have set up origin that references https://github.com/yourname/yourproject.git project. Next step after setting origin to our remote git repository as need to issue git push command.

> git push –u origin master

The github will prompt us with two requests for username and password. We’ll get success message after we push all files to a remote git repository. You will also see a message “Baranch master set up to rack remote branch master from origin”. This message is an indication of successful git repository link set up. You will also see that all your local files have been transferred over from your local computer to remote git repository.

Note that you may run into problems with adding project to a github in case there are files already committed to the github repository. The following error message may occur.

hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

This error message is an indication that you need to synch files first from git hub to your local git repository. However, you may run into another problem if you try to run git pull command. There can be an error stating the following error message

There is no tracking information for the current branch.
Please specify which branch you want to merge with.

In order to mitigate this error you need to set up upstream from your local to remote git repositories via the following command.

> git branch --set-upstream master origin/master

OR

> git branch --set-upstream-to=origin/master master

This will resolve your tracking information missing error message and it will allow you to issue git pull command  in order to bring data from the remote repository in your local git repository.

> git pull

The git pull command is success and your repository is ready to be pushed form local to remote. Successful pull will be followed by the “Merge made by the 'recursive' strategy.” message. You can finally re-try

> git push –u origin master

This command will  return “Branch master set up to track remote branch master from origin.” Message if successful.

Finally, you can use git log command in order to see recent changes committed to the remote branch.

> git log

Adding a new user to your project is also simply. You need to navigate to admin panel and select collaborator menu option on the left. Then provide user email address and click add.

In order to add another project to your local development space, you need to clone it from existing remote repository on the github.com. The following command line does it for you.

> git clone git@github.com:developername/project.git

If success, you will see two message: Receiving object: 100% and Resolving deltas: 100%.

The very last step in our process of setting up Ruby on Rails app with Git remote repository is to issue bundle command line in order to resolve and install all of the dependencies as specified by the project we just cloned from remote git to our local git repository.

> bundle

 

Learn how to to branch in Git

Featured pages

Ruby

Set of Ruby Object Oriented Programming Language tutorials that cover such topics as Ruby strings, …

Rails

Rails Framework tutorial teaches you how to utilize de facto framework of choice for Ruby developme…

Ruby Duck Typing

“If an object quacks like a duck just go ahead and treat it as a duck” – this fun…

Regular Expressions

Ruby uses the =~ operator to check any given string against regular expression. For example, a stri…

Credit Card Number

Every last digit of any credit card is a check sum digit that is determined by all digits in front …

Ruby Arrays

Ruby Programming Language has built in support for Arrays. Arrays help you define some of the compl…

Ruby Hashes

Hashes are very similar to arrays in Ruby and hashes interface is similar to Ruby array interface. …

Ruby Code Block

Ruby is very unique language when it comes to code blocks. You can simply pass a code block to a me…