Distributed version control systems (VCS’s), like Git, provide a rich set of features for managing source code. Many development tools, including XCode, provide built-in support for various VCS’s. These tools provide simple configuration with limited customization to get you up and running quickly while still providing the safety net of basic version control.
I hate losing (and re-doing) work. I have OCD when it comes to saving and versioning source code. Save early, save often, and commit to the VCS often. I also hate merging code. Smaller and more frequent commits enable me to minimize merge time and effort as well.
The work flow I prefer even for personal exploratory projects is:
- Make small local changes to the codebase to create an incrementally improved (and working) system.
- Commit these changes to the local repository. Local repositories are quick to access, function even while offline, and provides the confidence to continue making bold changes to the system. After all, I can easily recover to a recent working state.
- Repeat 1 & 2 until the codebase contains “significant” functionality and I have connectivity to the remote repository.
- Push the accumulated changes to the remote repository. The smaller the change set, the less likely extensive merging will be required. Smaller is better, IMHO.
The remote repository typically has a greater degree of fault tolerance and active management dedicated to it. This can be as simple as a network share that is backed up nightly or as complex as dedicated hardware with specialized server-side processing and significant administrative monitoring.
XCode’s out-of-the-box Git integration enables steps 1 and 2 above. Time Machine backups of the local repository add an additional degree of fault tolerance, but do not support collaboration or take advantage of managed infrastructure such as on-premises or cloud-based storage.
Creating a Remote Repository
These are the steps I use to enable the full workflow identified above. For simplicity the “remote” repository is created on the local file system. This location could easily be on a mounted network volume.
Create a Test Project
My project is called HelloGit and is located at /Users/Don/Dev/HelloGit. Be sure to commit all outstanding changes. XCode always leaves a single changed file for me after the project is created and the initial commit is submitted.
Clone the Local Repository
We want to clone the XCode-created Git repository to the location where the remote repository will reside. In this case it will be /Users/Don/Dev/RemoteHelloGit.
- Open the Terminal application.
- Clone the local repository to the remote repository location: git clone /Users/Don/Dev/HelloGit /Users/Don/Dev/RemoteHelloGit
Convert the Remote Repository to a Bare Repository
The remote repository only needs to contain the Git database. It does not need a checked out branch or local files.
- Go to the remote repository folder: cd /Users/Don/Dev/RemoteHelloGit
- Indicate the repository is “bare”: git config --bool core.bare true
- Remove files, leaving the .git folder: rm -R *
- Remove the “origin” remote: git remote rm origin
Configure the Local Repository
The local repository should reference the remote repository. The remote name “origin” is used by convention to indicate the originating repository. This is set automatically when a repository is cloned. We will use the “origin” name here to reflect that relationship.
- Go to the local repository folder: cd /Users/Don/Dev/HelloGit
- Add the remote: git remote add origin /Users/Don/Dev/RemoteHelloGit
Any changes made to the local Git repository can be pushed to the remote repository subject to the merging rules Git enforces.
- Create a new local file: date > date.txt /li>
- Add the new file to the local index: git add date.txt
- Commit the change to the local repository: git commit -m "New file: date.txt"
- Push the change to the remote repository: git push origin master
Now you can save, commit, and push/pull to your OCD hearts’ content!
Code without fear!