Git 常用指令手册
待补充。
2. 基础与定义
Section titled “2. 基础与定义”待补充。
3. 核心原理
Section titled “3. 核心原理”待补充。
4. 实现与应用
Section titled “4. 实现与应用”一、Git 命令索引
Section titled “一、Git 命令索引”1. git clone <仓库地址>
Section titled “1. git clone <仓库地址>”用途:从远程仓库克隆项目到本地。
git clone https://github.com/xxx/project.git2. git status
Section titled “2. git status”用途:查看当前仓库状态,包括修改、暂存、未跟踪文件。
git status3. git branch
Section titled “3. git branch”用途:查看本地分支。
git branch4. git branch -r
Section titled “4. git branch -r”用途:查看远程分支。
git branch -r5. git branch -a
Section titled “5. git branch -a”用途:查看所有分支,包括本地和远程。
git branch -a6. git branch --show-current
Section titled “6. git branch --show-current”用途:查看当前所在分支。
git branch --show-current7. git fetch origin
Section titled “7. git fetch origin”用途:获取远程仓库最新提交和分支信息,但不修改当前代码。
git fetch origin8. git fetch origin --prune
Section titled “8. git fetch origin --prune”用途:更新远程分支信息,并删除已经失效的远程分支引用。
git fetch origin --prune9. git fetch --all --prune
Section titled “9. git fetch --all --prune”用途:更新所有远程仓库的信息。
git fetch --all --prune10. git switch <分支名>
Section titled “10. git switch <分支名>”用途:切换到已有本地分支。
git switch main11. git switch -c <新分支名>
Section titled “11. git switch -c <新分支名>”用途:从当前代码创建并切换到新的本地分支。
git switch -c feat/test12. git switch -c <新分支> origin/<远程分支>
Section titled “12. git switch -c <新分支> origin/<远程分支>”用途:基于某个远程分支创建自己的本地分支。
git switch -c feat/minco-hjr origin/feat/minco13. git branch <新分支名>
Section titled “13. git branch <新分支名>”用途:创建新分支,但不切换过去。
git branch feat/test14. git branch -d <分支名>
Section titled “14. git branch -d <分支名>”用途:删除已经合并的本地分支。
git branch -d feat/test15. git branch -D <分支名>
Section titled “15. git branch -D <分支名>”用途:强制删除本地分支。
git branch -D feat/test16. git add <文件>
Section titled “16. git add <文件>”用途:把指定文件加入暂存区。
git add README.md17. git add .
Section titled “17. git add .”用途:把当前目录下所有修改加入暂存区。
git add .18. git restore <文件>
Section titled “18. git restore <文件>”用途:撤销尚未加入暂存区的文件修改。
git restore README.md19. git restore --staged <文件>
Section titled “19. git restore --staged <文件>”用途:把文件从暂存区移出,但保留文件修改。
git restore --staged README.md20. git diff
Section titled “20. git diff”用途:查看尚未执行 git add 的代码修改。
git diff21. git diff --staged
Section titled “21. git diff --staged”用途:查看已经加入暂存区、准备提交的修改。
git diff --staged22. git diff <分支1>..<分支2>
Section titled “22. git diff <分支1>..<分支2>”用途:比较两个分支之间的代码差异。
git diff main..feat/minco-hjr23. git diff --stat <分支1>..<分支2>
Section titled “23. git diff --stat <分支1>..<分支2>”用途:查看两个分支修改了哪些文件以及修改量。
git diff --stat origin/feat/minco..feat/minco-hjr24. git commit -m "<提交说明>"
Section titled “24. git commit -m "<提交说明>"”用途:提交暂存区中的修改。
git commit -m "Add dynamic obstacle support"25. git log
Section titled “25. git log”用途:查看当前分支的完整提交历史。
git log26. git log --oneline
Section titled “26. git log --oneline”用途:简洁查看提交历史。
git log --oneline27. git log <分支名> --oneline
Section titled “27. git log <分支名> --oneline”用途:查看指定分支的提交历史。
git log origin/feat/minco --oneline28. git log --graph --decorate --oneline --all
Section titled “28. git log --graph --decorate --oneline --all”用途:查看所有分支的提交、分叉和合并关系。
git log --graph --decorate --oneline --all29. git show <commit>
Section titled “29. git show <commit>”用途:查看某一次提交具体修改了什么。
git show a1b2c3d30. git pull
Section titled “30. git pull”用途:获取当前远程分支最新代码并合并到本地。
git pull31. git pull --rebase
Section titled “31. git pull --rebase”用途:获取远程更新,并把自己的提交接到最新远程提交之后。
git pull --rebase32. git push
Section titled “32. git push”用途:把当前本地分支的提交推送到对应远程分支。
git push33. git push -u origin <分支名>
Section titled “33. git push -u origin <分支名>”用途:第一次把本地新分支推送到远程,并建立跟踪关系。
git push -u origin feat/minco-hjr之后只需要:
git push34. git push origin --delete <分支名>
Section titled “34. git push origin --delete <分支名>”用途:删除远程分支。
git push origin --delete feat/test35. git remote -v
Section titled “35. git remote -v”用途:查看远程仓库名称和地址。
git remote -v36. git remote show origin
Section titled “36. git remote show origin”用途:查看 origin 的详细信息以及本地、远程分支跟踪关系。
git remote show origin37. git stash
Section titled “37. git stash”用途:临时保存当前未提交修改。
git stash38. git stash list
Section titled “38. git stash list”用途:查看已经保存的 stash。
git stash list39. git stash pop
Section titled “39. git stash pop”用途:恢复最近一次 stash,并删除该 stash。
git stash pop40. git merge <分支名>
Section titled “40. git merge <分支名>”用途:把指定分支合并到当前分支。
git merge feat/minco-hjr41. git rebase <分支名>
Section titled “41. git rebase <分支名>”用途:把当前分支的提交重新接到另一个分支最新提交之后。
git rebase main42. git reset --soft HEAD~1
Section titled “42. git reset --soft HEAD~1”用途:撤销最近一次 commit,但保留修改,并保持在暂存区。
git reset --soft HEAD~143. git reset HEAD~1
Section titled “43. git reset HEAD~1”用途:撤销最近一次 commit,保留代码修改,但取消暂存。
git reset HEAD~144. git reset --hard HEAD~1
Section titled “44. git reset --hard HEAD~1”用途:彻底删除最近一次 commit 和对应代码修改。
git reset --hard HEAD~1注意:该命令会直接丢失代码修改。
45. git revert <commit>
Section titled “45. git revert <commit>”用途:通过创建新的 commit 撤销指定历史提交。
git revert a1b2c3d适合撤销已经推送到远程的提交。
46. git tag
Section titled “46. git tag”用途:查看所有标签。
git tag47. git tag <标签名>
Section titled “47. git tag <标签名>”用途:给当前提交创建标签。
git tag v1.048. git push origin <标签名>
Section titled “48. git push origin <标签名>”用途:把标签上传到远程。
git push origin v1.0二、克隆与提交流程
Section titled “二、克隆与提交流程”假设远程仓库为:
https://github.com/xxx/YOPO-swarm.git远程已有基础分支:
origin/feat/minco希望创建自己的开发分支:
feat/minco-hjr1. 仓库克隆
Section titled “1. 仓库克隆”git clone https://github.com/xxx/YOPO-swarm.gitcd YOPO-swarm2. 远程仓库检查
Section titled “2. 远程仓库检查”git remote -v3. 远程分支同步
Section titled “3. 远程分支同步”git fetch origin --prune查看全部分支:
git branch -a可能看到:
* main remotes/origin/main remotes/origin/feat/minco4. 本地分支创建
Section titled “4. 本地分支创建”git switch -c feat/minco-hjr origin/feat/minco确认当前分支:
git branch --show-current应该显示:
feat/minco-hjr5. 分支首次推送
Section titled “5. 分支首次推送”git push -u origin feat/minco-hjr此时分支关系:
origin/feat/minco │ │ 基于该分支创建 ▼feat/minco-hjr │ │ git push ▼origin/feat/minco-hjr6. 代码开发
Section titled “6. 代码开发”修改代码后检查状态:
git status查看具体修改:
git diff7. 修改暂存
Section titled “7. 修改暂存”全部加入:
git add .或者只加入指定文件:
git add src/example.py再次确认:
git status8. 修改提交
Section titled “8. 修改提交”git commit -m "Add MINCO trajectory optimization"查看提交:
git log --oneline9. 远程分支推送
Section titled “9. 远程分支推送”git push
或者
git push -u origin HEAD三、日常开发流程
Section titled “三、日常开发流程”每次开始开发:
git statusgit pull --rebase修改代码后:
git diffgit add .git commit -m "修改说明"git push完整流程:
检查仓库状态 ↓同步远程代码 ↓修改代码 ↓检查代码差异 ↓加入暂存区 ↓创建 Commit ↓推送到远程推荐日常使用:
git status
git pull --rebase
# 修改代码
git diff
git add .
git commit -m "Update xxx"
git push5. 分析与总结
Section titled “5. 分析与总结”四、YOPO 分支结构
Section titled “四、YOPO 分支结构”origin/feat/minco │ │ 原始基础分支 ▼feat/minco-hjr │ │ 本地开发 ▼origin/feat/minco-hjr原则:
origin/feat/minco:保留原始代码。feat/minco-hjr:自己的本地开发分支。origin/feat/minco-hjr:自己的远程开发分支。- 日常修改尽量只在
feat/minco-hjr上进行。
6. 关联与参考
Section titled “6. 关联与参考”待补充。