Book Review: How to Implement Design Patterns in PHP
Changing Mailman Python Scripts for Virtual Host Support
Symfony 2 Crash Course
Nice n' Easy JQuery Image Rotator
Enforce Coding Standards with PHP_CodeSniffer and Eclipse IDE on Ubuntu Linux
Development Resource Project

Changing Git Repos to Use an External Drive as Origin

Friday, 20 April 18, 10:09 am
I've always had multiple computers at home on which I do development work. Every time I start a new project on one machine, I will clone it from that machine onto any others as and when I want to work on it from another machine. The drawback of this approach is that in order to push and pull commits, the origin computer has to be turned on. There's also a minor annoyance in that the origin for different projects will be arbitrarily on any one of the machines I own. Right now I'm traveling away from home, and while I still have three computers that I work on, switching them on and networking them is much more inconvenient. So to avoid that issue, I want to set up an external USB flash drive as origin for all my projects.

Putting the repos on the drive is as simple as copying the project folders. Use the -p switch to preserve file ownership and permissions. If your project directory is ~/projects/project, your flash drive's label is GIT, and you're using current standard Linux automounting, it would look something like this:
cp -pr ~/projects/project /run/media/rdeeson/GIT/
Now we need to update our Git URLs to point to the new location. From the project directory on your computers (ie not the flash drive directory), check what URLs are currently set up with:
git remote -v
If the project was originally cloned from a different machine, you'll see an entry for origin listed. Update it to point to the location on the flash drive like so:
git remote set-url origin /run/media/rdeeson/GIT/project
If the project is the original and was not cloned from elsewhere, there won't be anything for origin already listed, so add it and then set it as upstream for all relevant branches:
git remote add origin /run/media/rdeeson/GIT/project git pull git branch --set-upstream-to=origin/master master
The git pull above will output an error about there being no tracking information for the remote branch, however we need to run it in order for the subsequent command to work.

You'll need to do this for each of the computers you work on. Afterwards, whenever you do a git pull, it should pull from the flash drive rather than the network. Likewise, git push would push to there as well. However, as Git doesn't like to push to a branch that is currently checked out, I prefer to use git pull from the flash drive folder. To make this work, we need to go to the project directory on the flash drive, remove any origin remote, and then add a remote for each machine you'll be sharing with. (If all your projects have the same local path on all of your machines, you can of course add just one remote to cover them all.)
cd /run/media/rdeeson/GIT/project git remote remove origin git remote add local /home/rdeeson/projects/project
Note that rather than changing to the directory on the flash drive and then running your Git commands, you can instead use the -C switch to specify the directory e.g.
git -C /run/media/rdeeson/GIT/project pull local master

Please enter your comment in the box below. Comments will be moderated before going live. Thanks for your feedback!

Cancel Post

/xkcd/ Pendulum Types