Skip to content

git

Here are the three best ways to sync a Git repo onto an EC2 Ubuntu instance. Choose the one that fits your workflow (auto-deploy, manual pull, or CI/CD).


Method 1 — Clone & Pull directly on the EC2 instance

Section titled “✅ Method 1 — Clone & Pull directly on the EC2 instance”

(simplest and most common)

Terminal window
ssh -i ~/.ssh/your-key.pem ubuntu@your-ec2-ip
Terminal window
sudo apt update
sudo apt install git -y
Terminal window
git clone https://github.com/username/repo.git

Or with SSH:

Terminal window
git clone git@github.com:username/repo.git
Terminal window
cd repo
git pull

On EC2:

Terminal window
ssh-keygen -t ed25519
Terminal window
cat ~/.ssh/id_ed25519.pub

Copy → paste into GitHub: GitHub → Settings → SSH keys → Add key

Then you can clone with:

Terminal window
git clone git@github.com:username/repo.git

Method 2 — Sync using rsync from your local machine

Section titled “✅ Method 2 — Sync using rsync from your local machine”

(best for deployment without exposing server to GitHub)

From your laptop:

Terminal window
rsync -avz --exclude 'node_modules' --exclude '.git' \
-e "ssh -i ~/.ssh/web1.pem" \
. ubuntu@op1.evolo.fr:~/starl1

Then on EC2:

Terminal window
cd ~/starl1
npm install
pm2 restart app

This is what we already talked about for deployments.


Method 3 — Auto-deploy using GitHub Webhooks + PM2 deploy

Section titled “✅ Method 3 — Auto-deploy using GitHub Webhooks + PM2 deploy”

(best for production)

This pulls your repo automatically when you push to GitHub.

Terminal window
npm install -g pm2

2. Add deploy section to your ecosystem.config.js:

Section titled “2. Add deploy section to your ecosystem.config.js:”
module.exports = {
apps: [{
name: "app",
script: "index.js",
}],
deploy: {
production: {
user: "ubuntu",
host: "your-ec2-ip",
ref: "origin/main",
repo: "git@github.com:username/repo.git",
path: "/home/ubuntu/app",
"post-deploy": "npm install && pm2 reload app --update-env"
}
}
}
Terminal window
pm2 deploy production setup
pm2 deploy production

This does:

✔ Clone repo ✔ Install dependencies ✔ Restart Node app ✔ Rollback in case of failure

Automatic and clean.


NeedBest Method
Simple syncGit clone + pull
Deploy from local machinersync
Full auto-deploymentPM2 deploy
Large app or multiple serversPM2 or GitHub Actions