#!/bin/bash

# === Step 1: 用户输入项目名 ===
echo "请输入 GitHub 上的仓库名称(将自动创建):"
read repo_name

# === Step 2: 用户输入 GitHub 用户名 ===
echo "请输入你的 GitHub 用户名:"
read github_user

# === Step 3: 输入 GitHub Token(不会显示)===
read -s -p "请输入 GitHub Token(只显示一次): " github_token
echo ""

# === Step 4: 初始化 Git 仓库 ===
git init
echo "node_modules/" >> .gitignore  # 可选,添加你自己的规则
git add .
git commit -m "Initial commit"

# === Step 5: 用 GitHub API 创建远程仓库 ===
response=$(curl -s -H "Authorization: token $github_token" \
    -d "{\"name\":\"$repo_name\"}" \
    https://api.github.com/user/repos)

# 检查是否成功创建仓库
if echo "$response" | grep -q "\"full_name\""; then
    echo "GitHub 仓库创建成功:$repo_name"
else
    echo "❌ 创建仓库失败,返回信息如下:"
    echo "$response"
    exit 1
fi

# === Step 6: 添加远程地址并推送 ===
git remote add origin https://github.com/$github_user/$repo_name.git
git branch -M main
git push -u origin main

echo "✅ 已成功推送到 GitHub 仓库:https://github.com/$github_user/$repo_name"

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐