使用PowerShell的Git命令

时间:2013-05-02 06:19:41

标签: git powershell

如何将git的默认目录作为当前powershell脚本的位置

这是 git-init.ps1

的内容

C:/Users/MyUser/Desktop/Project101/git-init.ps1

我想初始化此路径 C:/ Users / MyUser / Desktop / Project101 /

但问题是git会初始化此路径 c:/Users/MyUser/.git 我只是按照Git Bash的命令

任何帮助都会受到重视。 TIA

function run-git
{
    # location of my installed git
    $x = Get-ChildItem env:LOCALAPPDATA | %{ $_.Value }
    $y = $x + "\Programs\Git\bin\sh.exe"

    # .$y --login -i -c "git init"
    # .$y --login -i -c "git remote add -t 'master' 'origin' 'E:/ProjectRepository/Project101'"
    # .$y --login -i -c "git pull"

    # get the location of this script
    $Invocation = (Get-Variable MyInvocation -Scope 1).Value
    $p = Split-Path $Invocation.MyCommand.Path

    .$y --login -i -c "cd $p"
    .$y --login -i -c "git init"
    # .$y --login -i -c "git init 'E:/WorkingDirectory'"
    .$y --login -i -c "git status"
}

run-git;

1 个答案:

答案 0 :(得分:0)

问题在于你做这样的一行:

.$y --login -i -c "cd $p"

您正在运行shell,更改目录,然后退出shell。新的位置丢失了。

我建议使用

Push-Location $p

# Do git commands here, remember that git spews verbose on stderr and causes powershell to barf

Pop-Location

另一个选择是使用git --work-tree="$p"或类似的东西,但这种方式就是疯狂。

相关问题