Git 项目维护命令指南
GitHub 仓库 :Robot-Nav
适用场景:日常代码管理、版本控制、团队协作
目录
- [0. 上传项目到 GitHub](#0. 上传项目到 GitHub)
- [1. 基础配置](#1. 基础配置)
- [2. 初始化与克隆](#2. 初始化与克隆)
- [3. 日常开发流程](#3. 日常开发流程)
- [4. 分支管理](#4. 分支管理)
- [5. 远程仓库操作](#5. 远程仓库操作)
- [6. 查看与比较](#6. 查看与比较)
- [7. 撤销与回退](#7. 撤销与回退)
- [8. 标签管理](#8. 标签管理)
- [9. 其他常用命令](#9. 其他常用命令)
- [10. 常见问题解决](#10. 常见问题解决)
0. 上传项目到 GitHub
方式一:本地已有项目上传(推荐)
Step 1:进入项目目录并初始化 Git
bash
cd "f:\lenovo\lenovo\Desktop\项目汇总\LCM-MPPI"
git init
Step 2:添加所有文件到暂存区
bash
git add .
Step 3:提交文件
bash
git commit -m "first commit: 初始化项目"
Step 4:在 GitHub 网页上创建仓库
- 打开 https://github.com/new
- Repository name 填写
LCM-MPPI - 点击 Create repository
Step 5:关联远程仓库并推送
bash
git remote add origin https://github.com/Robot-Nav/LCM-MPPI.git
git branch -M main
git push -u origin main
方式二:先创建空仓库再克隆推送
Step 1:在 GitHub 网页上创建空仓库
Step 2:克隆空仓库到本地
bash
git clone https://github.com/Robot-Nav/LCM-MPPI.git
cd LCM-MPPI
Step 3:复制项目文件到该目录
Step 4:添加提交推送
bash
git add .
git commit -m "first commit"
git push origin main
方式三:先克隆原仓库,再添加新项目
适用于想保留原项目历史的情况:
bash
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
后续更新代码
bash
git add .
git commit -m "更新说明:本次修改的内容"
git push origin main
首次推送需要认证
首次推送时会要求输入 GitHub 用户名和 Personal Access Token(不是密码)。
生成 Token 步骤:
- 打开 GitHub → Settings
- 左侧底部找到 Developer settings
- Personal access tokens → Generate new token (classic)
- 勾选
repo权限 - 生成后复制使用
推送 Tags(发布版本)
bash
git tag -a v1.0.0 -m "版本1.0.0"
git push origin v1.0.0
git push origin --tags
1. 基础配置
设置用户名和邮箱
bash
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
查看配置
bash
git config --list
git config user.name
git config user.email
设置默认分支名
bash
git config --global init.defaultBranch main
设置拉取策略
bash
git config --global pull.rebase false
2. 初始化与克隆
在当前目录初始化新仓库
bash
git init
克隆远程仓库
bash
git clone https://github.com/Robot-Nav/RC-ESDF-2D.git
git clone https://github.com/Robot-Nav/LCM-MPPI.git
克隆指定分支
bash
git clone -b branch_name https://github.com/Robot-Nav/repo_name.git
克隆到指定目录
bash
git clone https://github.com/Robot-Nav/repo_name.git ./target_folder
3. 日常开发流程
查看当前状态
bash
git status
git status -s
添加文件到暂存区
bash
git add filename.txt
git add file1.cpp file2.h
git add .
git add -A
提交更改
bash
git commit -m "提交信息:本次修改的内容"
git commit -m "feat: 添加新功能"
git commit -m "fix: 修复bug"
git commit -m "docs: 更新文档"
一次性添加并提交
bash
git commit -am "提交信息"
查看提交历史
bash
git log
git log --oneline
git log --oneline -10
git log --graph --oneline --all
git log --author="name"
git log --since="2024-01-01"
git log --until="2024-12-31"
4. 分支管理
查看分支
bash
git branch
git branch -a
git branch -r
git branch -v
创建新分支
bash
git branch feature_branch
git branch develop
切换分支
bash
git checkout branch_name
git switch branch_name
创建并切换到新分支
bash
git checkout -b new_branch
git switch -c new_branch
重命名分支
bash
git branch -m old_name new_name
git branch -M main
删除分支
bash
git branch -d branch_name
git branch -D branch_name
合并分支
bash
git merge branch_name
git merge --no-ff branch_name
查看分支差异
bash
git diff branch1..branch2
git diff main..feature_branch
5. 远程仓库操作
添加远程仓库
bash
git remote add origin https://github.com/Robot-Nav/repo_name.git
git remote add upstream https://github.com/original_owner/repo_name.git
查看远程仓库
bash
git remote -v
git remote show origin
修改远程仓库地址
bash
git remote set-url origin https://github.com/Robot-Nav/repo_name.git
重命名远程仓库
bash
git remote rename origin upstream
推送代码到远程
bash
git push origin main
git push -u origin branch_name
git push origin --all
git push origin --tags
推送tags
bash
git push origin v1.0.0
git push origin --follow-tags
从远程拉取代码
bash
git pull origin main
git pull
git pull --rebase origin main
获取远程更新(不合并)
bash
git fetch origin
git fetch --all
删除远程分支
bash
git push origin --delete branch_name
设置上游分支
bash
git branch --set-upstream-to=origin/branch_name
git branch -u origin/branch_name
6. 查看与比较
查看工作区与暂存区差异
bash
git diff
git diff filename.txt
查看暂存区与上次提交的差异
bash
git diff --cached
git diff --staged
查看两次提交之间的差异
bash
git diff commit1_id..commit2_id
git diff HEAD~1..HEAD
查看某个文件的历史
bash
git log -p filename.txt
git log --follow -p filename.txt
查看谁修改了某行代码
bash
git blame filename.txt
git blame -L 10,20 filename.txt
查看当前HEAD指向
bash
git rev-parse HEAD
查看简略统计信息
bash
git diff --stat
7. 撤销与回退
撤销工作区的修改(未add)
bash
git checkout -- filename.txt
git restore filename.txt
git restore .
取消暂存(已add,未commit)
bash
git reset HEAD filename.txt
git restore --staged filename.txt
git reset HEAD
回退到上次提交(未push)
bash
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
| 参数 | 工作区 | 暂存区 | 说明 |
|---|---|---|---|
--soft |
不变 | 不变 | 保留更改在暂存区 |
--mixed |
不变 | 清空 | 默认选项,清空暂存区 |
--hard |
清空 | 清空 | 危险!丢弃所有更改 |
回退到指定提交(未push)
bash
git reset --hard commit_id
git reset --hard HEAD~3
撤销已推送的提交
bash
git revert commit_id
git revert HEAD~1..HEAD
丢弃本地所有修改
bash
git checkout -- .
git restore .
git reset --hard HEAD
8. 标签管理
创建标签
bash
git tag v1.0.0
git tag -a v1.0.0 -m "版本1.0.0发布"
git tag -a v1.0.0 commit_id
查看标签
bash
git tag
git tag -l "v1.*"
git show v1.0.0
删除标签
bash
git tag -d v1.0.0
git push origin --delete v1.0.0
推送标签到远程
bash
git push origin v1.0.0
git push origin --tags
9. 其他常用命令
储藏工作区更改
bash
git stash
git stash save "暂存信息"
git stash -u
git stash -a
查看储藏列表
bash
git stash list
恢复储藏
bash
git stash apply
git stash apply stash@{0}
git stash pop
删除储藏
bash
git stash drop
git stash drop stash@{0}
git stash clear
清理未跟踪文件
bash
git clean -f
git clean -fd
git clean -n
搜索提交历史
bash
git log --grep="keyword"
git log -S "code_content"
创建压缩包
bash
git archive main --format=zip --output=archive.zip
查看忽略的文件
bash
git status --ignored
10. 常见问题解决
1. 合并冲突
bash
git merge branch_name
git status
手动编辑冲突文件,然后:
bash
git add filename.txt
git commit -m "解决冲突:合并branch_name"
2. 取消正在进行的rebase
bash
git rebase --abort
继续rebase
bash
git rebase --continue
3. cherry-pick
bash
git cherry-pick commit_id
git cherry-pick commit_id1..commit_id2
4. 更新fork仓库
bash
git remote add upstream original_repo_url
git fetch upstream
git merge upstream/main
5. 修改最后一次提交
bash
git commit --amend
git commit --amend -m "新提交信息"
git commit --amend --no-edit
6. 查看远程仓库信息
bash
git remote show origin
7. 设置代理(如需要)
bash
git config --global http.proxy http://proxy.example.com:8080
git config --global https.proxy https://proxy.example.com:8080
8. 忽略文件权限变化
bash
git config core.fileMode false
附录:Robot-Nav 项目推荐工作流
首次克隆项目
bash
git clone https://github.com/Robot-Nav/RC-ESDF-2D.git
cd RC-ESDF-2D
日常开发流程
bash
git checkout -b feature/your_feature
git add .
git commit -m "feat: 描述本次修改"
git push -u origin feature/your_feature
合并到main分支
bash
git checkout main
git pull origin main
git merge feature/your_feature
git push origin main
git branch -d feature/your_feature
发布新版本
bash
git tag -a v1.0.0 -m "版本1.0.0"
git push origin v1.0.0
git push origin --tags
文档版本:v1.0
更新日期:2026-04-14