Git初始化本地文件夹,并推送到远端

推荐先在 GitHub、GitLab 或 Gitee 创建一个空仓库,不要勾选自动创建 README,然后执行:

bash 复制代码
# 1. 进入本地项目
cd /path/to/project

# 2. 初始化 Git(如果已经是 Git 仓库则跳过)
git init

# 3. 提交本地文件
git add .
git commit -m "Initial commit"

# 4. 将默认分支命名为 main
git branch -M main

# 5. 关联远程仓库
git remote add origin <远程仓库地址>

# 例如 HTTPS
git remote add origin https://github.com/username/project.git

# 或 SSH
git remote add origin git@github.com:username/project.git

# 6. 检查远程地址
git remote -v

# 7. 首次推送
git push -u origin main

以后更新只需:

bash 复制代码
git add .
git commit -m "说明本次修改"
git push

如果提示 origin already exists:

bash 复制代码
git remote set-url origin <新的远程仓库地址>

如果远程仓库已经包含 README 等提交,先同步:

bash 复制代码
git pull --rebase origin main
git push -u origin main

推送前建议检查:

bash 复制代码
git status

确保 .env、密码、API Key、编译产物等已经写入 .gitignore。使用 HTTPS 时通常需要访问令牌,而不是账户密码;使用 SSH 时需要先配置 SSH 密钥。

相关推荐
赵民勇5 小时前
Git命令详解
git
不老刘1 天前
Antigravity 的 New Worktree 模式:给 AI 代理一个安全的“平行工位”
人工智能·git·ai编程·antigravity
光谱SpectrAI1 天前
Windows 也中招:Git Bash 和 Node 仍信任泄露的 eMudhra 证书
git
YIN_尹1 天前
【git原理与使用】git初识与安装
大数据·git·elasticsearch
Leo_Mr1 天前
Vue3 项目配置 Git 提交规范(企业通用方案:husky + commitlint + cz-git + lint-staged)
git
jyOverQ1 天前
Git rebase 和 merge 有什么区别?什么时候该用哪个?
git
晗夜未央1 天前
跨仓库 Skill 换台电脑就失效?用 Git remote 校验项目
人工智能·git·python
Lsetea1 天前
Git访问HTTPS证书不受信任:用URL级sslCAInfo限定私有CA范围
git·https·开发工具·ssl证书·ca证书
এ慕ོ冬℘゜2 天前
Git 基础入门|和 SVN 对比 + 四大区域 + 常用命令详解
git·svn
DevLoom_2 天前
macOS 命令行 curl/git 报 SSL 证书错误?eMudhra 事件完整修复指南
git·macos·ssl