Git 项目维护命令指南

GitHub 仓库Robot-Nav
适用场景:日常代码管理、版本控制、团队协作


目录


一、上传项目到 GitHub

1.1 方式一:本地已有项目上传(推荐)

Step 1:进入项目目录并初始化 Git

cd "f:\lenovo\lenovo\Desktop\项目汇总\LCM-MPPI"
git init

Step 2:添加所有文件到暂存区

git add .

Step 3:提交文件

git commit -m "first commit: 初始化项目"

Step 4:在 GitHub 网页上创建仓库

Step 5:关联远程仓库并推送

git remote add origin https://github.com/Robot-Nav/LCM-MPPI.git
git branch -M main
git push -u origin main

1.2 方式二:先创建空仓库再克隆推送

Step 1:在 GitHub 网页上创建空仓库

Step 2:克隆空仓库到本地

git clone https://github.com/Robot-Nav/LCM-MPPI.git
cd LCM-MPPI

Step 3:复制项目文件到该目录

Step 4:添加提交推送

git add .
git commit -m "first commit"
git push origin main

1.3 方式三:先克隆原仓库,再添加新项目

适用于想保留原项目历史的情况:

git clone https://github.com/Robot-Nav/RC-ESDF-2D.git
cd RC-ESDF-2D
git remote add new-origin https://github.com/Robot-Nav/LCM-MPPI.git
git push new-origin --all
git push new-origin --tags

1.4 处理大文件(>100 MB)—— 使用 Git LFS

如果项目中包含超过 100 MB 的单个文件(如模型权重、数据集、视频等),直接推送会被 GitHub 拒绝。此时必须使用 Git LFS (Large File Storage)

Step 1:安装 Git LFS

# Ubuntu/Debian
sudo apt-get install git-lfs

# CentOS/RHEL
sudo yum install git-lfs

# 或使用官方脚本
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
sudo apt-get install git-lfs

Step 2:在仓库中初始化 LFS

cd your-project
git lfs install

Step 3:指定要由 LFS 管理的文件类型

  • 跟踪所有文件(不推荐,会浪费存储):
    git lfs track "*"
    
  • 只跟踪特定大文件类型(推荐)
    git lfs track "*.zip" "*.7z" "*.rar"
    git lfs track "*.bin" "*.model" "*.pth"
    git lfs track "*.jpg" "*.png" "*.mp4"
    

执行后会自动生成 .gitattributes 文件。

Step 4:提交 .gitattributes 并添加大文件

git add .gitattributes
git add your-large-file.zip
git commit -m "Add large files with Git LFS"

Step 5:正常推送

git push origin main

注意:GitHub 免费账户提供 1 GB 存储空间1 GB/月流量。超出需要购买数据包或使用其他方案。

1.5 首次推送的认证问题

GitHub 已于 2021 年 8 月 13 日起 禁止使用账户密码进行 Git 操作,必须使用 Personal Access Token (个人访问令牌)SSH 密钥

方法一:使用 Token(推荐初次使用)
  1. 生成 Token:

    • GitHub 网页 → 右上角头像 → Settings
    • 左侧底部 → Developer settingsPersonal access tokensTokens (classic)
    • 点击 Generate new token (classic)
    • Note:任意填写,如 my-push-token
    • Expiration:建议 90 天或自定义
    • Scopes:勾选 repo(完整仓库控制权)
    • 点击 Generate token立即复制并保存(只显示一次)
  2. 推送时使用 Token:

    git push -u origin main
    
    • Username:输入你的 GitHub 用户名(如 Robot-Nav
    • Password粘贴刚才生成的 Token(不是 GitHub 登录密码)
方法二:使用 SSH(一劳永逸)
  1. 生成 SSH 密钥对(如果还没有):
    ssh-keygen -t ed25519 -C "your_email@example.com"
    
  2. 复制公钥:
    cat ~/.ssh/id_ed25519.pub
    
  3. 添加到 GitHub:
    • GitHub 网页 → SettingsSSH and GPG keysNew SSH key
  4. 修改本地仓库远程地址为 SSH 格式:
    git remote set-url origin git@github.com:Robot-Nav/your-repo.git
    
  5. 此后推送无需再输入密码。

1.6 什么是 .gitignore

.gitignore 是一个文本文件,用来告诉 Git 哪些文件或文件夹不要跟踪、不要提交到版本库。在项目根目录下创建 .gitignore 文件,每行写一个忽略规则,Git 就会自动跳过这些文件。

常见需要忽略的文件:

  • 编译产物:*.o*.exebuild/dist/
  • 依赖目录:node_modules/vendor/__pycache__/
  • 环境配置:.env*.localconfig.local.yaml
  • IDE 配置:.vscode/.idea/*.swp
  • 系统文件:.DS_StoreThumbs.db
  • 大文件或临时文件:*.zip*.tar.gz*.log

示例 .gitignore 文件:

# 编译产物
build/
*.o
*.exe

# Python 缓存
__pycache__/
*.pyc

# 环境配置
.env
*.local

# IDE
.vscode/
.idea/

# 系统文件
.DS_Store
Thumbs.db

# 大文件(已用 LFS 跟踪的可以忽略)
*.zip
*.tar.gz

最佳实践:在 git init 之后、git add . 之前,先创建 .gitignore 文件并提交,避免误将不需要的文件加入版本控制。

1.7 完整的大项目上传流程示例(含大文件处理)

以下是一个完整的、包含 Git LFS 处理大文件、配置用户信息、解决认证问题的命令序列:

# 1. 进入项目目录
cd ~/AlphaInsight

# 2. 初始化 Git 仓库
git init

# 3. 配置用户信息(若全局未配置)
git config user.name "YourName"
git config user.email "your@email.com"

# 4. 安装并初始化 Git LFS
sudo apt-get install git-lfs   # Ubuntu
git lfs install

# 5. 跟踪大文件(例如跟踪所有 .zip 和 .pth 文件)
git lfs track "*.zip" "*.pth"

# 6. 添加并提交 .gitattributes
git add .gitattributes
git commit -m "Track large files with Git LFS"

# 7. 添加所有项目文件
git add .
git commit -m "Initial commit: full project"

# 8. 关联远程仓库(使用 HTTPS)
git remote add origin https://github.com/Robot-Nav/AlphaInsight-.git

# 9. 推送(会提示输入用户名和 Token)
git push -u origin main

1.8 后续更新代码

git add .
git commit -m "更新说明:本次修改的内容"
git push origin main

1.9 推送 Tags(发布版本)

git tag -a v1.0.0 -m "版本1.0.0"
git push origin v1.0.0
git push origin --tags

二、基础配置

2.1 设置用户名和邮箱

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

2.2 查看配置

git config --list
git config user.name
git config user.email

2.3 设置默认分支名

git config --global init.defaultBranch main

2.4 设置拉取策略

git config --global pull.rebase false

三、初始化与克隆

3.1 在当前目录初始化新仓库

git init

3.2 克隆远程仓库

git clone https://github.com/Robot-Nav/RC-ESDF-2D.git
git clone https://github.com/Robot-Nav/LCM-MPPI.git

3.3 克隆指定分支

git clone -b branch_name https://github.com/Robot-Nav/repo_name.git

3.4 克隆到指定目录

git clone https://github.com/Robot-Nav/repo_name.git ./target_folder

四、日常开发流程

4.1 查看当前状态

git status
git status -s

4.2 添加文件到暂存区

git add filename.txt
git add file1.cpp file2.h
git add .
git add -A

4.3 提交更改

git commit -m "提交信息:本次修改的内容"
git commit -m "feat: 添加新功能"
git commit -m "fix: 修复bug"
git commit -m "docs: 更新文档"

例如:

cd ~/DRL_ws

# 第一次提交(项目初始化)
git commit -m "chore: 初始化DRL_ws项目"

# 添加新功能
git commit -m "feat: 添加DQN算法实现"

# 修复bug
git commit -m "fix: 修正经验回放采样错误"

# 更新文档
git commit -m "docs: 添加训练参数说明"

# 性能优化
git commit -m "perf: 优化神经网络前向传播速度"

# 代码重构
git commit -m "refactor: 重构奖励函数模块"

4.4 一次性添加并提交

# 日常开发流程
git add .                    # 添加所有(包括新文件)
git commit -m "feat: xxx"    # 提交

# 或者快速修复
git commit -am "fix: xxx"    # 只修改了现有文件时

4.5 查看提交历史

git log                              # 显示完整提交历史
git log --oneline                    # 每个提交显示一行(简洁模式)
git log --oneline -10                # 只显示最近10个提交
git log --graph --oneline --all      # 图形化显示所有分支的提交历史
git log --author="name"              # 只显示指定作者的提交
git log --since="2026-01-01"         # 显示2026年1月1日之后的提交
git log --until="2026-12-31"         # 显示2026年12月31日之前的提交

五、分支管理

5.1 查看分支

git branch                         # 列出本地所有分支(当前分支用*标记)
git branch -a                      # 列出所有分支(包括本地+远程)
git branch -r                      # 只列出远程分支
git branch -v                      # 列出本地分支并显示最后一次提交的信息

5.2 创建新分支

git branch feature_branch
git branch develop

5.3 切换分支

git checkout branch_name            # 切换到指定分支(传统命令,Git 2.23之前)
git switch branch_name              # 切换到指定分支(新命令,Git 2.23+推荐)

5.4 创建并切换到新分支

git checkout -b new_branch
git switch -c new_branch

5.5 重命名分支

git branch -m old_name new_name      # 重命名本地分支(如果新名称已存在会报错)
git branch -M main                   # 强制重命名当前分支为 main(覆盖已存在的同名分支)

5.6 删除分支

git branch -d branch_name             # 安全删除分支(已合并的分支才能删除)
git branch -D branch_name             # 强制删除分支(不管是否合并,危险)

5.7 合并分支

git merge branch_name                 # 将指定分支合并到当前分支(默认快速合并)
git merge --no-ff branch_name         # 禁用快速合并,强制创建合并提交(保留分支历史)

5.8 查看分支差异

git diff branch1..branch2
git diff main..feature_branch

六、远程仓库操作

6.1 添加远程仓库

git remote add origin https://github.com/Robot-Nav/repo_name.git
git remote add upstream https://github.com/original_owner/repo_name.git

6.2 查看远程仓库

git remote -v
git remote show origin

6.3 修改远程仓库地址

git remote set-url origin https://github.com/Robot-Nav/repo_name.git

6.4 重命名远程仓库

git remote rename origin upstream

6.5 推送代码到远程

git push origin main
git push -u origin branch_name
git push origin --all
git push origin --tags

6.6 推送 tags

git push origin v1.0.0
git push origin --follow-tags

6.7 从远程拉取代码

git pull origin main
git pull
git pull --rebase origin main

6.8 获取远程更新(不合并)

git fetch origin
git fetch --all

6.9 删除远程分支

git push origin --delete branch_name

6.10 设置上游分支

git branch --set-upstream-to=origin/branch_name
git branch -u origin/branch_name

七、查看与比较

7.1 查看工作区与暂存区差异

git diff
git diff filename.txt

7.2 查看暂存区与上次提交的差异

git diff --cached
git diff --staged

7.3 查看两次提交之间的差异

git diff commit1_id..commit2_id
git diff HEAD~1..HEAD

7.4 查看某个文件的历史

git log -p filename.txt
git log --follow -p filename.txt

7.5 查看谁修改了某行代码

git blame filename.txt
git blame -L 10,20 filename.txt

7.6 查看当前 HEAD 指向

git rev-parse HEAD

7.7 查看简略统计信息

git diff --stat

八、撤销与回退

8.1 撤销工作区的修改(未 add)

git checkout -- filename.txt
git restore filename.txt
git restore .

8.2 取消暂存(已 add,未 commit)

git reset HEAD filename.txt
git restore --staged filename.txt
git reset HEAD

8.3 回退到上次提交(未 push)

git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
参数 工作区 暂存区 说明
--soft 不变 不变 保留更改在暂存区
--mixed 不变 清空 默认选项,清空暂存区
--hard 清空 清空 危险!丢弃所有更改

8.4 回退到指定提交(未 push)

git reset --hard commit_id
git reset --hard HEAD~3

8.5 撤销已推送的提交

git revert commit_id
git revert HEAD~1..HEAD

8.6 丢弃本地所有修改

git checkout -- .
git restore .
git reset --hard HEAD

九、标签管理

9.1 创建标签

git tag v1.0.0
git tag -a v1.0.0 -m "版本1.0.0发布"
git tag -a v1.0.0 commit_id

9.2 查看标签

git tag
git tag -l "v1.*"
git show v1.0.0

9.3 删除标签

git tag -d v1.0.0
git push origin --delete v1.0.0

9.4 推送标签到远程

git push origin v1.0.0
git push origin --tags

十、其他常用命令

10.1 储藏工作区更改

git stash
git stash save "暂存信息"
git stash -u
git stash -a

10.2 查看储藏列表

git stash list

10.3 恢复储藏

git stash apply
git stash apply stash@{0}
git stash pop

10.4 删除储藏

git stash drop
git stash drop stash@{0}
git stash clear

10.5 清理未跟踪文件

git clean -f
git clean -fd
git clean -n

10.6 搜索提交历史

git log --grep="keyword"
git log -S "code_content"

10.7 创建压缩包

git archive main --format=zip --output=archive.zip

10.8 查看忽略的文件

git status --ignored

十一、常见问题解决

11.1 合并冲突

git merge branch_name
git status

手动编辑冲突文件,然后:

git add filename.txt
git commit -m "解决冲突:合并branch_name"

11.2 取消正在进行的 rebase

git rebase --abort

11.3 继续 rebase

git rebase --continue

11.4 cherry-pick

git cherry-pick commit_id
git cherry-pick commit_id1..commit_id2

11.5 更新 fork 仓库

git remote add upstream original_repo_url
git fetch upstream
git merge upstream/main

11.6 修改最后一次提交

git commit --amend
git commit --amend -m "新提交信息"
git commit --amend --no-edit

11.7 查看远程仓库信息

git remote show origin

11.8 设置代理(如需要)

git config --global http.proxy http://proxy.example.com:8080
git config --global https.proxy https://proxy.example.com:8080

11.9 忽略文件权限变化

git config core.fileMode false

附录:Robot-Nav 项目推荐工作流

A.1 首次克隆项目

git clone https://github.com/Robot-Nav/RC-ESDF-2D.git
cd RC-ESDF-2D

A.2 日常开发流程

git checkout -b feature/your_feature
git add .
git commit -m "feat: 描述本次修改"
git push -u origin feature/your_feature

A.3 合并到 main 分支

git checkout main
git pull origin main
git merge feature/your_feature
git push origin main
git branch -d feature/your_feature

A.4 发布新版本

git tag -a v1.0.0 -m "版本1.0.0"
git push origin v1.0.0
git push origin --tags

文档版本:v2.0
*更新日期:2026-05-22

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐