Git 项目维护命令指南相关讲解

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 网页上创建仓库

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 步骤:

  1. 打开 GitHub → Settings
  2. 左侧底部找到 Developer settings
  3. Personal access tokensGenerate new token (classic)
  4. 勾选 repo 权限
  5. 生成后复制使用

推送 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

相关推荐
i建模2 小时前
强制同步远程git仓库
git
ZPC82102 小时前
MoveIt Servo 与自己编写的 Action Server 通信
人工智能·算法·机器人
逛逛GitHub3 小时前
给你的 Claude Code 装上一个超酷的状态栏。 逛逛GitHub
github
zhanglianzhao3 小时前
Gazebo仿真机器人和相机时Gazebo ROS Control 插件偶发性加载失败bug分析
机器人·bug·ros·gazebo·ros_control
CoderJia程序员甲4 小时前
GitHub 热榜项目 - 日榜(2026-04-14)
人工智能·ai·大模型·github·ai教程
Hical_W4 小时前
告别回调地狱:在 C++ Web 框架中全面拥抱协程
c++·github
别叫我->学废了->lol在线等4 小时前
claudecode的agent定义
前端·chrome·github
研究点啥好呢5 小时前
Github热榜项目推荐 | 学习与贡献是开源的意义
学习·开源·github
鸿蒙程序媛5 小时前
【工具汇总】git 常用命令行汇总
大数据·git·elasticsearch