Why does Bamboo enter detached HEAD state on the second build of the same commit on the same agent?
Asked by Arkadiusz Drabczyk:

I use Atlassian Bamboo version 8.1.3 build 80110. I have a simple bamboo.yaml consisting of 5 stages and 7 jobs. Bamboo git cache is disabled. When the plan is run for the first time I get:

```
simple 23-Aug-2023 13:49:04 Starting task 'Source Code Checkout' of type 'com.atlassian.bamboo.plugins.vcs:task.vcs.checkout'
simple 23-Aug-2023 13:49:04 Checking out into /home/bamboo/bamboo-agent-home/xml-data/build-dir/MY-PROJECT3-JOB6
simple 23-Aug-2023 13:49:04 Updating source code to revision: e75accf7010794ada8febc4db5282bdb43fa620d
simple 23-Aug-2023 13:49:04 Creating local git repository in '/home/bamboo/bamboo-agent-home/xml-data/build-dir/MY-PROJECT3-JOB6/.git'.
simple 23-Aug-2023 13:49:04 Initialized empty Git repository in /home/bamboo/bamboo-agent-home/xml-data/build-dir/MY-PROJECT3-JOB6/.git/
simple 23-Aug-2023 13:49:04 Fetching 'refs/heads/*' from 'ssh://git@git.server.com:7999/my/project.git'.
simple 23-Aug-2023 13:49:04 Warning: Permanently added '[127.0.0.1]:37601' (RSA) to the list of known hosts.
simple 23-Aug-2023 13:49:05 From ssh://127.0.0.1:37601/my/project
simple 23-Aug-2023 13:49:05 * [new branch] my-branch -> my-branch
simple 23-Aug-2023 13:49:05 * [new branch] master -> master
simple 23-Aug-2023 13:49:05 * [new tag] v1.0.0 -> v1.0.0
simple 23-Aug-2023 13:49:05 Checking out revision e75accf7010794ada8febc4db5282bdb43fa620d.
simple 23-Aug-2023 13:49:05 Switched to branch 'my-branch'
simple 23-Aug-2023 13:49:05 Updated source code to revision: e75accf7010794ada8febc4db5282bdb43fa620d
simple 23-Aug-2023 13:49:05 Finished task 'Source Code Checkout' with result: Success
```

When it's run for the second time on the same agent I get:

```
simple 11-Sep-2023 15:28:35 Starting task 'Source Code Checkout' of type 'com.atlassian.bamboo.plugins.vcs:task.vcs.checkout'
simple 11-Sep-2023 15:28:35 Checking out into /home/bamboo/bamboo-agent-home/xml-data/build-dir/MY-PROJECT3-JOB6
simple 11-Sep-2023 15:28:35 Updating source code to revision: e75accf7010794ada8febc4db5282bdb43fa620d
simple 11-Sep-2023 15:28:35 Creating local git repository in '/home/bamboo/bamboo-agent-home/xml-data/build-dir/MY-PROJECT3-JOB6/.git'.
simple 11-Sep-2023 15:28:35 Initialized empty Git repository in /home/bamboo/bamboo-agent-home/xml-data/build-dir/MY-PROJECT3-JOB6/.git/
simple 11-Sep-2023 15:28:35 Fetching 'refs/heads/*' from 'ssh://git@git.server.com:7999/my/project.git'.
simple 11-Sep-2023 15:28:35 Warning: Permanently added '[127.0.0.1]:45627' (RSA) to the list of known hosts.
simple 11-Sep-2023 15:28:35 From ssh://127.0.0.1:45627/my/project
simple 11-Sep-2023 15:28:35 * [new branch] my-branch -> my-branch
simple 11-Sep-2023 15:28:35 * [new branch] master -> master
simple 11-Sep-2023 15:28:35 * [new tag] v1.0.0 -> v1.0.0
simple 11-Sep-2023 15:28:35 Checking out revision e75accf7010794ada8febc4db5282bdb43fa620d.
simple 11-Sep-2023 15:28:35 Note: switching to 'e75accf7010794ada8febc4db5282bdb43fa620d'.
simple 11-Sep-2023 15:28:35
simple 11-Sep-2023 15:28:35 You are in 'detached HEAD' state. You can look around, make experimental
simple 11-Sep-2023 15:28:35 changes and commit them, and you can discard any commits you make in this
simple 11-Sep-2023 15:28:35 state without impacting any branches by switching back to a branch.
simple 11-Sep-2023 15:28:35
simple 11-Sep-2023 15:28:35 If you want to create a new branch to retain commits you create, you may
simple 11-Sep-2023 15:28:35 do so (now or later) by using -c with the switch command. Example:
simple 11-Sep-2023 15:28:35
simple 11-Sep-2023 15:28:35 git switch -c <new-branch-name>
simple 11-Sep-2023 15:28:35
simple 11-Sep-2023 15:28:35 Or undo this operation with:
simple 11-Sep-2023 15:28:35
simple 11-Sep-2023 15:28:35 git switch -
simple 11-Sep-2023 15:28:35
simple 11-Sep-2023 15:28:35 Turn off this advice by setting config variable advice.detachedHead to false
simple 11-Sep-2023 15:28:35
simple 11-Sep-2023 15:28:35 HEAD is now at e75accf Some commit message
simple 11-Sep-2023 15:28:35 Updated source code to revision: e75accf7010794ada8febc4db5282bdb43fa620d
simple 11-Sep-2023 15:28:35 Finished task 'Source Code Checkout' with result: Success
```

I wonder why does Bamboo enter detached state instead of creating a branch and checking out revision on it when the build is run for the second time? I thought it's maybe because branch already exists as mentioned here [https://jira.atlassian.com/browse/BAM-20120?focusedId=1877088\&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-1877088](https://jira.atlassian.com/browse/BAM-20120?focusedId=1877088&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-1877088) but I use \`clean-working-dir: true\` and creating empty git repository succeeds. Is it documented somewhere?

Possibly related issues:

<https://jira.atlassian.com/browse/BAM-14600>  
<https://jira.atlassian.com/browse/BAM-20120>


Answers:
author: Eduardo Alvarenga
body: Hello [@Arkadiusz Drabczyk](/t5/user/viewprofilepage/user-id/5313758)

Welcome to **Atlassian Community**!

On the initial build run, Bamboo checks out the most recent revision in the branch, also known as the HEAD or latest. However, during the second run, it checks out the same revision as the first run. If other commits have been pushed between these two runs, and the revision used is no longer the latest, you'll encounter a "detached HEAD state".

Check this message that indicates the revision you used on your first run is not the latest:

```
simple 11-Sep-2023 15:28:35 HEAD is now at e75accf Some commit message
```

This indicates that the revision "*e75accf7010794ada8febc4db5282bdb43fa620d*" used in your first run is no longer the latest. This situation arises when you rerun a build using the original run's revision, which is no longer the HEAD or latest in the branch.

However, if you initiate a new build, Bamboo will use the latest revision, preventing the "detached HEAD state" message from appearing.

Kind regards,

Eduardo Alvarenga  
Atlassian Support APAC

**--please don't forget to Accept the answer if the reply is helpful--**