4

I'm new to Git and I don't understand how I can achieve the following. There are 2 servers:

  1. Localhost
  2. Linode.com VPS

Basically, I want to:

  1. Write new code on my localhost.
  2. Push the new code to the development version on the VPS where it can be tested (at dev.domain.com or something like that.)
  3. If the new code is working, push it to the production version on the same VPS. Should be accessible at domain.com.

What is the right way to achieve what I want?

Alexander Zaytsev
  • 2,744
  • 1
  • 16
  • 8

3 Answers3

5

There are several ways to do this. If you have the ability to run an ssh server on you VPS then this is fairly simple.

In your git repository on localhost you will setup two git remotes. They will have the same host but different paths (one remote for the dev path and one for the prod path).

git remote add prod ssh://[user@]host.xz[:port]/path/to/prod/repo.git/
git remote add dev ssh://[user@]host.xz[:port]/path/to/dev/repo.git/

And if you setup ssh public/private key access then you don't have to type a password everytime.

Once you have committed the changes you want into your repo on localhost then you will do this to push them to the dev environment:

git push dev    # remote named dev points to dev repository

After they are verified you can then push them to production (from your repo on localhost):

git push prod   # remote named prod points to prod repository

If you are going to change the git repo on localhost in between pushing to dev and prod (other than fixes you want applied) then there are many ways to address this:

  • branch or tag before pushing to dev and push that instead of your main branch (recommended anyways for other reasons).
  • make a copy of the repo on localhost and push that.
  • branch before making changes and push the branch instead of the main branch.
  • login into the VPS and just push (or pull) from the dev to the prod repo

That doesn't cover half your options, but maybe enough to get thinking.

kanaka
  • 70,845
  • 23
  • 144
  • 140
  • That was my plan, until I discovered that pushing to non-bare repositories is not recommended. Considering the fact that I'll be working alone on this site, I guess it isn't an issue? I've found two ways to push into a non-bare repository: http://stackoverflow.com/questions/1764380/push-to-non-bare-repository/1764793#1764793 I guess I could use the second option? – Alexander Zaytsev Dec 09 '10 at 15:11
  • If you aren't going to be doing any editing on the dev and prod repos then you should be safe doing the `git reset --hard HEAD` after push. You could also consider just doing an `rsync -avz --del --exclude=.git ...` (the --del option deletes files at the target that are no longer in the source). – kanaka Dec 09 '10 at 19:44
  • 1
    Yes, I think I won't be doing any editing there. After another thought I came up with an idea of using 4 repositories: local, remote-development, remote-production and remote-bare. I can push from the local repository to the remote-bare and then I can pull from the latter to the development/production repository. What do you think? – Alexander Zaytsev Dec 09 '10 at 21:05
  • Yep, that is essentially the pull model but without requiring you to expose your localhost development system to the Internet. – kanaka Dec 09 '10 at 22:09
  • Thanks, I will use this variant then. – Alexander Zaytsev Dec 10 '10 at 14:27
0

PhpStorm can automatically synchronize changes over sftp, even when you change branches locally.

On Windows systems that is the best solution I've found so far. On unix/mac systems you can use rsync in combination with a utility that watches for file system changes.

  • wouldn't it be easier to use a central git server and use branches for the different env's? – iwein Oct 21 '12 at 07:25
0

A suggestion: (which is not exactly what you want)

1) Use the "normal git way of working".. have a local and a remote repository.

2) Pull the local repository code onto the VPS for testing

3) Pull the remote repository code onto the VPS for production

Diego Dias
  • 21,634
  • 6
  • 33
  • 36
  • If the VPS is outside his LAN/WAN then this assumes that localhost is exposed to the Internet. Usually, the internal development machines are not as secured as the deployment systems. – kanaka Dec 09 '10 at 14:40
  • I don't have a static IP thus I can't pull the local repository code to the VPS. – Alexander Zaytsev Dec 09 '10 at 15:22