From index_old2.php to Git: how the agency discovered version control
How we went from FTP and copied files to Subversion and then Git at the agency, with the commands you actually need as a beginner.
When I started at the agency last year, our “version control” looked like this:
/public_html
index.php
index_old.php
index_old2.php
index_backup_jan.php
index_DO_NOT_USE.php
functions.php
functions.php.bak
functions_domien.php
Nobody knew who had changed what. If two people worked on the same file, whoever clicked “upload” in FileZilla last won. And yes, index_DO_NOT_USE.php was in use, by an iframe on a page nobody remembered.
This is the story of how that changed, and what I learned along the way.
Step 1: Subversion#
The trigger was a classic. A colleague and I both edited a client site’s functions.php on the same Friday afternoon. He uploaded at four, I uploaded at five past four. A full day of his work: gone. Jef looked over from behind his monitor, pointed at the sheet of A4 that says “NO DEPLOYS ON FRIDAY” and said nothing. That was worse than if he’d said something.
A week later there was a Subversion server running on an old PC under Jef’s desk. SVN is centralised: there’s one repository on the server, and everybody works in a “working copy”.
svn checkout svn://devbox/clients/bakery/trunk bakery
cd bakery
# ... edit some files ...
svn status
svn diff
svn commit -m "Update opening hours on contact page"
svn update
That alone was a huge step forward. We had history, we could see who changed what, and svn update warned about conflicts instead of quietly overwriting someone’s work.
But there were annoyances too:
- Every commit went straight to the server. Saving work-in-progress while trying something out meant everyone got it.
- SVN has branches, but merging is painful. After one failed merge, nobody at the agency ever created a branch again.
- When the PC under Jef’s desk was down, nobody could commit. That happened once, when the cleaner unplugged it for the vacuum cleaner.
- All those
.svnfolders in every subdirectory. Which, of course, also ended up on the production server via FTP.
Step 2: Git#
I’d been using Git at home for a few months on my own hobby projects, and pretty much every open source project I use lives on GitHub. When we landed a new, bigger project in January, I suggested we give Git a try. Jef was sceptical (“another tool”), but after a ten-minute demo of branching he was sold.
The big difference: Git is distributed. You have the whole repository locally, with the entire history. Committing is local and fast. You only share your work when you push.
The commands you actually need#
There are hundreds of Git commands. Two months in, I use about ten of them daily.
One-time setup:
git config --global user.name "Domien"
git config --global user.email "[email protected]"
git config --global color.ui auto
Start a new project, or fetch an existing one:
git init
# or
git clone ssh://git@devbox/clients/webshop.git
The daily loop:
git status # what changed?
git diff # what exactly?
git add contact.php # I want this in the next commit
git add -p # or pick hunk by hunk, highly recommended
git commit -m "Validate postcode in contact form"
git log --oneline -10 # last ten commits
Branches, the real reason to switch:
git checkout -b new-menu # create a new branch and switch to it
# ... work, commit ...
git checkout master # back to master
git merge new-menu # bring the work in
git branch -d new-menu # clean up
Working with the central repository:
git pull origin master # get your colleagues' work
git push origin master # share your own
And for when you’ve messed something up (it happens):
git checkout -- contact.php # throw away local changes to one file
git commit --amend # fix the last commit, as long as it isn't pushed yet
A .gitignore from day one#
I wish I’d known this on day one. Put files that don’t belong in version control in .gitignore straight away, or you’ll drag them along forever:
# WordPress config with passwords
wp-config.php
# client uploads
wp-content/uploads/
# editor and OS junk
.DS_Store
Thumbs.db
*.swp
Passwords in version control are a bad idea. The bakery’s password now lives forever in the history of our SVN repository. It’s been changed since, but still.
Moving existing SVN projects over#
For projects that were already in SVN, there’s git svn. It pulls in the full history:
git svn clone svn://devbox/clients/bakery -s bakery-git
The -s means your repository follows the standard trunk/branches/tags layout. On big repositories this takes a while: get a coffee, or two.
And deploying?#
This is the honest part: we still deploy over FTP. The difference is that we now upload from a clean checkout of master instead of from somebody’s working folder. Jef eventually wants something with git pull on the server or a script using rsync, but we’re not there yet. One step at a time.
Meanwhile there’s no index_old2.php anywhere anymore. Except on that one site for the logistics client that we’re not allowed to touch. But that’s a story for another day.
Want to read more? The free online book Pro Git is the best explanation I know. You can find it, along with the documentation, via git-scm.com.