命令方式創(chuàng)建遠(yuǎn)程代碼倉(cāng)庫(kù)
- https://docs.github.com/rest/reference/repos#create-a-repository-for-the-authenticated-user
- https://docs.github.com/en/developers/apps/scopes-for-oauth-apps
- https://github.com/settings/tokens
- https://docs.github.com/
- Next.js 入門教程[TypeScript] https://github.com/jimboyeah/Next.js-Tutorials
Next.js 服務(wù)端渲染教程
將本地代碼 push 到 github 遠(yuǎn)程代碼倉(cāng)庫(kù)之前,總是通過(guò)在線新建 github 代碼倉(cāng)庫(kù)的操作是比較麻煩的。
借助 Github API,可以利用命令創(chuàng)建遠(yuǎn)程代碼倉(cāng)庫(kù),十分便捷。
首先需要申請(qǐng)并獲取自己的 API Token,用于鑒權(quán),OAuth scope requirements。
-
public_reposcope orreposcope to create a public repository -
reposcope to create a private repository
通過(guò) API Token 可以在 OAuth 應(yīng)用中安全訪問(wèn)需要授權(quán)的 API 資源,以下可以檢測(cè)什么類型的 OAuth 作用域是可用的:
$ curl -H "Authorization: token YOUR_OAUTH-TOKEN" https://api.github.com/users/codertocat -I
HTTP/1.1 200 OK
X-OAuth-Scopes: repo, user
X-Accepted-OAuth-Scopes: user
對(duì)于遠(yuǎn)程創(chuàng)建倉(cāng)庫(kù),檢查其中已經(jīng)授權(quán)的 API 列表 X-OAuth-Scopes 和 X-Accepted-OAuth-Scopes 兩項(xiàng)。
勾選 repo 支持倉(cāng)庫(kù)的完全控制,具體參考 Scopes for OAuth Apps:
-
repo:statusAccess commit status -
repo_deploymentAccess deployment status -
public_repoAccess public repositories -
repo:inviteAccess repository invitations -
security_eventsRead and write security events
然后在本機(jī)使用腳本簡(jiǎn)化遠(yuǎn)程創(chuàng)建倉(cāng)庫(kù)的過(guò)程,在 Windows 系統(tǒng)上可以使用以下腳本:
@ECHO off
SETLOCAL
set user=yyyyyy
set token=xxxxx
set repo=%1
GOTO :RUN
:ERR
echo Error occurs %errorlevel% !
GOTO :END
:RUN
IF "%repo%" == "" (
echo Script [%0] need a repository name for github!
echo Example: %0 demo-repository
GOTO :END
)
IF "%repo%" == "user" (
curl -u %user%:%token% https://api.github.com/user
GOTO :END
)
IF "%repo%" == "demo" (
curl -H "Authorization: token %token%" https://api.github.com/repos/%user%/%repo%
GOTO :END
)
IF "%repo%" == "test" (
curl -H "Authorization: token %token%" https://api.github.com/users/codertocat -I
rem curl -v -H "Authorization: %token% TOKEN" https://api.github.com/repos/octodocs
GOTO :END
)
IF not "%repo%" == "" (
echo Script [%0] ready to create an new repository [%repo%] for github!
curl -u '%user%:%token%' -H "Authorization: token %token%" https://api.github.com/user/repos -d "{\"name\":\"%repo%\"}"
rem curl -u '%user%:%token%' -H "Accept: application/vnd.github.v3+json" https://api.github.com/user/repos -d "{\"name\":\"%repo%\"}"
if not %errorlevel% == 0 (
GOTO :ERR
)
git remote add origin git@github.com:%user%/%repo%.git
echo Done with:
echo https://github.com/%user%/%repo%.git
)
:END
ENDLOCAL
如 bash 的配置文件中加入下述函數(shù)定義:
github-create()
{if [ $1 ]
then
repo_name=$1
else
repo_name=`basename $(pwd)`
echo "set Repo name to ${repo_name}"
fi
curl -u 'username:api_token' https://api.github.com/user/repos -d '{"name":"'$repo_name'"}'
git remote add origin git@github.com:username/$repo_name.git
}
注意,需要使用自己的 username 與 api_token 覆蓋上述函數(shù)中相應(yīng)的值。
創(chuàng)建代碼倉(cāng)庫(kù)只需輸入命令:
github-create repo_name
如果沒(méi)有指定倉(cāng)庫(kù)名稱 repo_name,會(huì)自動(dòng)將當(dāng)前路徑的文件夾名稱設(shè)置為代碼倉(cāng)庫(kù)的名稱。然后,就可以將本地代碼倉(cāng)庫(kù) push 到遠(yuǎn)程代碼倉(cāng)庫(kù):
git push -u origin master
