Search code examples
gitazure-devopsyaml

How to move files inside Azure Devops and retain history?


I moved some project to Azure Devops and I would like to move a file from folder1 to folder2 without losing the history of the file.

I tried by using this pipeline but the history was not copied with the file:

steps:
- checkout: self
  persistCredentials: true

- task: CopyFiles@2
  inputs:
    SourceFolder: 'folder1'
    Contents: 'file.txt'
    TargetFolder: '$(Build.SourcesDirectory)/folder2'

- script: |
   git config --global user.email "myemail@email.com"
   git config --global user.name "me"
   git remote update
   git fetch
   git checkout master
   git add $(Build.SourcesDirectory)/folder2/file.txt
   git status
   git commit -m "copy files"
   git push origin master


Solution

  • According to the Moving Files in Git Basics - Recording Changes to the Repository, you can use the git mv command to move files and keep the history.

    Sample yaml:

    steps:
    - checkout: self
      persistCredentials: true
    
    - script: |
       git config --global user.email "myemail@email.com"
       git config --global user.name "me"
       git remote update
       git fetch
       git checkout main
       git mv test.txt newfolder/test.txt
       git status
       git commit -m "Moved test.txt to new directory"
       git push origin main
    

    Result: we can find the history of the file.

    enter image description here