以夜為箋,寄心成簡。

Git常用命令工具书

记录一些常用的Git命令,方便查阅。

pull

拉取本地没有的分支

从远程pull本地没有的分支,同时关联本地分支与远程分支,然后切换到该分支:

1
2
git fetch // 先fetch
git checkout -b <new_local_branch> <remote_name>/<remote_branch>

push

关联分支并推送

push 本地分支 到 远程分支(如果远程没有该分支则自动创建一个),且将本地分支与远程分支关联起来:

1
2
3
// 本地分支和远程分支名一样
git push -u <remote_name> <branch>

branch

删除分支

1
2
3
4
5
6
7
8
// 删除本地分支
git branch -d <branch_name>

// 强制删除未merge的分支
git branch -D <branch_name>

// 删除远程分支
git push <remote_name> --delete <branch_name>

rebase

修复历史中某个commit

1
2
3
4
5
6
7
8
9
10
// 1. 找到需要修改的commit_id,然后执行交互式rebase
git rebase -i <commit_id>^

// 2. 将需要修改的commit前的pick改为edit,保存退出
// 3. 现在在目标commit上,进行修改,然后add
// 4. amend
git commit --amend
// 5. 继续rebase
git rebase --continue

submodule

给仓库添加子模块

1
2
3
4
5
6
// 将远程其他仓库作为子模块添加到当前仓库中(可以指定保存路径local_path)
git submodule add <remote_repository_url> [<local_path>]

// 然后会出现 .gitmodules 文件和你指定的文件路径,记得提交
git add .gitmodules <local_path>
git commit -m "Add submodule"

更新 submodule

1
2
3
4
5
// pull主仓库的同时更新submodule
git pull --recurse-submodules

// 直接更新 submodule
git submodule update --init --recursive

© 2026 Ruihao (Ray) Zhang

Elegant theme by Shiro · Made by Acris with ❤️

白は、余白の名。