Search code examples
gitbitbucketbitbucket-pipelinesgit-workflow

Understanding Git: Latest push is not visible on production server


I have self-taught working with git repositories wanted to create the following environment which fits my needs.

  • I code on my local machine
  • I have a bitbucket repository, where I keep everything update (as far as I understand, my origin)
  • I want to push code to a production server over ssh

Everything is working as expected, I only have one problem while pushing to the production server. I always need to login to the server by shell to make a hard reset before the latest pushed commit is visible on the frontend. It looks like the last push is simply not getting "activated" on the production side.

I have the following workflow to setup my environment:

  1. I create a git repo on my production server with git init
  2. I clone the repo on my local machine with git clone ssh://urlToRepo
  3. I create a new repo on bitbucket
  4. On my local machine, I link the bitbucket repo with git remote add origin URL
  5. I work on my code, commit and push it to both remotes.
  6. On bitbucket, the commit is visible as expected.
  7. On the production server, the changes are not active. I need to do a git reset --hard to have them visible.

To get my pushes accepted on the production side, I also need to configure git config receive.denyCurrentBranch ignore. I tried git init --bare thinking maybe with a bare repo, I do not have this problem. Unfortunately, no files are shown at all on the server.

As you see, I'm probably not fully understanding the concept. Does anybody know how to help and hint me into the right direction?

Thanks already.


Solution

  • As @choroba pointed out, Git is not a deployment tool. You can use it do that, but often it makes more sense to use something like a ci/cd service for that sort of thing.

    You mentioned that you are using Bitbucket, which offers a feature called Bitbucket Pipelines that lets you run bash scripts upon pushing to certain branches or branch patterns. Most Git-Hosting providers offer a solution like this. For example: GitHub Actions and GitLab CI.

    So in order to push the latest code to the server you would write a Bitbucket Pipeline configuration that copies the code from the repository to your server via ssh and rsync.

    Here is an article that describes how to use ssh (keys) inside Bitbucket Piplelines.

    An alternative solution could be that you have a bash script on your server, that can be triggered through ssh, to run something like: git pull. Here is a link to an answer that should help you to achieve deploying your code via 'pull from repo to server'.

    I'd recommend the 'push to server' approach, but the 'pull from repo to server' works too. It's really up to you.