本地项目Git化并推送到GitHub私有仓库
jerry2 CTO

这篇记录一次把本地项目接入 Git,并推送到 GitHub 私有仓库的完整流程。重点不是背命令,而是把真实操作中容易卡住的地方讲清楚:远程仓库怎么建、token 什么时候粘贴、403 怎么判断、upstream 提示该怎么处理。

1. 本地项目初始化 Git

进入项目根目录:

1
cd /path/to/your/project

先确认当前目录,避免在错误路径初始化仓库:

1
pwd

创建 .gitignore,把依赖、构建产物、私有配置排除掉。比如微信小程序项目可以先放这些:

1
2
3
4
5
6
7
node_modules/
miniprogram_npm/
project.private.config.json
.DS_Store
*.log
dist/
coverage/

初始化仓库并提交:

1
2
3
4
git init
git branch -M main
git add .
git commit -m "Initial commit"

检查状态:

1
2
git status --short
git log --oneline -1

如果 git status --short 没有输出,说明工作区是干净的。

2. 创建 GitHub 私有仓库

在 GitHub 页面新建仓库:

  • Repository name:项目名,例如 wechat-web
  • Visibility:选择 Private
  • 不要勾选 README、.gitignore、license

因为本地已经有首个提交,远程仓库保持空仓库最省事。

创建完成后,在本地配置远程地址:

1
2
git remote add origin https://github.com/你的用户名/仓库名.git
git remote -v

如果已经配错了远程地址,可以改掉:

1
git remote set-url origin https://github.com/你的用户名/仓库名.git

3. 用 token 推送代码

GitHub 现在不支持用账号密码推送代码,HTTPS 推送需要 Personal Access Token。

推荐生成 classic token:

  1. 打开 GitHub Settings -> Developer settings -> Personal access tokens。
  2. 选择 Generate new token,建议使用 classic token。
  3. 勾选 repo 权限。
  4. 生成后立即复制 token。

注意:token 只显示一次,不要发给别人,也不要写进聊天、文档或代码里。

在终端里这样保存凭据:

1
2
3
4
5
6
7
8
9
10
11
12
cd /path/to/your/project

printf "protocol=https\nhost=github.com\nusername=你的用户名\n\n" | git credential reject
printf "protocol=https\nhost=github.com\n\n" | git credential reject

read -s GITHUB_TOKEN
# 执行到这里时,终端会停住。此时粘贴 token,然后按回车。
# 因为用了 -s,屏幕不会显示任何字符,这是正常的。

printf "protocol=https\nhost=github.com\nusername=你的用户名\npassword=$GITHUB_TOKEN\n\n" | git credential approve

git push --set-upstream origin main

这里最容易误会的是 read -s GITHUB_TOKEN。它不是报错,也不是卡死,而是在等你输入 token。正确顺序是:执行 read 后,再粘贴 token,再按回车。

4. 常见报错处理

could not read Username

报错类似:

1
fatal: could not read Username for 'https://github.com': Device not configured

这说明 Git 没拿到可用的 GitHub 凭据。重新清理旧凭据,再写入 token:

1
2
printf "protocol=https\nhost=github.com\nusername=你的用户名\n\n" | git credential reject
printf "protocol=https\nhost=github.com\n\n" | git credential reject

然后重新执行 read -s GITHUB_TOKENgit credential approve

Write access to repository not granted

报错类似:

1
2
remote: Write access to repository not granted.
fatal: unable to access 'https://github.com/xxx/xxx.git/': The requested URL returned error: 403

这通常表示 token 有效,但没有这个仓库的写权限。

如果是 classic token,确认勾选了 repo 权限。

如果是 fine-grained token,确认:

  • Repository access 选中了目标仓库
  • Contents 权限是 Read and write
  • Metadata 权限至少是 Read-only

Invalid username or token

报错类似:

1
2
remote: Invalid username or token. Password authentication is not supported for Git operations.
fatal: Authentication failed

这通常表示 token 无效、复制不完整、已经过期,或终端里保存的是旧 token。建议撤销旧 token,重新生成一个 classic token,并重新写入凭据。

提示设置 upstream

如果看到:

1
2
3
To push the current branch and set the remote as upstream, use

git push --set-upstream origin main

直接按提示执行:

1
git push --set-upstream origin main

成功后,本地 main 会追踪远程 origin/main,以后可以直接使用:

1
2
git push
git pull

5. token 泄露后怎么办

如果 token 被贴到了聊天、截图、文档或公开网页里,就要把它当成已经泄露处理。

正确做法:

  1. 立刻打开 GitHub token 管理页面。
  2. Revoke 或 Delete 泄露的 token。
  3. 重新生成一个新 token。
  4. 重新写入本机 Git 凭据。

不要继续使用已经泄露的 token。哪怕仓库是私有的,token 泄露也可能导致仓库内容被读取或篡改。

6. 完整命令模板

下面是一次从本地初始化到推送的最小模板:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cd /path/to/your/project

git init
git branch -M main
git add .
git commit -m "Initial commit"

git remote add origin https://github.com/你的用户名/仓库名.git

printf "protocol=https\nhost=github.com\nusername=你的用户名\n\n" | git credential reject
printf "protocol=https\nhost=github.com\n\n" | git credential reject

read -s GITHUB_TOKEN
# 在这里粘贴 token,然后按回车

printf "protocol=https\nhost=github.com\nusername=你的用户名\npassword=$GITHUB_TOKEN\n\n" | git credential approve

git push --set-upstream origin main

这套流程跑通后,一个本地项目就完成了 Git 初始化、远程仓库绑定和首次推送。后续日常开发只需要:

1
2
3
git add .
git commit -m "你的提交说明"
git push
 评论