Run Git commands in a script

VSTS | TFS 2018 | TFS 2017 | TFS 2015

For some workflows you need your build process to run Git commands. For example, after a CI build on a feature branch is done, the team might want to merge the branch to master.

Git.exe is available on hosted agents and on on-premises agents.

Enable scripts to run Git commands

Grant version control permissions to the build service

Go to the Version Control control panel tab ▼

  • VSTS: https://{your-account}{your-team-project}/_admin/_versioncontrol
  • On-premises: https://{your-server}:8080/tfs/DefaultCollection/{your-team-project}/_admin/_versioncontrol

manage project

If you see this page, select the repo, and then click the link:

control panel top to team project

control panel team project version control tab

On the Version Control tab, select the repository in which you want to run Git commands, and then select Project Collection Build Service.


Grant permissions needed for the Git commands you want to run. Typically you'll want to grant:

  • Branch creation: Allow
  • Contribute: Allow
  • Read: Inherited allow
  • Tag creation: Inherited allow

When you're done granting the permissions, make sure to click Save changes.

Enable your build definition to run Git.exe

On the variables tab set this variable:

Name Value
system.prefergit true

On the options tab select Allow scripts to access OAuth token.

Make sure to clean up the local repo

Certain kinds of changes to the local repository are not automatically cleaned up by the build process. So make sure to:

  • Delete local branches you create.
  • Undo git config changes.

If you run into problems using an on-premises agent, to make sure the repo is clean:


List the files in your repo

Make sure to follow the above steps to enable Git.exe.

On the build tab add this step:

Task Arguments

Utility: Command Line
List the files in the Git repo.
Tool: git

Arguments: ls-files

Merge a feature branch to master

You want a CI build to merge to master if the build succeeds.

Make sure to follow the above steps to enable Git.exe.

On the Triggers tab select Continuous integration (CI) and include the branches you want to build.

Create merge.bat at the root of your repo:

@echo off
IF %BUILD_SOURCEBRANCH% == refs/heads/master (
   ECHO Building master branch so no merge is needed.
SET sourceBranch=origin/%BUILD_SOURCEBRANCH:refs/heads/=%
git checkout master
git status
git merge %sourceBranch% -m "Merge to master"
git status
git push origin
git status

On the build tab add this as the last step:

Task Arguments

Utility: Batch Script
Run merge.bat.
Path: merge.bat


Can I run Git commands if my remote repo is in GitHub or an external Git service such as Bitbucket?


Which steps can I use to run Git commands?

Batch Script

Command Line


Shell Script

How do I avoid triggering a CI build when the script pushes?

Add ***NO_CI*** to your commit message. For example, git merge origin/features/hello-world -m "Merge to master ***NO_CI***"

How does enabling scripts to run Git commands affect how the build process gets build sources?

When you set system.prefergit to true, the build process uses Git.exe instead of LibGit2Sharp to clone or fetch the source files.

Do I need an agent?

You need at least one agent to run your build or release. Get an agent.

I can't select a default agent queue and I can't queue my build or release. How do I fix this?

See Agent pools and queues.

I use TFS on-premises and I don't see some of these features. Why not?

Some of these features are available only on VSTS and not yet available on-premises. Some features are available on-premises if you have upgraded to the latest version of TFS.