Get name of branch into code


I have a question about passing the branch name to my code as a string.

So we are using a git repository and the branch number also refers to the staging environment where the build is placed. Meaning, if my branch is 001, my url is

I am writing automated tests which are executed on the staging environment of the branch. My question is, is it possible to pass the branch number to my code so I can make it part of the URL I want to test on?

I am using visual studio 2017, selenium and specflow.


I actually found a great solution which perfectly works. Sharing so in the future, others can use it too if they need to.

ProcessStartInfo startInfo = new ProcessStartInfo("git.exe");

startInfo.UseShellExecute = false;
startInfo.WorkingDirectory = "dir Here";
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.Arguments = "rev-parse --abbrev-ref HEAD";

Process process = new Process();
process.StartInfo = startInfo;

string branchname = process.StandardOutput.ReadLine();

Answered By – Anand

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published