title: Why a VSTS build definition when "getting resources" creates a repository with no branches and detached?
body: <p>I have created a build definition in VSTS. I want to run a powerShell script right after the "get sources". </p>
  
  <p>While "getting sources" phase I can see that some branches are being created:</p>
  
  <pre><code>From https://domain.visualstudio.com/_git/main
  * [new branch]      develop           -&gt; origin/develop
  * [new branch]      master            -&gt; origin/master
  .......
  </code></pre>
  
  <p>Then it checks out the latest commit on a detached HEAD:</p>
  
  <pre><code>Checking out files: 100% (16011/16011), done.
  Note: checking out '2c5ac1a6dab64603656be2610dc5c2c627b916d4'.
  You are in 'detached HEAD' state. You can look around, make experimental
  changes and commit them, and you can discard any commits you make in this
  state without impacting any branches by performing another checkout.
  If you want to create a new branch to retain commits you create, you may
  do so (now or later) by using -b with the checkout command again. Example:
  git checkout -b &lt;new-branch-name&gt;
  HEAD is now at 2c5ac1a...  "Merge develop to master"
  </code></pre>
  
  <p>Then when it is time for my powerShell script there are no branches created, when I issue a </p>
  
  <pre><code>"git branch -v"
  </code></pre>
  
  <p>I see this : </p>
  
  <pre><code> 2016-11-30T10:42:26.6370529Z * (HEAD detached at 41e7eb4) 41e7eb4 first commit
  </code></pre>
  
  <p>Moreover, when I try to create checkout or create new branches in my powerShell script they appear as "red lines (errors)":</p>
  
  <pre><code>2016-11-30T10:42:26.6370529Z * (HEAD detached at 41e7eb4) 41e7eb4 first commit
  2016-11-30T10:42:27.0110730Z ##[error]Previous HEAD position was 41e7eb4... first commit
  2016-11-30T10:42:27.7881014Z ##[error]Switched to a new branch 'develop'
  2016-11-30T10:42:27.9670995Z ##[error] * [new branch]      test -&gt; test
  2016-11-30T10:42:28.1945915Z ##[error]Process completed with exit code 0 and     had 3 error(s) written to the error stream.
  2016-11-30T10:42:28.1955965Z ##[section]Finishing: PowerShell Script
  </code></pre>
  
  <p>Where are the rest of the branches created during the "get resources" phase? Why do I have a detached HEAD? And why I cannot checkout/create branches?</p>
  
  <p>Thanks,</p>
  
  <hr>
  
  <p><strong>--UPDATE--</strong>
  My build is able to create branches and push them directly to the remote. I needed to grant "contribute" and "branch creation" access to my build service account. However, I still get errors that make my build red, although branches and commits are actually created and pushed to the remote. </p>
  
  <p><strong>My script</strong> will be the following (I simpliefied it)(it is an inline PowerShell script):</p>
  
  <pre><code>git branch -a -v
  git fetch --all
  git branch -a
  git checkout -b master remotes/origin/master
  
  git checkout -b testBranch
  Out-File -FilePath auto-file.txt
  git add .
  git commit -m "just created an auto commit"
  git push -u origin testBranch
  </code></pre>
  
  <p><strong>And here is the corresponding Log:</strong></p>
  
  <pre><code>2016-12-09T08:05:14.9777018Z ##[section]Starting: Build
  2016-12-09T08:05:14.9897018Z Current agent version: '2.109.1'
  2016-12-09T08:05:15.6197698Z Downloading task: PowerShell
  2016-12-09T08:05:16.6935776Z ##[section]Starting: Get Sources
  2016-12-09T08:05:16.7755760Z Syncing repository: main (TfsGit)
  2016-12-09T08:05:16.7806005Z Prepending Path environment variable with    directory containing 'git.exe'.
  2016-12-09T08:05:16.7925998Z ##[command]git version
  2016-12-09T08:05:16.9935896Z ##[command]git init "C:\a\1\s"
  2016-12-09T08:05:17.0525625Z Initialized empty Git repository in C:/a/1/s/.git/
  2016-12-09T08:05:17.0565864Z ##[command]git remote add origin https://domain  .visualstudio.com/MyFirstProject/_git/main
  2016-12-09T08:05:17.0795632Z ##[command]git config gc.auto 0
  2016-12-09T08:05:17.1035633Z ##[command]git config --get-all http.https://domain   .visualstudio.com/MyFirstProject/_git/main.extraheader
  2016-12-09T08:05:17.1337092Z ##[command]git config --get-all http.proxy
  2016-12-09T08:05:17.1597344Z ##[command]git -c http.extraheader="AUTHORIZATION: bearer ********" fetch --tags --prune --progress origin
  2016-12-09T08:05:18.1333289Z remote: 
  2016-12-09T08:05:18.1343246Z remote:                    vSTs           
  2016-12-09T08:05:18.1343246Z remote:                  vSTSVSTSv        
  2016-12-09T08:05:18.1343246Z remote:                vSTSVSTSVST        
  2016-12-09T08:05:18.1343246Z remote: VSTS         vSTSVSTSVSTSV        
  2016-12-09T08:05:18.1343246Z remote: VSTSVS     vSTSVSTSV STSVS        
  2016-12-09T08:05:18.1343246Z remote: VSTSVSTSvsTSVSTSVS   TSVST        
  2016-12-09T08:05:18.1343246Z remote: VS  tSVSTSVSTSv      STSVS        
  2016-12-09T08:05:18.1343246Z remote: VS   tSVSTSVST       SVSTS        
  2016-12-09T08:05:18.1343246Z remote: VS tSVSTSVSTSVSts    VSTSV        
  2016-12-09T08:05:18.1343246Z remote: VSTSVST    SVSTSVSTs VSTSV        
  2016-12-09T08:05:18.1343246Z remote: VSTSv        STSVSTSVSTSVS        
  2016-12-09T08:05:18.1343246Z remote:                VSTSVSTSVST        
  2016-12-09T08:05:18.1343246Z remote:                  VSTSVSTs        
  2016-12-09T08:05:18.1343246Z remote:                    VSTs    (TM)        
  2016-12-09T08:05:18.1343246Z remote: 
  2016-12-09T08:05:18.1343246Z remote:  Microsoft (R) Visual Studio (R) Team Services        
  2016-12-09T08:05:18.1343246Z remote: 
  2016-12-09T08:05:18.2981730Z From https://domain  .visualstudio.com/MyFirstProject/_git/main
  2016-12-09T08:05:18.2981730Z  * [new branch]      develop    -&gt; origin/develop
  2016-12-09T08:05:18.2981730Z  * [new branch]      master     -&gt; origin/master
  2016-12-09T08:05:18.3001809Z ##[command]git checkout --progress --force 7af1f2611269277da2c520b84148844e650acc4f
  2016-12-09T08:05:18.3241819Z Note: checking out '7af1f2611269277da2c520b84148844e650acc4f'.
  2016-12-09T08:05:18.3241819Z 
  2016-12-09T08:05:18.3241819Z You are in 'detached HEAD' state. You can look around, make experimental
  2016-12-09T08:05:18.3241819Z changes and commit them, and you can discard any commits you make in this
  2016-12-09T08:05:18.3241819Z state without impacting any branches by performing another checkout.
  2016-12-09T08:05:18.3241819Z 
  2016-12-09T08:05:18.3241819Z If you want to create a new branch to retain commits you create, you may
  2016-12-09T08:05:18.3241819Z do so (now or later) by using -b with the checkout command again. Example:
  2016-12-09T08:05:18.3241819Z 
  2016-12-09T08:05:18.3241819Z   git checkout -b &lt;new-branch-name&gt;
  2016-12-09T08:05:18.3241819Z 
  2016-12-09T08:05:18.3241819Z HEAD is now at 7af1f26... try to fix cachedconfig issue
  2016-12-09T08:05:18.3261731Z ##[command]git config http.https://domain  .visualstudio.com/MyFirstProject/_git/main.extraheader "AUTHORIZATION: bearer ********"
  2016-12-09T08:05:18.3491840Z ##[section]Finishing: Get Sources
  2016-12-09T08:05:18.3511738Z ##[section]Starting: PowerShell Script
  2016-12-09T08:05:18.3811752Z ==============================================================================
  2016-12-09T08:05:18.3811752Z Task         : PowerShell
  2016-12-09T08:05:18.3811752Z Description  : Run a PowerShell script
  2016-12-09T08:05:18.3821755Z Version      : 1.2.1
  2016-12-09T08:05:18.3821755Z Author       : Microsoft Corporation
  2016-12-09T08:05:18.3821755Z Help         : [More Information](https://go.microsoft.com/fwlink/?LinkID=613736)
  2016-12-09T08:05:18.3821755Z ==============================================================================
  2016-12-09T08:05:18.4611894Z ##[command]. 'C:\Users\buildguest\AppData\Local\Temp\928a835e-178f-40c1-8ba5-61da96fc8fc7.ps1' 
  2016-12-09T08:05:19.1941140Z * (HEAD detached at 7af1f26) 7af1f26 try to fix cachedconfig issue
   2016-12-09T08:05:19.2081155Z   remotes/origin/develop 7af1f26 try to fix cachedconfig issue
   2016-12-09T08:05:19.2090954Z   remotes/origin/master  41e7eb4 first commit
   2016-12-09T08:05:19.2090954Z Fetching origin
   2016-12-09T08:05:19.5648482Z * (HEAD detached at 7af1f26)
   2016-12-09T08:05:19.5648482Z   remotes/origin/develop
   2016-12-09T08:05:19.5648482Z   remotes/origin/master
   2016-12-09T08:05:19.5909153Z ##[error]Previous HEAD position was 7af1f26... try to fix cachedconfig issue
   Switched to a new branch 'master'
  
   2016-12-09T08:05:19.5939144Z Branch master set up to track remote branch master from origin.
   2016-12-09T08:05:19.6609174Z ##[error]Switched to a new branch 'testBranch'
  
   2016-12-09T08:05:19.6609174Z [testBranch 847ac8b] just created an auto commit
   2016-12-09T08:05:19.6619147Z  Committer: buildguest &lt;buildguest&gt;
   2016-12-09T08:05:19.6619147Z Your name and email address were configured automatically based
   2016-12-09T08:05:19.6619147Z on your username and hostname. Please check that they are accurate.
   2016-12-09T08:05:19.6619147Z You can suppress this message by setting them explicitly. Run the
   2016-12-09T08:05:19.6619147Z following command and follow the instructions in your editor to edit
   2016-12-09T08:05:19.6619147Z your configuration file:
   2016-12-09T08:05:19.6619147Z 
   2016-12-09T08:05:19.6619147Z     git config --global --edit
   2016-12-09T08:05:19.6619147Z 
   2016-12-09T08:05:19.6619147Z After doing this, you may fix the identity used for this commit with:
   2016-12-09T08:05:19.6619147Z 
   2016-12-09T08:05:19.6619147Z     git commit --amend --reset-author
   2016-12-09T08:05:19.6619147Z 
   2016-12-09T08:05:19.6619147Z  1 file changed, 1 insertion(+)
   2016-12-09T08:05:19.6619147Z  create mode 100644 auto-file.txt
   2016-12-09T08:05:20.4268088Z ##[error]To https://domain .visualstudio.com/MyFirstProject/_git/main
  
   2016-12-09T08:05:20.4268088Z Branch testBranch set up to track remote branch testBranch from origin.
   2016-12-09T08:05:20.6018082Z ##[error] * [new branch]      testBranch -&gt; testBranch
  
   2016-12-09T08:05:20.8482358Z ##[error]Process completed with exit code 0 and had 4 error(s) written to the error stream.
   2016-12-09T08:05:20.8492359Z ##[section]Finishing: PowerShell Script
   2016-12-09T08:05:20.8502361Z ##[section]Starting: Post Job Cleanup
   2016-12-09T08:05:20.8562360Z Cleaning any cached credential from repository: main (Git)
   2016-12-09T08:05:20.8602360Z ##[command]git config --unset-all http.https://domain .visualstudio.com/MyFirstProject/_git/main.extraheader
   2016-12-09T08:05:20.8872360Z ##[command]git remote set-url origin https://domain  .visualstudio.com/MyFirstProject/_git/main
   2016-12-09T08:05:20.9142348Z ##[command]git remote set-url --push origin https://domain .visualstudio.com/MyFirstProject/_git/main
   2016-12-09T08:05:20.9342662Z ##[section]Finishing: Post Job Cleanup
   2016-12-09T08:05:20.9352355Z ##[section]Finishing: Build
  </code></pre>