GitHub代码推送指南

🚀 使用说明:将本地代码推送到 GitHub 仓库的 main 分支

本文档介绍如何从一个全新的目录开始,初始化 Git 仓库,并将本地代码推送到 GitHub 仓库的 main 分支。


前提条件

  1. 已安装 Git for Windows (可在命令行输入 git --version 验证)。

  2. 已在 GitHub 上创建一个空仓库,例如:

    复制代码
    https://github.com/username/example.git

操作步骤

1. 初始化本地仓库

进入本地项目目录,执行:

复制代码
git init

2. 添加远程仓库地址

复制代码
git remote add origin https://github.com/username/example.git

3. 创建并切换到 main 分支

复制代码
git checkout -b main

4. 配置提交者信息(只需设置一次)

复制代码
git config --global user.name "username"
git config --global user.email "username@gmail.com"

5. 添加代码并提交

复制代码
git add .
git commit -m "Initial commit: add example source code"

6. 拉取远程 main 分支(合并 GitHub 上的 README 等文件)

复制代码
git pull --rebase origin main

7. (如有需要)再次添加和提交

如果第 5 步已经提交过,可以跳过。

复制代码
git add -A
git commit -m "Add project source code"

8. 推送到远程 main 分支

复制代码
git push -u origin main

验证推送结果

  1. 打开 GitHub 仓库页面。

  2. 确认 main 分支下已经显示本地上传的代码。


✅ 至此,代码已成功推送到 GitHub 的 main 分支。

以后你只需执行以下三步就能更新代码:

复制代码
git add .
git commit -m "Update code"
git push
相关推荐
崔庆才丨静觅4 小时前
Claude Code GitHub Actions 使用教程
github·api·claude
砖厂小工1 天前
用 GLM + OpenClaw 打造你的 AI PR Review Agent — 让龙虾帮你审代码
android·github
程序员鱼皮1 天前
又一个新项目完结,我要出海了!
ai·github·开源项目
徐小夕1 天前
pxcharts-vue:一款专为 Vue3 打造的开源多维表格解决方案
前端·vue.js·github
Moment1 天前
想要长期陪伴你的助理?先从部署一个 OpenClaw 开始 😍😍😍
前端·后端·github
我叫黑大帅1 天前
前端如何利用 GitHub Actions 自动构建并发布到 GitHub Pages?
前端·面试·github
HelloGitHub1 天前
这个年轻的开源项目,想让每个人都能拥有自己的专业级 AI 智能体
开源·github·agent
刘发财1 天前
弃用html2pdf.js,这个html转pdf方案能力是它的几十倍
前端·javascript·github