15

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.

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.

Community
  • 1
  • 1
user2088807
  • 1,378
  • 2
  • 25
  • 47
  • Are you aware of Posh Git? https://github.com/dahlbyk/posh-git – Mark Wragg Mar 20 '17 at 12:41
  • posh-git wouldn't help in any way shape or form – 4c74356b41 Mar 20 '17 at 12:43
  • Reading your code example, you are already in your repo. That means posh-git would help like mentioned by @Mark. (e.g. `Get-GitDirectory`) – Clijsters Mar 20 '17 at 15:50
  • Possible duplicate of [How can I display my current git branch name in my PowerShell prompt?](https://stackoverflow.com/questions/1287718/how-can-i-display-my-current-git-branch-name-in-my-powershell-prompt) – Michael Freidgeim Jun 16 '17 at 06:21
  • If one uses posh-git, would one not then need to install it also on every server and developer machine where the PS-script should be run? Ok for personal use, but... – Svein Terje Gaup Nov 29 '19 at 09:09

3 Answers3

26

CD Your repository folder

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

from Show just the current branch in Git

Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
1

Try Join-Path

$gitRepo = Join-Path $currentPath ".." -Resolve
4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • Thank you for your answer, I know have a new error (i've tried with and without the quotes around $gitRepo in the call of git-cmd. – user2088807 Mar 20 '17 at 12:27
  • why do you want to execute that command in the root of the repo? – 4c74356b41 Mar 20 '17 at 12:40
  • To get the name of the current branch. If it's not master I want to checkout master and pull master. – user2088807 Mar 20 '17 at 12:44
  • so why not just checkout master and pull it? checkout to master would do no bad if you are on master @user2088807 also, why not just use `git status -b` – 4c74356b41 Mar 20 '17 at 12:50
0

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
Cengkuru Michael
  • 4,590
  • 1
  • 33
  • 33