Remote: Repository not found. fatal: repository https://github... not found

I am having problems creating a git repository automatically, using powershell and git. Can this be done? Could you help me?
Here is what I am doing:

mkdir <new directory>
cd <new directory>
git init
<drop a file into new directory>
git add . 
git commit -m "Autogenerated repo"
$url = "https://$$user/$repo.git"
git remote add origin $url
git remote set-url origin $url
git push -u origin master 

I get the error:
remote: Repository not found.
fatal: repository ‘https://github…’ not found

BTW, PAT has full repo access rights and can upload code into existing repo just fine.
Mario C.

The repository must exist on Github before you can push to it. If you’d like to automate that you should be able to do that using the API. As far as I know there are tools that wrap that API in a command line or library for you, but I’m not familiar with them.

1 Like

@mariokart229 ,

As @airtower-luna mentioned, before you can push to the remote repository, this remote repository must have been created in the specified organization account or user account, and generally this remote repository is empty that does not contains any files.
For example, on the web page, an empty remote repository looks like this:

If you want to use a shell script to automatically create the empty remote repository and push some files to it, instead of manually creating the repository. You can run the “Create an organization repository” or “Create a repository for the authenticated user” API at first in the script.

Hi brightran,
thank you for your reply. I do not see an empty repository in your supplied image. The image has the same code for creating the repo as the one I have tried, with the exception of the additional “git branch -M master”. I have tried adding it. Results are the same. As you and airtower-luna suggest, API could work. I have yet to see it do so.

Thank you for your reply. I am trying to get it to work via the API. Here is what I am trying:

    curl.exe -H @{"Authorization"="token $GHPAT"}$GHUser/repos `
             -d @{"name"="$GHRepo"}

Results in:
> curl.exe : % Total % Received % Xferd Average Speed Time Time Time Current
> At C:\Users\extcloft\Desktop\Azure To Git Backup\migrate.ps1:39 char:5
> + curl.exe -H @{“Authorization”=“token $GHPAT”}
*> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
> + CategoryInfo : NotSpecified: ( % Total % … Time Current:String) [], RemoteException
> + FullyQualifiedErrorId : NativeCommandError

No repo is created.

Other stuff tried, yielding same or other errors:

curl.exe -u ($GHUser+":"+$GHPAT)$GHUser/repos -d "name"="$GHRepo"

curl -H @{"Authorization"="token $GHPAT"}$GHUser/repos -d @{"name"="$GHRepo"}

$CurlArgument = '-H', "@{'Authorization'='token $GHPAT'}", 
                    '-X', 'POST',
                    '-d', '@{"name"="$GHRepo"}'
    $CURLEXE = 'C:\Program Files\Git\mingw64\bin\curl.exe'
    & $CURLEXE @CurlArgument
$form = @{
        H  = @{'Authorization'='token $GHPAT'}
       d  = @{"name"="$GHRepo"}
 $result = Invoke-WebRequest -Uri '$GHUser/repos' -Method Post -Body $form

Found solution

    $authHeader= New-Object 'System.Collections.Generic.Dictionary[String,String]'
    $authHeader.Add("Authorization", "token $GHPAT")

    #list repos
    #curl -H $authHeader -uri
    $body = '{"name":"'+"$GHRepo"+'"}'
    $reply = Invoke-RestMethod -H $authHeader -uri -Method Post -body $body