Categories
Tutorial

Keeping Your Git Repo Local

Introduction

Github is a good place to keep your remote Git repositories, especially if you want to share them.  However, if you want to keep it private, it costs extra.  Also, Github has a limit on the sizes of files it can store.  You can partly get around this with Git LFS, though that has some convenience costs and the free version has limits.  An alternative would be to have an external hard drive, perhaps one connected to your network, and keep your “remote” Git repo local.

My Setup: An Example Case

Personally, I have an external drive connected through the network to my Windows 10 PC, mapped as my Z: drive, and I use it as a backup drive.  It is also a good place to store remote Git repos.

I also use Atlassian’s Sourcetree for my Git repo management.  This is a GUI front-end to Git that makes working with Git convenient.  It also allows you to use an internal Git system so you do not need to install Git separately.  This system comes with Git Bash, a convenient BASH shell that is useful as a command shell separately from using Git.

Let us suppose I have a project called MyProject that already has some files in it, and I want to maintain a separate Git repo.  It is located in the directory D:\Documents\Reps\MyProject.  You can follow along with your own project if you change the paths and names appropriately.

Keeping your Git repo local with Sourcetree and Git Bash

Stage and Commit the project locally

  1. Open up Sourcetree.
  2. We first want to turn the project into a Git repo.  To do this, select from the top menu, the File menu, then select the item Clone/New.
  3. Press the Create button (with a Plus (+) sign icon on it).
  4. Uncheck “Create Repository on Account” since we are going to use a “local” remote.
  5. Either use the Browse button or just type, to set the repo path to D:\Documents\Repos\MyProject (or whatever it is for your situation).
  6. Click the blue Create button.
  7. You get a popup asking if you want to create the Git repo in a directory that already exists.  Yes, do this.  It will not overwrite your information (unless you have a directory or file called “.git” in the directory already).
  8. The “Working Copy” of your repo will appear.  If it doesn’t, select Working Copy from the left sidebar menu.
  9. Now, click the Stage All button, somewhere near the middle of the window, at the top-right of the Ustaged Files panel.  (You will only be able to do this if there are files in your project.   If not, you can add a test file just to try out these steps.)  The file(s) will move to the upper Staged Files panel.
  10. Type in some commit message in the commit box at the bottom (such as, “Initial Commit”).  Then press the Commit button at the bottom-left of the window.

Your project has now been committed, but only locally, that is within the newly-created .git directory in your project directory.

Create the Remote on your Disk Drive

Now, we get to the real point of this post: creating the remote repository on your disk drive.  My setup is that the external networked hard drive is the Z drive, and I want to put the remote in a file called Z:\Repos\MyProject.  As before, change this according to your actual situation.

  1. While still in Sourcetree with the Working Copy of MyProject still showing, click the Terminal button near the top-right of the window.
  2. In the Git Bash terminal window that appears (which should automatically have a working directory of /d/Documents/Repos/MyProject, or whatever your project working directory is), type the following to create the new repo (changing path and project name to what you need it to be):git init --bare /z/repos/MyProject

    Note, git bash uses /z/ instead of Z:\ for the root of the Z drive, and the directory slashes go forward instead of backward.  This can be fast or slow depending on how fast your drive is.  If all goes well, you will see a response like “Initialized empty Git repository in Z:/repos/MyProject/”.

  3. While still in the Git Bash terminal window, type the following to clone your project to that new repo you created (while you are still in the same working directory):git clone --bare /z/repos/MyProject

    If all goes well, you will see a message like “Cloning into bare repository ‘MyProject.git’…”.  It is ok if you get a message like “warning: You appear to have cloned an empty repository.”, because we still need to “push” the last commit to this remote repo.

Connect and Push the Local to the Remote

  1. In Sourcetree, select the Repository menu from the top menu and select the “Add Remote…” item.
  2. In the popup, with the Remotes tab active, click the Add button
  3. For Remote name: check the Default Remote.
  4. For Url/Path, type/z/repos/MyProject

    (or the one that you used above)

  5. Ignore the Optional Extended Integration section
  6. Click OK, then click OK again.
  7. As a test, click Push to push your last commit (make a new commit first if you need to). Check the “Push?” checkbox, and leave local and remote branches as Master, and leave “Track?” checked.
  8. Click the Push button at the bottom.
  9. Depending on amount to push and speed of the disk drive you push to, it might take a while. (My Z drive is slow, being an external networked drive)
  10. It should finish with no errors: no news is good news!

Conclusion

You now know how to keep a git repo local.  I considered showing how to set up a Gitlab instance in a virtual machine, but that was quite involved, and there are some difficult issues with using networked drives and Gitlab.