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
相关推荐
u1301301 小时前
GitHub 热榜项目:日榜(2026-10-27)
人工智能·github
zroooo2 小时前
GitHub Flow
github
粥里有勺糖3 小时前
视野修炼-技术周刊第137期 | 编程已经面目全非
前端·github·ai编程
miofly3 小时前
GitHub 今日推荐|rea:用 MCP 把反编译器和 AI agent 接到一起的逆向工具
typescript·开源·github
MicrosoftReactor4 小时前
技术速递|超越一刀切:为 GitHub Copilot CLI 和 VS Code 实现决策路由
vscode·ai·github·copilot·决策路由
m4Rk_4 小时前
【论文阅读】Agent 记忆机制(113):Agentic Memory Management——从“检索相关信息”走向“管理 Agent 内部记忆”
论文阅读·人工智能·学习·开源·github
技术达芬奇5 小时前
git bundle介绍
github
暖心hebbie设计学习6 小时前
GitHub 热榜拆解(10.28)|5 个 AI 工具:从设计到写代码,附 issue 区坑点
人工智能·github
逛逛GitHub6 小时前
看这 11 个案例,AI 做游戏真的越来越顶了。
github
前端小万6 小时前
想做个人副业别乱买服务器!我的 5 个月真实开销分享
前端·后端·github