Making a new Mac your Git server
My problem was figuring out how to think about this and, hand in hand with that, what to search for to find simple solutions. Presented here is the simplest solution I have found. The main source of all the information presented here is the Pro Git book. Familiarity with the terminal is assumed.
There will be updates
My friend Martin quickly suggested an even better procedure for achieving the same results. All the below works just fine, but his suggestion nicer so I will probably update this post soon-ish to reflect.
Martin's better version, in short:
On the server:
git init --bareOn the client:
git initgit remote add origin [url]
git push origin HEAD:master
To cut a long story short
Activate ssh access (called "Remote login" in the "Sharing" system preference pane) on the server Mac.
Create a bare version of your project:
git clone --bare ProjectName ProjectName.gitCopy the bare version to the server:
scp -r ProjectName.git user@url.to.server:path/to/git/folderClone the project back to your work computer from the server:
git clone user@url.to.server:path/to/git/folder/ProjectName.git
And boom, we're done. Fore some more details and explanations, read on.
The situation
One Mac on which you have some code. In my case, a laptop with a mixture of projects either in local, Xcode-created, Git repositories and projects completely without version control.
One Mac, in my case a nice little Mac mini under the TV, which acts as some form of server. I want my projects to live here for ease of access, control and additional backup. Both Macs run Lion and have a recent version of Xcode installed. (On the server and for this exercise, this only matters because Xcode installs and sets Git up for you. Everything should work exactly the same no matter how you got Git on your server, but this was my setup so it is the only one I have tried.)
To begin with, turn on "remote login" in the Sharing panel in System preferences on the server. This enables logging in over ssh, which can be useful for a million different things. You can also set up which types of users get to log in, create new users and so forth. For my case, I am quite happy to keep going with the users I already have.
Decide where you want to store your repositories. When you log in using ssh, you by default end up in the users' home folder. So it is convenient to put your folder for repositories reasonably near that. For my case, I already had a folder for coding right inside my home folder, so I threw a git folder in there.
Getting existing Git versioned projects to the server
Fire up a terminal and go to the parent folder of your project's folder. Type:
git clone --bare ProjectName ProjectName.git
… where "ProjectName" is the name of the project's folder (inside of which you find the .git folder). If there are spaces and stuff it is easiest to put the name in quotes and change the .git version to a similar name without spaces. So, for example, git clone --bare "Project name" ProjectName.git.
This creates a folder full of relatively indecipherable stuff which is just right for putting on your server.
To send it there, type:
scp -r ProjectName.git user@url.to.server:path/to/git/folder
ProjectName.git will be put inside the folder specified at the end of the path. The -r means "recursive" and tells the copy to include everything inside folders and sub-folders. Leave it out and things will not work. "user" is an existing user on the server. When you hit enter you will be prompted for the user's password. Enter it, hit enter and the copy should begin. For local network copies, it also ought to finish pretty quickly.
Once it does, it is time for the exciting part: cloning the repository from the server and testing everything. At this point it should be safe to delete your local project folder and the .git version as everything is safe on the server. If you are anything like me, though, you probably want to keep the folders around until you have tested this step. So, head where you want your fresh, server-connected, project to live and type:
git clone user@url.to.server:path/to/git/folder/ProjectName.git
Again, the folder ProjectName will be created inside the folder which you are in when you issue the command.
Time to check that everything worked. Open up the freshly cloned project in Xcode and head to the Repositories tab of the Organizer. When you highlight your project you will notice the third line "Origin" displaying the URL of your server, the very same one you entered when issuing the final git clone command.
Congratulations, you are all set! You can now keep working just like you did before, committing code as you go. Commits are local as always. Issue a push when you want to send your work to the server, and a pull if you want to download changes.
Extra material: putting an old project under version control
This is straight out of Apple's documentation. At the time of writing, Xcode provides no way of adding an existing project to git version control, so once again we go to the terminal.
Open the project's folder in the terminal and enter the following three commands with enter presses in-between.
git init
git add .
git commit -m "Initial commit"
Then you can proceed with the above recipe, creating a bare repository and sending it to the server. This works and is quick (which is why I did it this way), but it is not optimal. For one thing, adding . means adding exactly everything, including build folders and such stuff you would rather be without. For another, I am pretty certain there is a way to go straight from unversioned code to a bare repository or something and save a couple of steps. But, to my mind, getting everything under control reliably and easily is more important than doing it the ideal way. Details can be fiddled with once everything is safe and sound.
© 2012 Fredrik Björeman - bjoreman.com. All rights reserved.