Skip to content

How to clone a git repository and switch to a branch using Powershell/Ansible in Windows?

Create a new folder which would be the parent folder for your code.

New-Item -Path "D:\" -Name "source" -ItemType "directory" 

Navigate to the parent folder.

cd D:\source

Set SSL verify as false to avoid/ignore SSL errors.

git config --global http.sslVerify "false" 

Clone your repository.

git clone https://username:passowrd@bitbucket.org/repository-name/repo.git 

Switch to your branch.

git checkout branch-name 

That’s it. When you want to invoke this script in an Ansible playbook, you can use win_shell task to accomplish it.

Full code:

name: clone repository
win_shell: |
        New-Item -Path "D:\" -Name "source" -ItemType "directory"
        cd D:\source
        git config --global http.sslVerify "false"
        git clone https://username:passowrd@bitbucket.org/repository-name/repo.git 
        git checkout branch-name