我正在编写一个脚本来自动初始化、添加项目并将其推送到 GitHub,以自学一些脚本编写。我收到一些错误,但其中一个错误似乎使脚本无法工作:
error: src refspec main does not match any
error: failed to push some refs to 'https://github.com/fakename/repo_name'
我已经查过了,似乎脚本认为没有分支可以将文件推送到,但是我确实有必要的代码来创建远程存储库和要推送到的分支。这是我的代码:
# get target file name
FILE="usr/theowner/Programming/Testing/github_script/repo_name"
f="$(basename "$FILE")"
# make a github repo using the filename
gh repo create "$f" --private
echo Created repo for "$f"!
#add file within folder to now established repo
echo
cd "$f"
git init
git config user.email [email protected]
git add .
git config user.name fakename
git commit -m Committed using gh-repo.sh
git remote add origin https://github.com/fakename/"$f"
git branch -M main
git checkout -b main
git push origin main
echo Pushed files to "$f"!
这是我遇到的另外两个错误,但尽管有这些错误,脚本的其余部分似乎仍然运行:
error: pathspec 'using' did not match any file(s) known to git
error: pathspec 'gh-repo.sh' did not match any file(s) known to git
git commit -m
接受一个参数——消息。其他参数 (using gh-repo.sh
) 被解释为文件。要解决此问题,请使用双(或单)引号将消息作为一个参数传递:
git commit -m "Committed using gh-repo.sh"
错误“src refspec main does not match any”出现在
git branch -M main
和git checkout -b main
之后,因为这两个命令都无法创建分支——存储库中当前没有提交,而分支是指向提交的指针。一旦你修复了git commit -m "Message"
,就会有一个提交,脚本的其余部分应该可以正常工作。