Deploy branch from intermediate bare git repo

Updated: 09 November 2023

Scenario

  1. I have a project on BitBucket.
  2. I work on this project locally.
  3. I want to deploy the changes to Production.
  4. Rather than pulling changes directly from Bitbucket to the working directory on the Production server, I need to go via a bare-repo on the Production server. The bare repo has been cloned from BitBucket earlier. This workflow is similar to deployments on Cloudways or Plesk.

Method

1. Push changes from local to BitBucket as normal.

2. From inside the bare repo directory on the Prod server, fetch all the changes from BitBucket

cd /bare/repo/my-proj.git
git fetch origin *:*

3. Transfer the changes to the working directory

cd /anywhere/on/the/system
git --work-tree /path/deploy/dir --git-dir /bare/repo/my-proj.git checkout --force my-branch