Answers: <p>The commit happens on the machine where the change was made, these changes are then pushed to the remote repository through <code>git push origin HEAD</code> or similar. So the commit has happened by the time your deployment pipeline runs.</p>
  <p>I think perhaps you are duplicating some work, you haven't posted the full YAML so I can't tell for sure, but the pipeline should <a href="https://support.atlassian.com/bitbucket-cloud/docs/git-clone-behavior/" rel="nofollow noreferrer">be configured</a> to clone the repository</p>
  <h2>Workflow to access an arbitrary branch in same git</h2>
  <p>Assuming BitBucket has been configured to checkout a copy of the repository and execute commands in the repository as it's &quot;working directory&quot; then you could safely use:</p>
  <p><code>git checkout origin/master</code></p>
  <p>This will put the repository into a <a href="https://stackoverflow.com/questions/61497693/what-is-the-git-checkout-master-vs-git-checkout-origin-master">Detached Head</a> state, and give you access to the head commit on origin/master and then execute your code from there.</p>
  <h2>Workflow to access a different git repo</h2>
  <p>If you need a different repo you can access this through:</p>
  <pre><code>git clone [repo-uri]
  git checkout master
  </code></pre>
  <p><code>master</code> is probably the default branch, so the second command is probably unnecessary but I don't know your config for sure.</p>
  <h2>General Troubleshooting</h2>
  <p>It can be helpful to add some troubleshooting commands to the pipeline in times like this:</p>
  <ul>
  <li><code>echo $BITBUCKET_CLONE_DIR</code> - will print the clone directory</li>
  <li><code>pwd</code> - will print the current working directory</li>
  <li><code>git log --oneline --graph --decorate --all</code> will give you some idea of the state of the tree</li>
  </ul>
  <p>I'd also suggest checking out the repository in a different directory/folder on a local machine, and checking everything is where it's supposed to be. Sometimes, someone pushes some changes which look okay, but in practice are wrong:</p>
  <ul>
  <li>check the mainline branch is <code>master</code> not <code>main</code></li>
  <li>check the <code>origin/master</code> branch ref got updated to the latest commit, sometimes a botched command locally can result in pushing commits but not updating the ref</li>
  <li>check the commit exists at all</li>
  </ul>
  
Answers: CC BY-SA 4.0
Answers: 2024-08-14T21:57:02.133Z
Answers: 2024-08-14T21:57:02.133Z
Answers: Richard Slater
Body: <p>I am adding a production deployment pipeline to a Bitbucket project. The YAML file is roughly</p>
  <pre class="lang-yml prettyprint-override"><code>image: php:8.2.20
  
  pipelines:
    branches:
      master:
        - step:
            name: Deploying to production
            deployment: production
            script:
              - apt-get update &amp;&amp; apt-get install -y openssh-client
              - ssh $PROD_SSH_USER@$PROD_SSH_HOST &quot;bash -s&quot; &lt; scripts/production.sh
  </code></pre>
  <p>(<strong>Note:</strong> the YAML file only shows the essential part, the image is required for other tasks.)</p>
  <p>The <code>production.sh</code> scripts is basically:</p>
  <pre class="lang-sh prettyprint-override"><code>#!/bin/bash
  
  cd /path/to/project
  
  git fetch origin
  git reset --hard origin/master
  
  # further installation steps
  </code></pre>
  <p>However, the <code>git reset</code> does not fetch the latest commit. Is it because the pipeline runs before the commit to the <code>master</code> branch is done?</p>
  <p>If not, why isn't <code>git reset</code> pulling the latest commit?</p>
  <p>If so, how can I get the commit being set to <code>master</code>?</p>
  
Comments: CC BY-SA 4.0
Comments: 2024-08-14T15:47:47.030Z
Comments: @SyedM.Sannan you want my question to be downvoted and closed before anyone can answer? If some admin could move this question to devops, then I agree that it should go there.
Comments: Yanick Rochon
Tags: <git><cicd><bash><ssh><bitbucket-pipelines>
Title: Are Bitbucket branches pipelines running before or after the commit to that branch?
UserDisplayName: Yanick Rochon
ThreadText: # Are Bitbucket branches pipelines running before or after the commit to that branch?
  
  (Asked by: username_0 on 2024-08-14)
  
  I am adding a production deployment pipeline to a Bitbucket project. The YAML file is roughly
  
  ```yml
  image: php:8.2.20
  
  pipelines:
    branches:
      master:
        - step:
            name: Deploying to production
            deployment: production
            script:
              - apt-get update && apt-get install -y openssh-client
              - ssh $PROD_SSH_USER@$PROD_SSH_HOST "bash -s" < scripts/production.sh
  
  ```
  
  (**Note:** the YAML file only shows the essential part, the image is required for other tasks.)
  
  The `production.sh` scripts is basically:
  
  ```sh
  #!/bin/bash
  
  cd /path/to/project
  
  git fetch origin
  git reset --hard origin/master
  
  # further installation steps
  
  ```
  
  However, the `git reset` does not fetch the latest commit. Is it because the pipeline runs before the commit to the `master` branch is done?
  
  If not, why isn't `git reset` pulling the latest commit?
  
  If so, how can I get the commit being set to `master`?
  
  **Tags:** git, cicd, bash, ssh, bitbucket-pipelines
  
  > **Comments:**
  > **username_0**: @SyedM.Sannan you want my question to be downvoted and closed before anyone can answer? If some admin could move this question to devops, then I agree that it should go there.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-08-14. Score: 1 | Accepted answer
  
  The commit happens on the machine where the change was made, these changes are then pushed to the remote repository through `git push origin HEAD` or similar. So the commit has happened by the time your deployment pipeline runs.
  
  I think perhaps you are duplicating some work, you haven't posted the full YAML so I can't tell for sure, but the pipeline should [be configured](https://support.atlassian.com/bitbucket-cloud/docs/git-clone-behavior/) to clone the repository
  
  ## Workflow to access an arbitrary branch in same git
  
  Assuming BitBucket has been configured to checkout a copy of the repository and execute commands in the repository as it's "working directory" then you could safely use:
  
  `git checkout origin/master`
  
  This will put the repository into a [Detached Head](https://stackoverflow.com/questions/61497693/what-is-the-git-checkout-master-vs-git-checkout-origin-master) state, and give you access to the head commit on origin/master and then execute your code from there.
  
  ## Workflow to access a different git repo
  
  If you need a different repo you can access this through:
  
  ```
  git clone [repo-uri]
  git checkout master
  
  ```
  
  `master` is probably the default branch, so the second command is probably unnecessary but I don't know your config for sure.
  
  ## General Troubleshooting
  
  It can be helpful to add some troubleshooting commands to the pipeline in times like this:
  
  * `echo $BITBUCKET_CLONE_DIR` - will print the clone directory
  * `pwd` - will print the current working directory
  * `git log --oneline --graph --decorate --all` will give you some idea of the state of the tree
  
  I'd also suggest checking out the repository in a different directory/folder on a local machine, and checking everything is where it's supposed to be. Sometimes, someone pushes some changes which look okay, but in practice are wrong:
  
  * check the mainline branch is `master` not `main`
  * check the `origin/master` branch ref got updated to the latest commit, sometimes a botched command locally can result in pushing commits but not updating the ref
  * check the commit exists at all
  
  
  ---
  
  