How to set up git deployment to Staging and Production from BitBucket so they don't have working dir
Asked by David Newey:

We have a repo that contains 'develop' and 'master' branches among others. I have it set up so that I push new commits from my machine to the develop branch. I am currently the only developer but expect to have more in the future. I create a branch on my machine for a new feature, do the development, commit and merge that branch to develop on my machine and then push to develop in the Bit Bucket repo. I want to then push the code to our Staging server.

There should be no development on the Staging server so I don't need a working directory. I just want it simple - push the code to Staging and the changes are merged into the existing code. If some correction to the code in Staging is needed, that will be done in the original feature branch on my machine and committed, merged and and pushed to 'develop' on Bit Bucket and again pushed to Staging.

Likewise, once the code on the Staging server is verified, I need to merge the code into 'Master' from 'develop' and push to the Production server from 'Master' in a similar manner as I'm pushing to Staging. All simple and clean.

A couple questions:

1. How to set up the git instances on Staging and Production servers for this scenario?

2. Production server code is an image from another machine and so it currently has a lot of "changes" both "modified" and "new". How do I convert this over from that to the set-up I'm talking about above?

Note: This is a Laravel 8 framework with PHP 8 on Centos 8 machines.


Answers:
author: Mark C
body: Hi [@David Newey](/t5/user/viewprofilepage/user-id/4404161)

Thank you for reaching out to the community.

For best practices, we do recommend managing your environments as branches within your repository.  
In your case, you can have the following branches:

* development
* staging
* production (or master)

The workflow would be: development \> staging \> production (or master)  
![](https://getsupport.atlassian.com/images/icons/emoticons/information.png) If you want to have other branches (e.g. feature branches) you can merge them to the **development** branch first then continue the process.  
For merging one branch to another, you can implement [pull requests](https://support.atlassian.com/bitbucket-cloud/docs/use-pull-requests-for-code-review/) as well.

Now, when it comes to your servers, you can use Git and just simply clone your repository based on their environment.  
For example:

* For your Staging server, you can clone your repository and Git checkout to the **staging** branch
* For your Production server, you can clone your repository and Git checkout to the **production (or master)** branch

After that, you can just do Git pull from your Staging or Production servers either manually or automatedly (e.g Using [Bitbucket Pipelines](https://bitbucket.org/product/features/pipelines)) to get new updates.  
Alternatively, you can also replace or overwrite a server's version using SSH or rsync commands just make sure you use the specific branch for the environment (staging or production).

By doing this workflow, you can separate your **Staging** and **Production** servers based on repository branches.

Hope it helps and let me know if you have further questions.

Regards,  
Mark C


Tags: bitbucket-cloud

cloud