Ubuntu本地文件上传github(版本控制)
文章目录
- 步骤 1:准备工作
- 步骤 2:创建 GitHub 仓库
- 步骤 3:初始化本地仓库
- 步骤 4:关联远程仓库
- 步骤 5:提交代码
- 步骤 6:验证结果
- other
- 1.查看所有关联的远程仓库
- 2:查看指定远程仓库的详细信息
- 常见问题处理
步骤 1:准备工作
1. 创建 GitHub 账号(如果已有可跳过)
github.com
2. 安装 Git
sudo apt-get install git
3. 配置 Git 用户信息
git config --global user.name "你的GitHub用户名"
git config --global user.email "你的GitHub注册邮箱"
步骤 2:创建 GitHub 仓库
登录 GitHub,点击右上角 ➕ 选择 New repository\
填写仓库信息:
Repository name: 项目名称(建议英文)
Description: 项目描述(可选)
选择 Public(公开)或 Private(私有)
不要勾选 Initialize this repository…
点击 Create repository
步骤 3:初始化本地仓库
1. 进入项目目录
cd /path/to/your/project
2. 初始化 Git 仓库
git init
3. 创建 .gitignore 文件(可选)
示例:忽略 Python 项目的虚拟环境、缓存文件等
echo "__pycache__/" >> .gitignore
echo ".env" >> .gitignore
echo "*.log" >> .gitignore
步骤 4:关联远程仓库
1. 获取远程仓库地址
在 GitHub 仓库页面点击 Code 按钮
选择 SSH 或 HTTPS 地址(推荐 SSH)
https://github.com/2572870097/unitree.git
2. 添加远程仓库地址
git remote add origin git@github.com:你的用户名/仓库名.git
步骤 5:提交代码
1. 添加所有文件到暂存区
git add .
2. 或指定特定文件
git add file1.txt file2.py
3. 提交到本地仓库
git commit -m "第一次提交:项目初始化"
4. 推送到远程仓库
git push -u origin main
如果 GitHub 默认分支是 master,则使用:
git push -u origin master
步骤 6:验证结果
刷新 GitHub 仓库页面,确认文件已上传
查看提交历史:
git log
other
1.查看所有关联的远程仓库
git remote -v
输出示例:
origin git@github.com:yourname/repo.git (fetch)
origin git@github.com:yourname/repo.git (push)
upstream git@github.com:official/repo.git (fetch)
upstream git@github.com:official/repo.git (push)
-v 参数:显示详细的远程仓库 URL
origin:默认远程仓库名称
upstream:常见用于跟踪源仓库的别名
2:查看指定远程仓库的详细信息
git remote show origin
输出示例:
- remote origin
Fetch URL: git@github.com:yourname/repo.git
Push URL: git@github.com:yourname/repo.git
HEAD branch: main
Remote branches:
main tracked
dev tracked
Local branch configured for ‘git pull’:
main merges with remote main
Local ref configured for ‘git push’:
main pushes to main (fast-forwardable)
常见问题处理
1. 显示 No such remote 'origin’
(1) 检查是否存在拼写错误
git remote -v
(2)如果确实未关联,需要先添加
git remote add origin 仓库地址
(3)修改已关联的远程地址
git remote set-url origin 新仓库地址
(4)删除无效的远程仓库
git remote remove upstream