Restoring a deleted file in GIT
Just because l always forget the exact syntax for restoring a deleted file from git
git checkout <deleting_commit>^ -- <file_path>
Git branching after committing
Let’s say you make some changes on your master branch and commit a few times. So when you do a good old ‘git status’ you may have something like this
kongy@Deadpool:~/Development/Sentia/project (master) $ git status # On branch master # Your branch is ahead of 'origin/master' by 3 commits.
However you now realize you have taken the red pill. You are in the middle of a monster feature, and realise that you should have branched before. Luckily we can fix this using some git magic.
“Why, oh why, didn’t I take the blue pill?”
Let’s go back 3 commits by checking it out.
kongy@Deadpool:~/Development/Sentia/project (master) $ git checkout HEAD~3 Note: moving to 'HEAD~3' which isn't a local branch If you want to create a new branch from this checkout, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b <new_branch_name> HEAD is now at 978b493... Merge branch 'master' of github.com:Sentia/project
Nice! Now our repository is sitting pretty at 3 commits previously. And even better, git also tell us how to create a branch from this point. So let’s do that now push the newly created branch up to Github.
kongy@Deadpool:~/Development/Sentia/project ((no branch)) $ git checkout -b magic Switched to a new branch 'tiling' kongy@Deadpool:~/Development/Sentia/project (magic) $ git push origin magic Total 0 (delta 0), reused 0 (delta 0) To git@github.com:Sentia/project.git * [new branch] magic -> magic
Now that we have created and saved the branch, lets grab all those changes in master, merge them into our branch, and push up to Github
kongy@Deadpool:~/Development/Sentia/project (magic) $ git merge master Updating 978b493..8a74bd3 Fast forward project.rb | 443 ++++++++++-----------------------
Cool, so our branch is now up to date. Now all we have to do is fix our master branch to become the same as the the GitHub origin master branch. I think there is probably an easier way to do this but I went and got the SHA commit key for the HEAD of origin to know where I wanted to go back to.
kongy@Deadpool:~/Development/Sentia/project (magic) $ git checkout master Switched to branch 'master' Your branch is ahead of 'origin/master' by 3 commits. kongy@Deadpool:~/Development/Sentia/project (master) $ git reset --hard 978b493631a1443b1c84 HEAD is now at 978b493 Merge branch 'master' of github.com:Sentia/project
And you’re done. Now you can work in peace on your branch without disturbing your fellow developers. And then cry when you need to merge their changes into your branch.
Setting up a new Rails app with Git
Ok so you want to be cool like all other rails developers out there and use Git for your projects. Here is a quick run down of how you might go about it.
Firstly create your rails app and using terminal go into the root of that project.
Then create a top, project-level .gitignore file. I use textmate so this can be done like so
$ mate .gitignore
Then add the following to the .gitignore file
.DS_Store log/*.log tmp/**/* config/database.yml db/*.sqlite3
You can also add more directories or files in here to ignore like css files if using sass for example, or your uploads directory.
Create some .gitignore files so the empty directories get tracked:
$ touch log/.gitignore $ touch tmp/.gitignore
and finally commit that bad boy
$ git add . $ git commit -m "First commit"
Running git add will tell git to track all the new files (Since first commit thats all of them). The commit will commit to your local Git repository and all that is left to do would be to add it to GitHub for example and your in business.
Cool colours and GitHub branch in Terminal
There are many posts out there about making terminal have nice colours and getting it to display your projects GitHub branch. So l thought l would post mine
Here is what my terminal looks like.

Yes l am using Ryan Bates railscasts as my sample project (Thanks Ryan). As you can see it has the username@machine and then if you are in a project running git is also has the branch.
All you need to do it add the following to either your bash_login or bash_profile depending on what you are using. So first you run
mate ~/.bash_login
I am using textmate to edit this file. Once there simple add the following
export CLICOLOR=1
export TERM=xterm-color
export LSCOLORS=gxfxcxdxbxegedabagacad # cyan directories
export PS1="\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ \`ruby -e \"print (%x{git branch 2> /dev/null}.grep(/^\*/).first || '').gsub(/^\* (.+)$/, '(\1) ')\"\`\[\033[37m\]$\[\033[00m\] "
Save the file and reload your bash like so and you are good to go enjoy.
. ~/.bash_login
Of course you can change the colour of the font but ill leave that to you
Application templates in Ruby on Rails
Here is the latest application template we have put together. It removes all the usual items and adds the base gems that we use for all our applications. It also creates the files needed for deployment and runs “Capify” on the project, plus a few other things such as sass etc.
# Remove unnecessary Rails files run 'rm README' run 'rm public/index.html' run 'rm public/favicon.ico' run 'rm public/images/rails.png' run 'rm -f public/javascripts/*' # Download JQuery run "curl -s -L http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js > public/javascripts/jquery.js" run "curl -s -L http://jqueryjs.googlecode.com/svn/trunk/plugins/form/jquery.form.js > public/javascripts/jquery.form.js" #Configure required gems gem "haml", :version => "2.0.4" gem 'thoughtbot-shoulda', :lib => 'shoulda', :source => 'http://gems.github.com' gem "thoughtbot-factory_girl", :lib => "factory_girl", :source => "http://gems.github.com" #Create Sass directory run 'mkdir public/stylesheets/sass' #Capify and create production environment.rb run 'mkdir config/deploy' run 'touch config/deploy/production.rb' #Add UAT environment and settings file 'config/environments/uat.rb', <<-CODE # Settings specified here will take precedence over those in config/environment.rb # Code is not reloaded between requests. Server needs to be restarted. config.cache_classes = true # Log error messages when you accidentally call methods on nil. config.whiny_nils = true # Show full error reports and disable caching config.action_controller.consider_all_requests_local = false config.action_controller.perform_caching = true config.action_view.cache_template_loading = true # Don't care if the mailer can't send config.action_mailer.raise_delivery_errors = false CODE #Create gitignore file and setup base ignores run "touch tmp/.gitignore log/.gitignore vendor/.gitignore" file '.gitignore', <<-FILE .DS_Store log/*.log tmp/**/* db/*.sqlite3 public/stylesheets/*.css FILE # Set up git repository git :init git :add => '.' git :commit => "-a -m 'Initial commit'" # Success! puts "SUCCESS!"




