title: Git problems in Azure DevOps Pipeline (CI)
body: <p>I'm trying to update the version in a .nuspec and .PSD1 file and add a comment to README.MD during the build.</p>
  <p>I've been following <a href="https://medium.com/@jsrice7391/creating-a-nuget-build-pipeline-for-powershell-modules-within-vsts-cb75e06161e0#bafa" rel="nofollow noreferrer">this</a> very helpful article that promises to deliver this goal, however, I wonder if the author, <a href="https://stackoverflow.com/users/6891245/justin-rice">Justin Rice</a> is making assumptions, or more likely, I don't have a clue.</p>
  <p>I have two branches, master and develop, both local and remote. I commit and push to develop, which triggers the build.</p>
  <p>Below is his git script, which runs after another task has modified the three files:</p>
  <pre><code>git config --global user.email &quot;VssAdministrator@factoryvm-az459.(biib)&quot;
  git config --global user.name &quot;VSTS Admin&quot;
  ECHO SOURCE BRANCH IS %BUILD_SOURCEBRANCH%
  IF %BUILD_SOURCEBRANCH% == refs/heads/master (
     ECHO Building master branch so no merge is needed.
     EXIT
  )
  SET sourceBranch=origin/%BUILD_SOURCEBRANCH:refs/heads/=%
  ECHO ADDING CHANGES FROM PIPELINE
  git branch %sourceBranch%
  ECHO ADDING MODIFIED FILES
  git add --all
  ECHO CREATING COMMIT
  git commit -m &quot;Changes made from Pipeline&quot;
  ECHO CHECKING MASTER
  git reflog
  git checkout -b master HEAD@{0}
  git pull --strategy recursive -X theirs origin %BUILD_SOURCEBRANCH%
  git pull --strategy recursive -X ours origin master
  git add --all
  git commit -m &quot;Added from VSTS Pipeline&quot;
  git push origin master
  </code></pre>
  <p>The first line of issue is <code>git branch %sourceBranch%</code>. I already have a <code>develop</code> branch and <code>%sourceBranch%</code> resolves to <code>origin/develop</code>. The error is: <code>fatal: A branch named 'origin/develop' already exists</code>. I don't understand why it needs to create a new branch. I have tried with `git checkout origin/develop instead, but it doesn't help.</p>
  <p>The next line is <code>git checkout -b master HEAD@{0}</code>. Again, I have a master branch and I receive the error <code>fatal: A branch named 'master' already exists.</code>.</p>
  <p>I think this line is trying to create a new branch based off of the detached head (bc49db5) HEAD@{0}, but I don't really understand it. <a href="https://www.atlassian.com/git/tutorials/using-branches/git-checkout" rel="nofollow noreferrer">Another article</a> (See quote below) suggests it should be the other way around: <code>git checkout -b HEAD@{0} master</code></p>
  <p>I've been scratching my head over this one for several days now, but to no avail. If anyone can explain what is going on or what I am doing wrong, I'd be most grateful.</p>
  <p>P.S. The full output (With additional comments) is as follows:</p>
  <pre><code>git version 2.21.0.windows.1
  ====== listing branches ======
  * (HEAD detached at 504cd36)
    master
    origin/develop
    remotes/origin/develop
    remotes/origin/master
  SOURCE BRANCH IS refs/heads/develop
  
  ====== ADDING CHANGES FROM PIPELINE ======
  fatal: A branch named 'origin/develop' already exists.
  
  ====== ADDING MODIFIED FILES ======
  ====== COMMITTING ======
  [detached HEAD bc49db5] Changes made from Pipeline
   3 files changed, 17 insertions(+), 15 deletions(-)
   rewrite PSAdminTools.nuspec (75%)
  
  ====== CHECKING MASTER ======
  bc49db5 HEAD@{0}: commit: Changes made from Pipeline
  504cd36 HEAD@{1}: checkout: moving from master to 504cd363ed2a7a1d9fa81ccc3a1a51790393cc27
  04e8856 HEAD@{2}: reset: moving to HEAD
  04e8856 HEAD@{3}: pull --strategy recursive -X theirs origin refs/heads/develop: Fast-forward
  86ca92a HEAD@{4}: checkout: moving from 7cf28c87edc068fb13cf1a7db3f15592fa717da3 to master
  7cf28c8 HEAD@{5}: commit: Changes made from Pipeline
  04e8856 HEAD@{6}: checkout: moving from master to 04e88560932f8999c0f8c9a49c691901b381d036
  86ca92a HEAD@{7}: reset: moving to HEAD
  86ca92a HEAD@{8}: checkout: moving from a9362489e83f5b90e3b72fff990431bb6b3d9bd5 to master
  a936248 HEAD@{9}: commit: Changes made from Pipeline
  f85c694 HEAD@{10}: checkout: moving from master to f85c69464cdd0e9b87cd87628421e93168c5a604
  86ca92a HEAD@{11}: reset: moving to HEAD
  86ca92a HEAD@{12}: pull --strategy recursive -X theirs origin refs/heads/develop: Fast-forward
  390d32e HEAD@{13}: checkout: moving from 8be7772ba3f03789ae8db8626d4240333d35c576 to master
  8be7772 HEAD@{14}: commit: Changes made from Pipeline
  86ca92a HEAD@{15}: checkout: moving from master to 86ca92a7b1ced170344a7240e6b5df95cfee36d4
  390d32e HEAD@{16}: reset: moving to HEAD
  390d32e HEAD@{17}: checkout: moving from 90bee5c560baf51ba69bb199750b73344d41117a to master
  90bee5c HEAD@{18}: commit: Changes made from Pipeline
  390d32e HEAD@{19}: checkout: moving from master to 390d32e58f9306397c917f90f57c43a795a86c09
  
  ====== Checking out master =======
  fatal: A branch named 'master' already exists.
  
  ====== Pulling from their refs/heads/develop =======
  From https://blah.visualstudio.com/PSAdminTools/_git/PSAdminTools
   * branch            develop    -&gt; FETCH_HEAD
  Already up to date.
  
  ====== Pulling from ours master =======
  From https://blah.visualstudio.com/PSAdminTools/_git/PSAdminTools
   * branch            master     -&gt; FETCH_HEAD
  Already up to date.
  
  ====== Current branch ======
  * (HEAD detached from 504cd36)
    master
    origin/develop
    pipeline
  
  ====== Adding files ======
  ====== Committing to master =======
  HEAD detached from 504cd36
  nothing to commit, working tree clean
  
  ======= Pushing to master ======
  Everything up-to-date
  
  ======= Git Status =======
  HEAD detached from 504cd36
  nothing to commit, working tree clean
  </code></pre>
  <blockquote>
  <p>git checkout -b {new-branch} {existing-branch}</p>
  <p>By default git checkout -b will base the new-branch off the current HEAD. An optional additional branch parameter can be passed to git checkout. In the above example,  is passed which then bases new-branch off of existing-branch instead of the current HEAD.</p>
  </blockquote>
  <h2>Update 20/05/2019</h2>
  <p>I seem to have got it working, although, I understand what my changes are doing, I still don't understand why these change are needed. In other words, why does the script' author's code work and mine doesn't (presuming his works).</p>
  <p><strong>Change 1:</strong>
  <code>git branch %sourceBranch%</code> to <code>git branch -f %sourceBranch%</code></p>
  <p><strong>Change 2:</strong>
  <code>git checkout -b master HEAD@{0}</code> to <code>git checkout -B master HEAD@{0}</code></p>
  <p>My initial working state has two branches: <code>master</code> and <code>develop</code>, both local and remote.</p>
  <p>The output of <code>git pull --strategy recursive -X theirs origin %BUILD_SOURCEBRANCH%</code> still says <code>Already up to date.</code></p>
  <p>The output of <code>git commit -m &quot;Added from VSTS Pipeline&quot;</code> says <code>nothing to commit, working tree clean</code>.</p>
  <p>I'm not adding this update to the answer as although I'm unblocked, I still want to understand what is going on. i.e. I haven't answered my question.</p>