When we think about git and git repos, we don’t often think about separating the
.git repo itself from the working directory. But we can actually have a lot of fun with bare repos. They give you a lot of flexibility, and when you’re doing things like deploying code or running builds, that’s useful.
Searching the web, it’s actually not super easy to find info on how to do this. I figured that writing up a post on it would be helpful both for me and for anyone who finds this.
Cloning a repo bare is easy enough. When you run
git clone, you simply include the
--bare flag. It’ll create a directory that is identical to the
.git directory inside your normal old git checkout. The convention is to name this directory
<whatever>.git, but that’s optional. The only difference between this checkout and your normal repo’s
.git directory is that the
config file will have
bare = true. So to wrap up, your whole
clone command will look like this:
git clone --bare firstname.lastname@example.org:<org|user>/<repo-name>.git <repo-name>.git.
Now, because you have a bare repo, a few things are probably different from the repos that you’re accustomed to working with:
- There’s no ‘working directory’
- Nothing is ‘checked out’
- You aren’t ‘on’ a branch
The cool thing is that using a bare repo actually lets you work with a few working directories, if you want. Each working directory will be free of a
.git directory, so they’ll be smaller and not contain the entire history of your project.
Updating a Bare Repo
To update your repo, you’re going to use a
fetch command, but you’re going to specify an environment variable beforehand. You’ll want to point
GIT_DIR to your bare checkout:
GIT_DIR=~/my_repo.git git fetch origin master:master
master:master at the end of the command is telling
git to get the changes from your
master branch and update your local
master branch to match. If you want to update some other branch or from some other remote, you can adjust your command accordingly. If you’re looking to update all the branches in your repo, change out the
master:master and put use
Checking Out from a Bare Repo
Checking out from your bare repo is going to be almost identical to checking out anything in a normal repo, but you’ll need two environment variables specified:
GIT_WORKING_DIR. Your command will look a lot like this:
git checkout -f origin master
-f will discard any changes that have been made in the working directory. In most cases where you’ll be using this, that’s preferable to a failure just because something has changed in the directory.
This command will be the same whether you’re checking it out for the first time or updating it to the latest.
Hopefully that helps you (and me)! If you’ve got any questions or comments, of if I’ve made any errors, let me know in the comments below!
Leave a Reply