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.
Stage and Commit the project locally
- Open up Sourcetree.
- 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.
- Press the Create button (with a Plus (+) sign icon on it).
- Uncheck “Create Repository on Account” since we are going to use a “local” remote.
- 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).
- Click the blue Create button.
- 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).
- The “Working Copy” of your repo will appear. If it doesn’t, select Working Copy from the left sidebar menu.
- 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.
- 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.
- While still in Sourcetree with the Working Copy of MyProject still showing, click the Terminal button near the top-right of the window.
- 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/”.
- 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
- In Sourcetree, select the Repository menu from the top menu and select the “Add Remote…” item.
- In the popup, with the Remotes tab active, click the Add button
- For Remote name: check the Default Remote.
- For Url/Path, type
(or the one that you used above)
- Ignore the Optional Extended Integration section
- Click OK, then click OK again.
- 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.
- Click the Push button at the bottom.
- 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)
- It should finish with no errors: no news is good news!
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.
You must be logged in to post a comment.