Get the name of the current git branch with PowerShell

I would like to be able to do git commands from a PowerShell script which is not located in the same folder as my git repo.

First I want to check the current branch, and if it’s not master try to checkout master and pull master.

  • Jenkins Git plugin trying to build old revision during release build
  • git pull with “--work-tree” flag fails
  • I can't clone repository with AWS CodeCommit. What does it mean?
  • Git checkout doesn't seem to be working
  • Force git custom merge driver for the purpose of merging pom.xml (maven) files
  • Clone the latest version of a git repository through https
  • What I’ve done so far:

    function Get-ScriptDirectory {
        Split-Path $script:MyInvocation.MyCommand.Path
    }
    
    $currentPath = Get-ScriptDirectory
    [string]$Path_GIT = "C:\Program Files\Git\git-cmd.exe"
    $gitRepo = $currentPath + "\..\"
    $nameCurrentBranch = & $Path_GIT git -C "$gitRepo" rev-parse --abbrev-ref HEAD
    

    From the documentation here and the answers to this question.

    $gitRepo contains the path of the folder containing the git repo.

    I get the error:

    git-cmd.exe : fatal: Cannot change to 'C:\Users\MyName\Documents\Projects\MyProject\
    Batchs\.." rev-parse --abbrev-ref HEAD': Invalid argument
    At C:\Users\MyName\Documents\Projects\MyProject\Batchs\Publish.ps1:64 char:22
    + ... entBranch = & $Path_GIT git -C "$gitRepo" rev-parse --abbrev-ref HEAD ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (fatal: Cannot c...nvalid argument:String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError

    EDIT: New problem after the proposition of @4c74356b41 (using Join-Path):
    I don’t have any error a popup opens and close really fast and then my powershell script is stuck, as if it was waiting for the git-cmd.exe to close. But I can’t see the windows of the cmd git and can’t close it.

  • How to keep ignored files when switching to branch not ignoring them?
  • Build Plan error when depending on git package with extra-deps
  • npm can't install any packages and doesn't show any errors
  • How to view git checkout tags history?
  • Is it OK to integrate changes into release branch?
  • Git corrupted repo: how to pick a git object from a clean repository
  • 3 Solutions collect form web for “Get the name of the current git branch with PowerShell”

    CD Your repository folder

    $branch= &git rev-parse --abbrev-ref HEAD 
    

    from Show just the current branch in Git

    Try Join-Path

    $gitRepo = Join-Path $currentPath ".." -Resolve
    

    Use psget to install the posh-git plugin that shows you the current branch. If you have the latest powershell, chances are that psget is already installed. If you dont have it then get it by using this command.

    (new-object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1") | iex
    

    To install posh-git use the command:

    Install-Module posh-git
    
    Git Baby is a git and github fan, let's start git clone.