How to Recover a Mistakenly Deleted Branch

Workflow

git checkout -b _new_branch_name
# do some work and commit changed
git checkout master
git branch -d _new_branch_name
# doh... i meant to merge first

Fortunately, you can easily recover from this mistake.

git reflog
395b1ea HEAD@{0}: checkout: moving from _master_cleanup_akismet to _master_cleanup
bd7df04 HEAD@{1}: commit: spam handling using akismet for form submissions on contact_submission and applicant models
395b1ea HEAD@{2}: checkout: moving from _master_cleanup to _master_cleanup_akismet
395b1ea HEAD@{3}: commit: cleaning up and adding some basic features
a828ef3 HEAD@{4}: checkout: moving from master to _master_cleanup

Should show you a list of commits across all branches in desc chronological order.

To merge the branch you just deleted you can give merge the sha.

git merge bd7df04

Which would match the commit at HEAD@{1}

Git Feature Branch Naming Strategy

There are only two hard things in Computer Science: cache invalidation and naming things.
– Phil Karlton

Typically, we have three main branches at any given time in a project lifecycle.

master, development and staging.

Master is production ready code, development is actively being worked on and staging is pre-flight testing before deploying master.

Features are branched off of development and use underscores to indicate distance from originating branch. For instance

_development_users
__development_users_reset_password

indicates that __development_users_reset_password branched off of _development_users branch.

___development_users_reset_password_experiment

(3 underscores away) would indicate that I was experimenting with a branch off of development users reset password branch.

This provides a nice visual hierarchy when running git branch from the command line. It’s also helpful when you have multiple features being worked on by several people and you want to know where these branches are in relation to each other without needing to ask anyone. Here is a more complex example that illustrates the usefulness in using underscores in naming feature branches

  __development_people_importing_csv
* __development_people_importing_xml
  __development_users_password_reset
  _development_people_importing
  _development_users
  development
  master
  staging

Git Untrack Already Tracked Files

To remove files that are currently being tracked by git, you have to remove them from the “cache”. Note, doing this will NOT delete the file on your local machine. It will still be there but not be tracked.

 git rm -r --cached supersecretpasswords.txt 

You then need to add the file to the .gitignore file in the root of the project so that it isn’t tracked again on your next commit.


vim .gitignore
supersecretpasswords.txt

.gitignore files are tracked so remember to check in these changes.

git commit -am'my super secret passwords are safe!'

If you want to completely delete the file, on your local machine and from git

git rm supersecretpasswords.txt

If you’re working with a directory remember to add the -r flag for recursive removal!

Git: How to Delete a Branch with an Invalid Name

If you’ve named a branch beginning with two dashes “–”, you’re sort of in trouble because git interprets your branch name as a switch/flag. You can skip switches all together
by supplying two dashes before your branch name

git branch -d -- --index_for_suppliers

and your branch will be deleted!

How to Remove Your Last Git Commit

Remove your last commit (if you haven’t pushed yet)

git reset --hard HEAD~1

To see changes that have been committed and their position in HEAD

git reflog 

And to undo your previous reset and advance the cursor to the reference immediately behind the current state

git reset --hard HEAD@{1}

If you have already pushed you can

git revert HEAD

which will reverse your last commit by creating a new commit

Installing Ruby on Rails 3, MySQL, Git, Ruby Enterprise Edition, Passenger (Mod_Rails) on Ubuntu with Rackspace Cloud.

Short and sweet. Here all the commands I run in this order to set up a brand new box. It usually takes about 10 – 15 minutes on a 256 MB RAM instance. Compiling Ruby Enterprise Edition, which is super easy, will take the most amount of time. It will seem to have gotten stuck. It hasn’t. It just takes a little while.

# Update, upgrade and install all necessary packages for Ruby on Rails server if you've got a fresh Ubuntu slice
apt-get update
apt-get upgrade

apt-get install build-essential patch libssl-dev libreadline5-dev

apt-get install ruby1.8-dev ruby1.8 ri1.8 rdoc1.8 irb1.8 libreadline-ruby1.8 libruby1.8 libopenssl-ruby imagemagick librmagick-ruby1.8 librmagick-ruby-doc libfreetype6-dev xml-core postfix
# postfix will prompt you for details
# use Internet Site and enter in the domain name you are planning on sending email from 

apt-get install apache2 apache2-prefork-dev libapr1-dev libaprutil1-dev libcurl4-openssl-dev git-core mysql-server mysql-client libmysqlclient15-dev libmysql-ruby
# mysql will also prompt you to set up a root user account. set the password to be anything you like

# next, download the latest release of ruby enterprise edition but when you're installing it on your own machine version numbers and release dates may have changed.
# pay attention to the version and release date before the file extension. it will be something like
# ... 1.8.7-2010.02
# this will change to something like 2011.03, 2011.04... etc in the future.
# just double check the paths on when you are installing and make the necessary substitutions

# ruby enterprise edition is available at http://www.rubyenterpriseedition.com/download.html
wget http://rubyforge.org/frs/download.php/71096/ruby-enterprise-1.8.7-2010.02.tar.gz
tar xzvf ruby-enterprise-1.8.7-2010.02.tar.gz 

./ruby-enterprise-1.8.7-2010.02/installer
# this may take a little while (just follow the instructions)
# and hit enter to install in default location (recommended) when prompted 

# and to install passenger (which is mod_rails for apache)
/opt/ruby-enterprise-1.8.7-2010.02/bin/passenger-install-apache2-module 

# i take the output from the above script and add it to my available modules directory
vim /etc/apache2/mods-available/passenger.conf

# and enter something like this in the newly created file (your version numbers will prob. be different)
LoadModule passenger_module /opt/ruby-enterprise-1.8.7-2010.02/lib/ruby/gems/1.8/gems/passenger-3.0.2/ext/apache2/mod_passenger.so
PassengerRoot /opt/ruby-enterprise-1.8.7-2010.02/lib/ruby/gems/1.8/gems/passenger-3.0.2
PassengerRuby /opt/ruby-enterprise-1.8.7-2010.02/bin/ruby

# and then sym link it to the enabled directory so that apache knows about it
ln -s /etc/apache2/mods-available/passenger.conf /etc/apache2/mods-enabled/passenger.conf

# and now i want to include ruby enterprise edition in my path so i add it to my profile (again make sure the path is correct)
vim /etc/profile.d/passenger.sh
export PATH=/opt/ruby-enterprise-1.8.7-2010.02/bin:$PATH

. /etc/profile.d/passenger.sh
# the "." file will make the setting available for the current terminal session
rails -v
ruby -v
rake -v
# should all be working now
# and
which ruby
# should point to the ruby enterprise edition under /opt

# next i
# set up public/private keys
# so i can do
# ssh localhost without using a password
cd
test -e ~/.ssh/id_dsa.pub || ssh-keygen -t dsa
cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys2

# and finally install git
apt-get install git-core

You should now have a server ready to server ruby on rails applications!

Have Git Email Committers After Pushes

You need a Mail Transfer Agent MTA on the server. The easiest way is to install Sendmail, which Git uses by default.

apt-get install sendmail

Remember that /etc/hosts file needs the ip address to map to the domain name your sending mail from

# vim /etc/hosts
127.0.0.1     localhost localhost.localdomain
207.136.202.87    wwwexample.com
 

Sendmail has a tendency to hang when sending mail otherwise. To test sendmail

sendmail email@example.com
this is a test
how are you today world?
.

The period on a line by itself denotes end of message and will terminate the prompt and deliver the message.

Now you need to configure Git to send email after it receives a “push” from a committer. You can add email addresses, or you can set up a mailing list to email all members. Either way, you accomplish this with the following command, just remember to cd into the git repository.

git config --add hooks.mailinglist "mailinglist@example.com"

Next you need to activate the post-receive hook, located in the hooks directory of your repository.

cp post-receive.sample post-receive

And uncomment the last line, which uses sendmail to deliver the commit message

# uncomment the last line but keep the period "."
. /usr/share/doc/git-core/contrib/hooks/post-receive-email

All done. Now just make some changes to your source code, add and commit them and you should receive an email with all the details!

Very Basic Git Workflow

Very, very basic git workflow

git pull
git branch dev_branch
git checkout dev_branch
#make some changes
git checkout master
git merge dev_branch
git branch -d branch_to_delete
git push

Working with Branches in Git

Show all the branches

git branch

Create a new branch

git branch my_experimental_feature

Use that branch

git checkout my_experimental_feature

Pushing the new branch to a remote server

git push origin my_experimental_feature

Pulling that branch down on another machine

git pull origin my_experimental_feature

Listing all branches on other machine

git branch -a

Updating other machine

git pull origin my_experimental_feature
6 Sep 2009, 3:25pm
Git:
by

leave a comment

Non Standard Port Number with SSH and Git

Here is an example using the port 4567 to connect with over ssh and git

ssh remote add origin ssh://sean@seanbehan.com:4567/path/to/git
git push origin master