尽管语法似乎正确,gitignore文件被忽略

zbdgwd5y  于 2022-10-23  发布在  Git
关注(0)|答案(1)|浏览(227)

我写了一些powershell代码,因为我想实现两件事。第一个是组织我的注册表,第二个是学习ps1脚本语法。直到现在,我想我一方面做了一些令人印象深刻的事情,另一方面我陷入了困境。。。
我读了很多关于git的有趣而复杂的文章,在早些时候,我也成功地使用了.gitignore文件。
现在这个特殊的脚本每次都会忽略.gitignore,这完全不取决于我在做什么。
希望你们中有人能帮我解开这个谜团,为什么这个脚本总是忽略我的gitignore-它很糟糕,每次它都会在任何地方上传自己(这就是为什么它会在40 ++行中写入.gitignore的原因),而且如果每次上传node_modules时,它都会变得更糟糕
(在apps根文件夹中新生成的npx create-react-app testapp上测试此脚本)
顺便说一句,为了让事情更容易,我还使用git add .之前的第88行中的git rm -r --cached .清理了缓存,这也不起作用
文件名:git-create-repo.ps1
源代码:

$workdir = Convert-Path $pwd

$gituser = "l00tz1ffer"
$gitserver = "lootziffers-welt.de"
$defaultRemoteName = "origin"
$targetBranchName = "master"

$gitHubExists = 0

$workDirName = $workdir.Substring(($workdir.LastIndexOf("\") + 1), ($workdir.Length - 1 ) - $workdir.LastIndexOf("\"))

$git_dir_string = $workDirName + ".git"
echo $workDirName

git remote -v | Out-File -FilePath remotes.temp -Encoding utf8 -Append
$File = 'remotes.temp'

foreach ($line in Get-Content $File) {
    $remoteListingLine = $line
    $remoteHostName = $remoteListingLine.Substring(($remoteListingLine.IndexOf("@") + 1), ($remoteListingLine.LastIndexOf(":") - 1 ) - $remoteListingLine.IndexOf("@"))
    echo $remoteHostName
    if ($remoteHostName -contains "github.com") {
        echo "GitHub Repo found"
        $remoteListingLine = $remoteListingLine -replace $defaultRemoteName, "github"
        echo "Renaming properly ..."
        echo $remoteListingLine

        git remote rename $defaultRemoteName "github"
        $gitHubExists = 1

    }

}
Remove-Item 'remotes.temp'

if (-not (Test-Path -Path .gitignore)) {
    New-Item -Path '.gitignore' -ItemType File
}

If ( $workDirName -ne "git-create-repo" -and $workDirName -ne "git-create-repo.git") {
    $File = '.gitignore'

    foreach ($line in Get-Content $File) {
        if (-not (Test-Path -Path new.gitignore)) {
            New-Item -Path 'new.gitignore' -ItemType File
        }

        echo ".gitignore enthält folgenden Wert: $line" 
        if ($line -contains "git-create-repo.ps1") {
            echo "Duplicate entry found, Removing it"
        }
        elseif ($line.Length -eq 0) {
            echo "Empty Line Found in .gitignore -> Removing it"
        }
        elseif ($line -contains $null) {
            echo "Empty Line Found in .gitignore -> Removing it"
        }
        else {
            line | Out-File -FilePath new.gitignore -Encoding utf8 -Append

        }
    }

    Remove-Item '.gitignore'
    Rename-Item 'new.gitignore' '.gitignore'
    "git-create-repo.ps1" >> | Out-File -FilePath .gitignore -Encoding utf8 -Append

}

Start-Sleep -Seconds 3

git remote rm $defaultRemoteName
git branch -mv main $targetBranchName

ssh git@$gitserver "cd  $gituser && mkdir $git_dir_string && cd $git_dir_string && git init --bare"

# if (Test-Path -Path '.git' -PathType Container) {

git init
echo "Lokales Repo wurde Initialisiert"

git rm -r --cached .
git add .\.gitignore

$timestamp = (get-date).ToString('G')
git commit -m "Autogenerated Commit from ${[System.Environment]::UserName} -> Zeit: $timestamp"
echo "Autogenerated Commit -> Zeit: $timestamp"

git rm -r --cached .
git add .
echo "Dateien wurden zum Lokalen Repository hinzugefuegt"

# }

$timestamp = (get-date).ToString('G')
git commit -m "Autogenerated Commit from ${[System.Environment]::UserName} -> Zeit: $timestamp"
echo "Autogenerated Commit -> Zeit: $timestamp"

$git_repo_string = "git@lootziffers-welt.de:" + $gituser + "/" + $workDirName + ".git"
echo "Der Verwendete Remote Git Repo string lautet: $git_repo_string" 

git remote add $defaultRemoteName $git_repo_string
git push $defaultRemoteName $targetBranchName
if ($gitHubExists -eq 1) {
    git push github $targetBranchName
}

scp git@${gitserver}:${gituser}/repos.txt repos.txt
if (-not (Test-Path -Path .gitignore)) {
    New-Item -Path 'repos.txt' -ItemType File
}
$File = "repos.txt"
foreach ($line in Get-Content $File) {
    echo "repos.txt enthält folgenden Wert: $line" 
    if ($line -contains $git_repo_string) {
        echo "Duplicate entry found, Removing it"
    }
    elseif ($line.Length -eq 0) {
        echo "Empty Line Found in .gitignore -> Removing it"
    }
    elseif ($line -contains $null) {
        echo "Empty Line Found in .gitignore -> Removing it"
    }
    else {
        line | Out-File -FilePath new.repos.txt -Encoding utf8 -Append
    }
}
Remove-Item 'repos.txt'
Rename-Item 'new.repos.txt' 'repos.txt'

${git_repo_string}.ToString() | Out-File -FilePath repos.txt -Encoding utf8 -Append
scp repos.txt git@${gitserver}:${gituser}/repos.txt
Remove-Item 'repos.txt'

Start-Sleep -Seconds 5

在这里,我将向您介绍我的.gitignore文件的基本情况


# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies

/node_modules/*
node_modules/
/node_modules
/.pnp
.pnp.js

# testing

/coverage

# production

/build

# misc

.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
git-create-repo.ps1

感谢我亲爱的朋友们,感谢你们的快速支持,并花时间来概述这一庞大的代码。
真诚的

8tntrjer

8tntrjer1#

默认情况下,PowerShell 5以ASCII格式追加文本,git希望.gitignore使用UTF8格式。
你可以用两种方法解决这个问题。
1.从pwsh而不是powershell启动脚本,升级到PowerShell Core(6+)。pwsh将启动PowerShell核心。
1.使用"text" | Out-File -append -Encoding UTF8 -filepath .gitignore代替"text" >> .gitignore

相关问题