1

如何将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;
4

1 回答 1

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"或类似的东西,但这种方式很疯狂。

于 2013-07-14T17:46:01.277 回答