Creating Local Git Repositories with Sourcetree

Storing a repository locally on your hard drive sort of defeats the purpose behind Git’s design…it’s not on a remote server with timely backups, and you’re basically the only one who can access and maintain it.

That said, it is a viable solution if you happen to like using Git and you have the following reasons for doing it:

  • You’re using Windows
  • You want to version control your own code
  • You want to keep your code in your hands and not some server several hundred miles away.
  • You don’t have a convenient local area network computer to store the repository.

Surprisingly, it isn’t as difficult as most solutions make it out to be.  In fact, you do not need to run your own Git server.  All you need is a git client.  Then, using the client, you create two repositories instead of one.  And then have one repository point to the other as the remote.

You have to deal with one problem however, pushing will probably throw an error.  Git will refuse to push because the remote has a checked out branch.

http://stackoverflow.com/questions/11117823/git-push-error-refusing-to-update-checked-out-branch

There are three different solutions:

  1. Create a bare repository
  2. Create and check out a dummy branch on the remote repository
  3. Edit the configuration files to ignore this check

I’m going to cover two of them since the third one strikes me as interesting, but bad practice.  The first solution cannot be done using Sourcetree alone.  Creating a bare repository:

https://answers.atlassian.com/questions/172509/how-do-i-create-clone-a-bare-repository-in-sourcetree

What is a bare repository?

Bare repository is the one that has no working tree. It means its whole contents is what you have in.git directory.

You can only commit to bare repository by pushing to it from your local clone. It has no working tree, so it has no files modified, no changes.

To have central repository the only way it is to have a bare repository.

I included the above quote to basically say that a bare .git repository is just going to have a .git folder with stuff inside.  Nothing else will be in that directory, so if you do have source code in that directory, do not be afraid if they are not being updated whenever you push.

In Git you should only use a “bare” repository to clone and pull from, and push to. It doesn’t have a checked out tree, so it just does what the “server” notionally does in a centralized VCS – records commits, branches, etc when you push to it, and gives you the latest versions when you clone or pull from it.

I suppose I should also say that I’m throwing all the above links because I saw a stack overflow comment about how online repositories like GitHub are basically bare .git repositories, but I lost the link. Or maybe I didn’t…

There are two types of repositories: bare and non-bare

Bare repositories do not have a working copy and you can push to them. Those are the types of repositories you get in Github! If you want to create a bare repository, you can use

So, in short, you can’t push to a non-bare repository (Edit: Well, you can’t push to the currently checked out branch of a repository. With a bare repository, you can push to any branch since none are checked out. Although possible, pushing to non-bare repositories is not common). What you can do, is to fetch and merge from the other repository. This is how the pull request that you can see in Github works. You ask them to pull from you, and you don’t force-push into them.

But, as said before, you cannot create a remote repository using sourcetree alone.  Converting a remote repository to a bare repository can be done using the following method:

You can simply convert your remote repository to bare repository (there is no working copy in the bare repository – the folder contains only the actual repository data).

Execute the following command in your remote repository folder:

Then delete all the files except .git in that folder. And then you will be able to perform git push to the remote repository without any errors.

As said twice before, you can’t use Sourcetree to do the above.  However, you can download and use Git from the command line to pull it off:

http://git-scm.com

Which leads to our second solution…how can you push to the local (remote) repository using sourcetree?

The answer is to go to the second repository and create a dummy branch.  Then check out the dummy branch.  Now you can use the first repository to push to the second repository.

Remind me that I need to add how-to create the repositories later.

 

Leave a Reply

Your email address will not be published. Required fields are marked *