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)
1. SSH into your EC2
Section titled “1. SSH into your EC2”ssh -i ~/.ssh/your-key.pem ubuntu@your-ec2-ip2. Install Git
Section titled “2. Install Git”sudo apt updatesudo apt install git -y3. Clone your repo
Section titled “3. Clone your repo”git clone https://github.com/username/repo.gitOr with SSH:
git clone git@github.com:username/repo.git4. Update later (sync changes)
Section titled “4. Update later (sync changes)”cd repogit pull🔐 If using a private GitHub repo
Section titled “🔐 If using a private GitHub repo”On EC2:
1. Create SSH key
Section titled “1. Create SSH key”ssh-keygen -t ed255192. Show the public key
Section titled “2. Show the public key”cat ~/.ssh/id_ed25519.pubCopy → paste into GitHub: GitHub → Settings → SSH keys → Add key
Then you can clone with:
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:
rsync -avz --exclude 'node_modules' --exclude '.git' \-e "ssh -i ~/.ssh/web1.pem" \. ubuntu@op1.evolo.fr:~/starl1Then on EC2:
cd ~/starl1npm installpm2 restart appThis 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.
1. Install PM2 deploy tool:
Section titled “1. Install PM2 deploy tool:”npm install -g pm22. 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" } }}3. From your local machine:
Section titled “3. From your local machine:”pm2 deploy production setuppm2 deploy productionThis does:
✔ Clone repo ✔ Install dependencies ✔ Restart Node app ✔ Rollback in case of failure
Automatic and clean.
🎯 Which method should YOU use?
Section titled “🎯 Which method should YOU use?”| Need | Best Method |
|---|---|
| Simple sync | Git clone + pull |
| Deploy from local machine | rsync |
| Full auto-deployment | PM2 deploy |
| Large app or multiple servers | PM2 or GitHub Actions |