Thursday, June 26, 2008

Git: working with branches

The way we work with git is that for every remote pair programming session we create a separate branch. We give it a name after the feature we're working on.

git checkout -b feature1

It automatically switches to this branch.

During our work we tend to checkin our changes quite often. We do it with:

git checkin -a -m "created some specs for class A"

After we finish our session, we do two things.
First, we merge our branch to master:

git checkout master
git merge feature1

Then, we delete the branch we were working on:

git branch -D feature1

That's it.

If you happen to delete the branch BEFORE you merge it, don't panic, there is a solution.
In order to undelete a branch just after you deleted it, do:

git checkout -b new_branch_name HEAD@{1}


Marcin Domanski said...

Actually, there is one more thing you should be aware of - where does HEAD@{1} come from. Before you do the checkout, do

git reflog

You will see a list of all recent actions in your repository, including the last good commit, which you happened to delete. The list looks like this:

ba9a974... HEAD@{0}: commit: commit_message
391c84e... HEAD@{1}: commit: commit_message
0145b2f... HEAD@{2}: commit: commit_message

Now choose the one HEAD you would like to restore and do the checkout. You are now good to go and to do the merge...

Andrzej Krzywda said...

Thanks, Marcin!

Indeed, 'git reflog' is VERY useful in situations like this one.