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