Setting the default git branch in a bare repository

A bare repository in git does not include a checkout. So in order to set the default branch that users will get after they clone, you cannot use git-checkout mybranch.

Instead, if you want the default branch to be something other than master, you need to do this:

git symbolic-ref HEAD refs/heads/mybranch

Which will update the HEAD file in your repository so that it contains:

ref: refs/heads/mybranch

This is well documented in the git-symbolic-ref manpage but it's not necessarily the first place you'd think of looking.

Setting up a centralized git repository

A lot of people want to setup a shared repository to allow a team of developers to publish their code on a server. This centralized approach is the model behind CVS/Subversion-style, but is also one of the many workflows supported by git.

Setting up such a repository in git involves a few simple steps. For simplicity's sake, I will assume that:

  • you have sorted out the UNIX permissions issues (e.g. putting all developers in a common group and adjusting permissions on the appropriate directories)
  • and that the repository is located on the same machine (replace /tmp/myrepo.git by ssh://hostname/repo.git if you need to access a different machine).

First of all, you need to create the bare repository where your users will push their changes and pull in updates:

mkdir /tmp/myrepo.git  
cd /tmp/myrepo.git  
git --bare init --shared

A bare repository in git is one that doesn't have an associated code checkout. It's essentially like the .git subdirectory of a normal repository.

Then you need to create a normal repository from which you will push to the central one you just created:

mkdir /tmp/myrepo1  
cd /tmp/myrepo1  
git init

You can't push yet because this local repository is completely empty (hence there is nothing to push). So let's create an initial commit:

touch test  
git add test  
git commit

Now, you can push your commit to the central repository:

git push /tmp/myrepo.git master

Other developers can clone the shared code using the usual command:

cd /tmp  
git clone /tmp/myrepo.git myrepo2

If you want to be able to track the central repository (i.e. use "git pull" and "git push" directly), you will need your local master branch to track the remote branch.

To do this, you can either throw away your local repo and clone again or simply run the following:

git remote add origin /tmp/myrepo.git  
git config branch.master.remote origin  
git config branch.master.merge refs/heads/master

That's it. If you want a longer explanation however, have a look at Aaron Toponce's tutorial or see the Debian Alioth instructions.

Recovering lost git commits

Say you just deleted a branch or did a git reset --hard <earlier commit> and in the process lost a commit which you intended to keep.

Well, the good news is that unless you have run git gc to run a pass of garbage collection over your repo and get rid of objects which aren't linked to anymore, the objects for your lost commits should still be in the repo.

If you remember the SHA-1 IDs then you can simply use git cherry-pick to apply them to the current branch. However, if you don't have these commit IDs anymore, there is still a way to get to them.

This command will display all commits from the most recent to the oldest one:

git log -g