Github使用记录

之前训练模型的代码,不小心误删了,有一段时间没用github了,有一些指令忘了。这里记录了vscode与github的关联,方便后续代码管理。

一、本地项目(win系统)与Github关联

前提

  • 已安装 Git 并配置环境变量(路径:D:\Program Files (x86)\Git\bin + D:\Program Files (x86)\Git\cmd)

  • 已配置 Git 身份(用户名 / 邮箱):

    bash 复制代码
    git config --global user.name "用户名"
    git config --global user.email "邮箱"
  • GitHub 已创建空仓库(如 https://github.com/Data-MzL/TopoDyn.git)

操作步骤:

  1. 初始化本地 Git 仓库:

    bash 复制代码
    git init
  2. 关联远程 GitHub 仓库(若提示 origin already exists,先执行 git remote remove origin):

    bash 复制代码
    git remote add origin https://github.com/Data-MzL/TopoDyn.git
  3. 验证是否关联成功

    bash 复制代码
    git remote -v  # 输出 origin 对应仓库地址即成功

二、本地文件上传到 GitHub(分 2 种场景)

  • 场景 1:上传全部文件

    bash 复制代码
    # 1. 添加所有文件到暂存区
    git add .
    
    # 2. 提交本地修改(备注自定义)
    git commit -m "Initial upload: 上传所有项目文件"
    
    # 3. 拉取远程 LICENSE(避免冲突)
    git pull origin main --allow-unrelated-histories
    
    # 4. 推送到 GitHub
    git push -u origin main
  • 场景 2:仅上传指定文件(如 test.py

    bash 复制代码
    # 1. 清空暂存区(关键!避免残留文件)
    git reset
    
    # 2. 只添加目标文件
    git add test.py
    
    # 3. 提交(仅包含 test.py)
    git commit -m "仅上传 test.py 文件"
    
    # 4. 推送到 GitHub
    git push origin main

三、从 GitHub 删除文件(保留本地文件)

  • 场景 1:删除单个文件(如 Topo_Predictor.py)

    bash 复制代码
    # 1. 从 Git 跟踪中删除(本地文件保留)
    git rm --cached Topo_Predictor.py
    
    # 2. 提交删除操作
    git commit -m "删除 GitHub 上的 Topo_Predictor.py"
    
    # 3. 推送删除结果到 GitHub
    git push origin main
  • 场景 2:批量删除多个文件

    bash 复制代码
    # 1. 批量删除(按需修改文件名)
    git rm --cached TpOT.ipynb readme.md run_log.txt *.png
    # 2. 提交删除
    git commit -m "批量删除误传的多余文件"
    # 3. 推送删除结果
    git push origin main
  • 场景 3:删除整个文件夹(如 Papers/)

    bash 复制代码
    git rm --cached -r Papers/  # -r 表示递归删除文件夹
    git commit -m "删除 GitHub 上的 Papers 文件夹"
    git push origin main
  • 关键补充:避免误传文件的终极方案(.gitignore)

    创建 .gitignore 文件,让 Git 自动忽略无需上传的文件 / 文件夹:

    bash 复制代码
    # 1. 创建 .gitignore
    New-Item .gitignore -ItemType File
    
    # 2. 写入忽略规则(按需添加)
    Add-Content .gitignore @"
    # 忽略日志文件
    run_log.txt
    
    # 忽略图片
    *.png
    
    # 忽略指定文件
    Topo_Predictor.py
    TpOT.ipynb
    
    # 忽略指定文件夹
    Papers/
    Results/
    "@
    
    # 3. 提交并推送 .gitignore
    git add .gitignore
    git commit -m "添加 .gitignore 避免误传文件"
    git push origin main

四、总结

  1. 关联核心:git init → git remote add origin 完成本地与远程绑定,git remote -v 验证。
  2. 上传核心:指定文件上传需先 git reset 清空暂存区,再 git add 单个文件;全部上传用 git add .。
  3. 删除核心:用 git rm --cached 仅删除 GitHub 上的文件,保留本地文件,提交后推送生效。

所有命令均可直接复制到 PowerShell 执行,按场景选择即可,若遇到权限 / 冲突问题,优先检查 Git 身份配置和远程仓库关联状态。

相关推荐
GoGeekBaird11 小时前
从 Prompt Engineering 到 Loop Engineering,我觉得 AI 开发这事儿终于开始变味了
后端·github
aosky12 小时前
一台电脑配置多个 SSH Key 对应不同的 GitHub 账号
运维·ssh·github
YuePeng18 小时前
凌晨 3 点告警群炸了,我用浏览器干了原本 XShell 才能干的事
后端·github
QN1幻化引擎19 小时前
自注意力机制 20 年了,我们终于让它学会"压缩记忆"
github
程序员柒叔19 小时前
Hermes Agent 一周动态-2026-W24
人工智能·github·agent·openclaw·hermes
专注VB编程开发20年1 天前
通义比GITHUB Copilot差了10倍
github·copilot
驯龙高手_追风1 天前
Gitlab本地服务器搭建及配置-详细教程
git·github
逛逛GitHub1 天前
在你微信里用爽 Claude Code,我的开源 Skill 又更新了。
github
qq_白羊座1 天前
Linux 压缩 / 解压(tar)命令 + 参数详解
linux·运维·github
LeoZY_1 天前
CH347应用 USB转JTAG功能之:probe-rs搭配CH347下载MCU命令全指南
单片机·嵌入式硬件·mcu·开源·github