This is a quick note, largely aimed at myself, about using git in Rails development and assumes git is already installed on your computer.
When you start a new project (e.g. rails new), once you have got the framework largely configured, navigate to the application root directory and do the following:
- Create an empty git repository by typing the following in the terminal
- ensure all the files you dont necessarily want to keep track of (e..g log files) are inlcluded in .gitignore in your application root
- add all the files (except form those we want to ignore) to the repository
git add .
note: the "." indicates the current directory and git recursively iterates through all subdirectories.
- if you now enter:
you will see a list of pending git changes
- finally enter:
git commit -m "Initialize repository"
to commit your changes to the repository and add a message using the "-m" flag, in this case "Initialize repository"
You can see a list of your commits and their messages by entering:
Note that at this stage we've only made a local repository on the computer on which you are developing.
To store a repository on github we need to take a few more steps.
- On the github website, create a new repository (e.g. app1)
- In the terminal enter the following. This tells you local git that you want to use Github as your main (
git remote add origin https://github.com/<username>/first_app.git
- push your repository up to Github
git push -u origin master
note: the "-u' flag is something to do with setting the branches which I don't fully understand yet.
If you're like me, you'll be asked for your Github username and password in the terminal and then Git will push your local git up Github. You can go to the Github site and see it all there.
The README file that is displayed on Github at this stage is from the Readme.rdoc file which was automatically created when you setup your new rails project. This document can be modified to better explain your project.
At this stage, with a git commit just having been completed, all our files are part of our master branch. If we want to modify the contents of a file, (e.g. Readme.rdoc) we want to check it out into a new sub-branch (e.g. explanation) so that we aren't modifying the original file and aren't changing the master branch until we are ready to do so. to do this we enter the following:
git checkout -b modify-README
This makes a 'copy' of the Readme file and should have given you a message like "Switched to a new branch 'modify-README'
You can check which branch you are on by entering:
git branch, or
The '*' on git branch tells you which is the current branch
If you now edit the Readme file and save it, when you run "git status" you will see an updated of your changes (e.g. the Readme file changed) .
To commit these changes you enter:
- git commit -a -m "New README file"
We've seen the '-m' before, the '-a' means add all files which were created using the unix 'mv' command to make them (e.g. you may have decided to change your readme.rdoc to a markdown version by entering mv readme.rdoc readme.md.) If you have added new files (e.g. not used mv to make copies), you need to use git add . first.
To add the changes back to the master branch we need to switch back to the master branch (remember we are currently in the modify-README branch) and merge the changes back into it.
git checkout master
git merge modify-README
Now that the changes are merged back into the master branch, you can delete the sub-branch by typing:
- git branch -d modify-README
To learn more about git, read http://git-scm.com/book