跳转到内容

Git 常用指令手册

待补充。

待补充。

待补充。

用途:从远程仓库克隆项目到本地。

终端窗口
git clone https://github.com/xxx/project.git

用途:查看当前仓库状态,包括修改、暂存、未跟踪文件。

终端窗口
git status

用途:查看本地分支。

终端窗口
git branch

用途:查看远程分支。

终端窗口
git branch -r

用途:查看所有分支,包括本地和远程。

终端窗口
git branch -a

用途:查看当前所在分支。

终端窗口
git branch --show-current

用途:获取远程仓库最新提交和分支信息,但不修改当前代码。

终端窗口
git fetch origin

用途:更新远程分支信息,并删除已经失效的远程分支引用。

终端窗口
git fetch origin --prune

用途:更新所有远程仓库的信息。

终端窗口
git fetch --all --prune

用途:切换到已有本地分支。

终端窗口
git switch main

用途:从当前代码创建并切换到新的本地分支。

终端窗口
git switch -c feat/test

12. git switch -c <新分支> origin/<远程分支>

Section titled “12. git switch -c <新分支> origin/<远程分支>”

用途:基于某个远程分支创建自己的本地分支。

终端窗口
git switch -c feat/minco-hjr origin/feat/minco

用途:创建新分支,但不切换过去。

终端窗口
git branch feat/test

用途:删除已经合并的本地分支。

终端窗口
git branch -d feat/test

用途:强制删除本地分支。

终端窗口
git branch -D feat/test

用途:把指定文件加入暂存区。

终端窗口
git add README.md

用途:把当前目录下所有修改加入暂存区。

终端窗口
git add .

用途:撤销尚未加入暂存区的文件修改。

终端窗口
git restore README.md

用途:把文件从暂存区移出,但保留文件修改。

终端窗口
git restore --staged README.md

用途:查看尚未执行 git add 的代码修改。

终端窗口
git diff

用途:查看已经加入暂存区、准备提交的修改。

终端窗口
git diff --staged

用途:比较两个分支之间的代码差异。

终端窗口
git diff main..feat/minco-hjr

用途:查看两个分支修改了哪些文件以及修改量。

终端窗口
git diff --stat origin/feat/minco..feat/minco-hjr

用途:提交暂存区中的修改。

终端窗口
git commit -m "Add dynamic obstacle support"

用途:查看当前分支的完整提交历史。

终端窗口
git log

用途:简洁查看提交历史。

终端窗口
git log --oneline

用途:查看指定分支的提交历史。

终端窗口
git log origin/feat/minco --oneline

28. git log --graph --decorate --oneline --all

Section titled “28. git log --graph --decorate --oneline --all”

用途:查看所有分支的提交、分叉和合并关系。

终端窗口
git log --graph --decorate --oneline --all

用途:查看某一次提交具体修改了什么。

终端窗口
git show a1b2c3d

用途:获取当前远程分支最新代码并合并到本地。

终端窗口
git pull

用途:获取远程更新,并把自己的提交接到最新远程提交之后。

终端窗口
git pull --rebase

用途:把当前本地分支的提交推送到对应远程分支。

终端窗口
git push

用途:第一次把本地新分支推送到远程,并建立跟踪关系。

终端窗口
git push -u origin feat/minco-hjr

之后只需要:

终端窗口
git push

用途:删除远程分支。

终端窗口
git push origin --delete feat/test

用途:查看远程仓库名称和地址。

终端窗口
git remote -v

用途:查看 origin 的详细信息以及本地、远程分支跟踪关系。

终端窗口
git remote show origin

用途:临时保存当前未提交修改。

终端窗口
git stash

用途:查看已经保存的 stash。

终端窗口
git stash list

用途:恢复最近一次 stash,并删除该 stash。

终端窗口
git stash pop

用途:把指定分支合并到当前分支。

终端窗口
git merge feat/minco-hjr

用途:把当前分支的提交重新接到另一个分支最新提交之后。

终端窗口
git rebase main

用途:撤销最近一次 commit,但保留修改,并保持在暂存区。

终端窗口
git reset --soft HEAD~1

用途:撤销最近一次 commit,保留代码修改,但取消暂存。

终端窗口
git reset HEAD~1

用途:彻底删除最近一次 commit 和对应代码修改。

终端窗口
git reset --hard HEAD~1

注意:该命令会直接丢失代码修改。


用途:通过创建新的 commit 撤销指定历史提交。

终端窗口
git revert a1b2c3d

适合撤销已经推送到远程的提交。


用途:查看所有标签。

终端窗口
git tag

用途:给当前提交创建标签。

终端窗口
git tag v1.0

用途:把标签上传到远程。

终端窗口
git push origin v1.0

假设远程仓库为:

https://github.com/xxx/YOPO-swarm.git

远程已有基础分支:

origin/feat/minco

希望创建自己的开发分支:

feat/minco-hjr

终端窗口
git clone https://github.com/xxx/YOPO-swarm.git
cd YOPO-swarm

终端窗口
git remote -v

终端窗口
git fetch origin --prune

查看全部分支:

终端窗口
git branch -a

可能看到:

* main
remotes/origin/main
remotes/origin/feat/minco

终端窗口
git switch -c feat/minco-hjr origin/feat/minco

确认当前分支:

终端窗口
git branch --show-current

应该显示:

feat/minco-hjr

终端窗口
git push -u origin feat/minco-hjr

此时分支关系:

origin/feat/minco
│
│ 基于该分支创建
▼
feat/minco-hjr
│
│ git push
▼
origin/feat/minco-hjr

修改代码后检查状态:

终端窗口
git status

查看具体修改:

终端窗口
git diff

全部加入:

终端窗口
git add .

或者只加入指定文件:

终端窗口
git add src/example.py

再次确认:

终端窗口
git status

终端窗口
git commit -m "Add MINCO trajectory optimization"

查看提交:

终端窗口
git log --oneline

终端窗口
git push
或者
git push -u origin HEAD

每次开始开发:

终端窗口
git status
git pull --rebase

修改代码后:

终端窗口
git diff
git add .
git commit -m "修改说明"
git push

完整流程:

检查仓库状态
↓
同步远程代码
↓
修改代码
↓
检查代码差异
↓
加入暂存区
↓
创建 Commit
↓
推送到远程

推荐日常使用:

终端窗口
git status
git pull --rebase
# 修改代码
git diff
git add .
git commit -m "Update xxx"
git push

origin/feat/minco
│
│ 原始基础分支
▼
feat/minco-hjr
│
│ 本地开发
▼
origin/feat/minco-hjr

原则:

  • origin/feat/minco:保留原始代码。
  • feat/minco-hjr:自己的本地开发分支。
  • origin/feat/minco-hjr:自己的远程开发分支。
  • 日常修改尽量只在 feat/minco-hjr 上进行。

待补充。