Better git it in your soul*.
Git has a lot of great tutorials for getting started. There are also a number of great articles on how to use git and github for your workflow.
What I haven’t seen is an article on how to integrate git with your current site without storing any code on github. I’m writing this blog to create a quick reference for how to get up and running using git on your existing site.
Needed ¶
I’m making the assumption that you have the following:
- Knowledge of Linux
- A local development environment
- git-core installed both locally and on your webserver
- rsync installed both locally and on your webserver
- Keyless SSH access to your webserver
I’m using Ubuntu 12.04 locally, but I’d assume that most of this won’t be too different on a different distribution or on a Mac—but I’m probably totally wrong about that ☺
Step One: ¶
RSync your site to your local development environment
In order to begin to develop locally (and break the old cowboy-coding habits that you’ve undoubtedly developed over the years) you need a local copy of your site.
- Open your terminal emulator and
cd
to the directory in which you will be storing these files (i.e.cd /srv/www/tylercipriani.com/public_html
) - Rsync the
htdocs
orpublic_html
from your webserver into this local directory:
The command breaks down like this:
a
means “Archive”—keeps permissions, mtimes, etc the samev
means “Verbose”—increases verbosity of the commande
means “RSH”—allows you to use remote shell (same as RSH=command):/path/to/htdocs/
—the path to you htdocs folder. The trailing/
is significant—it means copy the content of the htdocs directory rather than the directory by name.
is the current directory
Step Two: ¶
Initialize git in local development environment.
This step will create a new git repository on your local machine and add all the code that you’ve rsynced in the previous step to that repo.
cd
to the directory to which you previously rsynced your site and initialize a git repository by runninggit init
- Add the contents of the current directory to the git repository by running
git add .
Commit all your newly added files to the repo by running your first commit
git commit -m “First Commit”
Step Three: ¶
Setup a bare repo on your web server
You need a bare repo out on your webserver that will act as a mirror to your local development environment.
ssh into your webserver and make a new directory, I usually make it above the webroot (i.e.
htdocs
)Once inside the new directory initialize a bare repository by using the
--bare
flag:Now we can define a new post-receive hook that will be triggered whenever an update is pushed to this new bare repository. The post-receive hook can be any type of script you want, the script below is written in bash.
cd
into the.git/hooks
directory and create a file called “post-receive”. Copy the code below into the file:make sure that this code is executable by running
chmod +x .git/hooks/post-receive
You’re Done! ¶
Push to your new repo, you beautiful command-line ninja, you!
Back on your local machine, in the webroot of your local development environment, add your bare webserver repo as your remote
and push your git repo up to your server. The post-receive hook will take care of the rest!
$ git remote add web ssh://user@tylercipriani.com/home/user/tylercipriani.com.git
$ git push -u origin master
By using the -u
flag you’re setting the upstream which means you can just run git pull
without further arguments to merge origin and master.
This is a post that has been a lifesaver for me. I read this every time I need to set up Git with an existing project (surprisingly often) and it is always an efficient way of doing things. Thanks for covering this so well Tyler - this is far better than other posts on this subject.
I use this post all the time, too
That’s the reason I write most of these posts—so I don’t have to figure out how to do something twice.
Following these steps, I get an error:
git push -u origin master
fatal: ‘origin’ does not appear to be a git repository
fatal: Could not read from remote repository.
However, using ‘web’ rather than ‘origin’ results in what appears to be success.
Am I in error?
No you’re not, I am, actually! The
git remote add web
named the remoteweb
. You can list out all your remotes withgit remote
.I’ll get a fix up here soon. Nice catch!
Thanks for the write up. This is quite nearly exactly what I’ve been looking for. I’m trying to setup a faux-central repo on my webserver so I can develop locally, push to central repo and then pull from central repo to production.
A couple things not quite working just yet, but I’m hopeful.