Sync Files Between a Computer and VPS w/ Git
Git is a version control system that allows you to track changes to files and collaborate with other developers. It can also be used to sync files between a VPS (Virtual Private Server) and a computer. Here’s how to do it in a few short steps:
- Install git on both the VPS and your computer if it’s not already installed. On the VPS, run
sudo apt-get install git
. - Create a git repository on your VPS by navigating to the desired directory and running
git init
. - Add your files to the repository using
git add .
. - Commit the changes with
git commit -m "Commit message"
. - On your computer, create a new repository with
git init
and add the VPS repository as a remote withgit remote add origin ssh://user@your_server_ip:port/path/to/repo
. Replace the placeholders with your VPS login information.
To automatically sync your files, you can use cronie, a task scheduler that allows you to run commands at specific intervals. Here’s how to set it up:
- On the VPS, install cronie with
sudo apt-get install cronie
. - To edit your crontab file, run
crontab -e
. - Add a line to the file to run the
git push
command at the desired interval. For example, to push changes every hour, you can use the following line:0 * * * * git push origin master
. - Save the file and exit.
On your computer, you can also use cronie to automatically pull changes from the VPS. Here’s how:
- Install cronie with
sudo apt-get install cronie
. - To edit your crontab file, run
crontab -e
. - Add a line to the file to run the
git pull
command at the desired interval. For example, to pull changes every hour, you can use the following line:0 * * * * git pull
. - Save the file and exit.
That’s it! You can now use git and cronie to automatically sync files between your VPS and computer.
If you are in need of VPS or domain registration services, we would recommend NameHero.
If you are interested in web design, managed hosting, or marketing services, we offer them at Sunshine Tech and Media.