如何将本地项目提交到 GitHub

目录

1、首先在 GitHub 上创建一个新的仓库:

java 复制代码
1. 登录 GitHub
2. 点击右上角 "+" 号
3. 选择 "New repository"
4. 填写仓库名称
5. 不要勾选 "Initialize this repository with a README"
6. 点击 "Create repository"

2、在本地项目中初始化 Git:

java 复制代码
# 进入项目目录
cd your-project-path

# 初始化 Git
git init

# 添加所有文件到暂存区
git add .

# 创建第一次提交
git commit -m "first commit"

3、添加远程仓库并推送:

java 复制代码
# 添加远程仓库(替换 USERNAME 和 REPO-NAME)
git remote add origin https://github.com/USERNAME/REPO-NAME.git

# 将本地 main 分支推送到远程
git branch -M main
git push -u origin main

4、如果你想忽略某些文件不提交,创建 .gitignore 文件:

java.DS_Store 复制代码
node_modules
/dist

# local env files
.env.local
.env.*.local

# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*

# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

5、如果遇到推送失败,可能需要:

java 复制代码
# 设置 Git 用户信息
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# 如果需要设置 token 认证(推荐)
# 1. 在 GitHub 设置中生成 Personal Access Token
# 2. 使用 token 替换密码进行认证

6、常用的 Git 命令:

java# 复制代码
git status

# 查看分支
git branch

# 创建并切换分支
git checkout -b feature-name

# 合并分支
git merge feature-name

# 拉取远程更新
git pull origin main

# 查看提交历史
git log

7、如果想将现有的远程仓库克隆到本地:

java 复制代码
git clone https://github.com/USERNAME/REPO-NAME.git
相关推荐
dong_junshuai3 小时前
每天一个开源项目#56 reverse-skill:11K Stars 的安全 Agent 路由器
github
逛逛GitHub3 小时前
3 个最近在 GitHub 上非常火的项目,最后一个有创意。
github
TunerT_TQ8 小时前
Valhalla 静态工程审阅 #012|Hertz 源码证据驱动评测【大厂开源基础设施特辑】
测试工具·微服务·开源·github·字节跳动·cloudwego·http框架
一次旅行8 小时前
fzf+ripgrep+fd终端三合一实战:一套检索工具链,大幅提升大型项目开发效率
人工智能·python·github
Dear~yxy8 小时前
HAproxy企业级实战
github
栈溢出的浪漫20 小时前
国内MCP工具推荐:AIbase宣布推出MCP资源网站
github·开发者·mcp·aibase·资源网站
一可米21 小时前
gitHub.com Actions自动化发布
运维·自动化·github
小弥儿1 天前
GitHub今日热榜 | 2026-08-01:硬件安全工具与AI教育并行
人工智能·学习·开源·github
梦想三三1 天前
LangChain Output Parser 实战:从字符串到结构化数据的完整指南
android·服务器·langchain·github·uv