一、在 GitHub 上创建私有仓库
-
打开 GitHub官网 并登录。
-
点击右上角的 “+” → 选择 “New repository”。
-
填写以下内容:
- Repository name:仓库名称,例如 my-private-repo。
- Description:可选,仓库描述。
- Visibility:选择 Private(私有)。
-
点击 “Create repository”。
二、本地操作:安装并配置 Git
如果还没有安装 Git:
- Windows:Git官网下载安装
- macOS:可用 Homebrew:brew install git
- Linux:使用包管理器,如 sudo apt install git
配置 Git 用户信息(只需一次):
git config --global user.name "你的GitHub用户名"
git config --global user.email "你的GitHub邮箱"
三、上传本地项目到 GitHub 私有仓库
假设你已经在本地有一个项目文件夹(比如 my-project):
方法一:已有代码目录
cd my-project
git init # 初始化Git仓库
git remote add origin https://github.com/你的用户名/你的仓库名.git
git add .
git commit -m "初次提交"
git branch -M main
git push -u origin main
注意:如果你开启了 GitHub 的 2FA,需要使用 Personal Access Token 替代密码。
四、从 GitHub 私有仓库克隆到本地
在终端或命令行中执行:
git clone https://github.com/你的用户名/你的仓库名.git
如果是私有仓库,你需要在输入用户名和密码时提供:
- 用户名:你的 GitHub 用户名
- 密码:Personal Access Token
五、后续上传和更新代码
在已经初始化的 Git 仓库目录下,继续提交和上传:
git add .
git commit -m "你的更新说明"
git push
小技巧(推荐)
使用 SSH 而不是 HTTPS,可以避免每次输入用户名密码:
生成 SSH 密钥:ssh-keygen -t rsa -b 4096 -C “你的邮箱”
添加到 GitHub:添加SSH公钥
使用 SSH 地址:git@github.com:用户名/仓库名.git