Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
Oil_Level_Recognition_System
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Oil_Level_Recognition_System
Commits
d0ae2cc4
Commit
d0ae2cc4
authored
Nov 30, 2025
by
Yuhaibo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1
parent
8b8517af
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
828 additions
and
0 deletions
+828
-0
git指令集.md
rules/git指令集.md
+828
-0
No files found.
rules/git指令集.md
0 → 100644
View file @
d0ae2cc4
# Git 指令手册
# Git 指令手册
## 📋 目录
-
[
基础配置
](
#基础配置
)
-
[
仓库操作
](
#仓库操作
)
-
[
分支管理
](
#分支管理
)
-
[
提交管理
](
#提交管理
)
-
[
远程仓库
](
#远程仓库
)
-
[
查看状态与历史
](
#查看状态与历史
)
-
[
撤销与回退
](
#撤销与回退
)
-
[
标签管理
](
#标签管理
)
-
[
暂存管理
](
#暂存管理
)
-
[
高级操作
](
#高级操作
)
---
## 基础配置
### 配置用户信息
```
bash
# 设置全局用户名
git config
--global
user.name
"你的名字"
# 设置全局邮箱
git config
--global
user.email
"你的邮箱@example.com"
# 设置当前仓库用户名(仅对当前仓库有效)
git config user.name
"你的名字"
# 设置当前仓库邮箱
git config user.email
"你的邮箱@example.com"
```
### 查看配置
```
bash
# 查看所有配置
git config
--list
# 查看全局配置
git config
--global
--list
# 查看特定配置项
git config user.name
git config user.email
```
### 其他配置
```
bash
# 设置默认编辑器
git config
--global
core.editor
"code --wait"
# 设置默认分支名称
git config
--global
init.defaultBranch main
# 启用颜色输出
git config
--global
color.ui auto
# 设置换行符转换
git config
--global
core.autocrlf
true
# Windows
git config
--global
core.autocrlf input
# Linux/Mac
```
---
## 仓库操作
### 初始化仓库
```
bash
# 在当前目录初始化Git仓库
git init
# 创建新目录并初始化
git init 项目名称
```
### 克隆仓库
```
bash
# 克隆远程仓库
git clone https://github.com/用户名/仓库名.git
# 克隆到指定目录
git clone https://github.com/用户名/仓库名.git 本地目录名
# 克隆指定分支
git clone
-b
分支名 https://github.com/用户名/仓库名.git
# 浅克隆(只克隆最近的提交历史)
git clone
--depth
1 https://github.com/用户名/仓库名.git
```
---
## 分支管理
### 查看分支
```
bash
# 查看本地分支
git branch
# 查看所有分支(包括远程)
git branch
-a
# 查看远程分支
git branch
-r
# 查看分支详细信息
git branch
-v
# 查看已合并的分支
git branch
--merged
# 查看未合并的分支
git branch
--no-merged
```
### 创建分支
```
bash
# 创建新分支(但不切换)
git branch 新分支名
# 基于指定提交创建分支
git branch 新分支名 提交哈希值
# 基于远程分支创建本地分支
git branch 新分支名 origin/远程分支名
```
### 切换分支
```
bash
# 切换到已存在的分支
git checkout 分支名
# 创建并切换到新分支
git checkout
-b
新分支名
# 基于远程分支创建并切换
git checkout
-b
本地分支名 origin/远程分支名
# 切换分支(新语法)
git switch 分支名
# 创建并切换分支(新语法)
git switch
-c
新分支名
```
### 合并分支
```
bash
# 合并指定分支到当前分支
git merge 分支名
# 不使用快进模式合并(保留分支历史)
git merge
--no-ff
分支名
# 压缩合并(将所有提交合并为一个)
git merge
--squash
分支名
# 取消合并
git merge
--abort
```
### 删除分支
```
bash
# 删除本地分支
git branch
-d
分支名
# 强制删除本地分支(即使未合并)
git branch
-D
分支名
# 删除远程分支
git push origin
--delete
分支名
git push origin :分支名
# 旧语法
```
### 重命名分支
```
bash
# 重命名当前分支
git branch
-m
新分支名
# 重命名指定分支
git branch
-m
旧分支名 新分支名
```
---
## 提交管理
### 添加文件到暂存区
```
bash
# 添加指定文件
git add 文件名
# 添加多个文件
git add 文件1 文件2 文件3
# 添加所有修改的文件
git add
.
git add
-A
# 添加指定目录
git add 目录名/
# 交互式添加
git add
-p
```
### 提交更改
```
bash
# 提交暂存区的文件
git commit
-m
"提交说明"
# 添加并提交(跳过git add)
git commit
-am
"提交说明"
# 修改最后一次提交
git commit
--amend
# 修改最后一次提交的说明
git commit
--amend
-m
"新的提交说明"
# 提交时跳过预提交钩子
git commit
--no-verify
-m
"提交说明"
```
### 查看更改
```
bash
# 查看工作区与暂存区的差异
git diff
# 查看暂存区与最后一次提交的差异
git diff
--cached
git diff
--staged
# 查看工作区与最后一次提交的差异
git diff HEAD
# 查看两个提交之间的差异
git diff 提交1 提交2
# 查看指定文件的差异
git diff 文件名
# 查看分支差异
git diff 分支1 分支2
```
### 移除文件
```
bash
# 从工作区和暂存区删除文件
git rm 文件名
# 只从暂存区删除,保留工作区文件
git rm
--cached
文件名
# 删除目录及其内容
git rm
-r
目录名
```
### 移动/重命名文件
```
bash
# 重命名文件
git mv 旧文件名 新文件名
# 移动文件到目录
git mv 文件名 目录/
```
---
## 远程仓库
### 查看远程仓库
```
bash
# 查看远程仓库
git remote
# 查看远程仓库详细信息
git remote
-v
# 查看指定远程仓库信息
git remote show origin
```
### 添加远程仓库
```
bash
# 添加远程仓库
git remote add origin https://github.com/用户名/仓库名.git
# 添加多个远程仓库
git remote add upstream https://github.com/原作者/仓库名.git
```
### 修改远程仓库
```
bash
# 修改远程仓库地址
git remote set-url origin 新地址
# 重命名远程仓库
git remote rename 旧名称 新名称
# 删除远程仓库
git remote remove 远程仓库名
```
### 拉取更新
```
bash
# 从远程仓库获取更新(不合并)
git fetch origin
# 从远程仓库获取所有分支
git fetch
--all
# 拉取远程仓库更新并合并
git pull origin 分支名
# 拉取并使用rebase合并
git pull
--rebase
origin 分支名
```
### 推送更新
```
bash
# 推送到远程仓库
git push origin 分支名
# 首次推送并设置上游分支
git push
-u
origin 分支名
# 推送所有分支
git push
--all
origin
# 强制推送(危险操作)
git push
-f
origin 分支名
git push
--force-with-lease
origin 分支名
# 更安全的强制推送
# 推送标签
git push origin 标签名
git push
--tags
# 推送所有标签
```
---
## 查看状态与历史
### 查看状态
```
bash
# 查看工作区状态
git status
# 简洁模式查看状态
git status
-s
git status
--short
```
### 查看提交历史
```
bash
# 查看提交历史
git log
# 简洁显示(一行)
git log
--oneline
# 显示最近N条记录
git log
-n
5
git log
-5
# 图形化显示分支历史
git log
--graph
--oneline
--all
# 显示每次提交的文件变化
git log
--stat
# 显示详细的差异内容
git log
-p
# 按作者筛选
git log
--author
=
"作者名"
# 按时间筛选
git log
--since
=
"2024-01-01"
git log
--after
=
"2 weeks ago"
git log
--before
=
"2024-12-31"
# 按提交信息搜索
git log
--grep
=
"关键词"
# 查看指定文件的历史
git log
--
文件名
# 美化的图形日志
git log
--graph
--pretty
=
format:
'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'
--abbrev-commit
```
### 查看提交详情
```
bash
# 查看最新提交
git show
# 查看指定提交
git show 提交哈希值
# 查看指定文件在某次提交的内容
git show 提交哈希值:文件路径
```
### 查看文件历史
```
bash
# 查看文件的每一行最后修改信息
git blame 文件名
# 查看文件在指定提交时的内容
git show 提交哈希值:文件名
```
---
## 撤销与回退
### 撤销工作区修改
```
bash
# 撤销工作区的修改(恢复到暂存区状态)
git checkout
--
文件名
# 撤销所有工作区修改
git checkout
--
.
# 新语法:恢复工作区文件
git restore 文件名
git restore
.
```
### 撤销暂存区
```
bash
# 将文件从暂存区移除(保留工作区修改)
git reset HEAD 文件名
# 新语法:取消暂存
git restore
--staged
文件名
git restore
--staged
.
```
### 回退提交
```
bash
# 回退到上一次提交(保留工作区修改)
git reset
--soft
HEAD~1
# 回退到上一次提交(保留工作区和暂存区修改)
git reset
--mixed
HEAD~1
git reset HEAD~1
# 默认是mixed模式
# 回退到上一次提交(丢弃所有修改)
git reset
--hard
HEAD~1
# 回退到指定提交
git reset
--hard
提交哈希值
# 回退多个提交
git reset
--hard
HEAD~3
# 回退3个提交
```
### 撤销提交(保留历史)
```
bash
# 创建新提交来撤销指定提交
git revert 提交哈希值
# 撤销最近的提交
git revert HEAD
# 撤销多个提交
git revert HEAD~2..HEAD
```
### 清理工作区
```
bash
# 查看将被删除的文件
git clean
-n
# 删除未跟踪的文件
git clean
-f
# 删除未跟踪的文件和目录
git clean
-fd
# 删除包括被忽略的文件
git clean
-fdx
```
---
## 标签管理
### 查看标签
```
bash
# 查看所有标签
git tag
# 查看符合模式的标签
git tag
-l
"v1.*"
# 查看标签详细信息
git show 标签名
```
### 创建标签
```
bash
# 创建轻量标签
git tag 标签名
# 创建附注标签(推荐)
git tag
-a
标签名
-m
"标签说明"
# 给指定提交打标签
git tag
-a
标签名 提交哈希值
-m
"标签说明"
```
### 删除标签
```
bash
# 删除本地标签
git tag
-d
标签名
# 删除远程标签
git push origin
--delete
标签名
git push origin :refs/tags/标签名
```
### 推送标签
```
bash
# 推送指定标签
git push origin 标签名
# 推送所有标签
git push origin
--tags
```
### 检出标签
```
bash
# 检出标签(会进入分离头指针状态)
git checkout 标签名
# 基于标签创建新分支
git checkout
-b
新分支名 标签名
```
---
## 暂存管理
### 暂存更改
```
bash
# 暂存当前工作区所有更改
git stash
# 暂存时添加说明
git stash save
"暂存说明"
# 暂存包括未跟踪的文件
git stash
-u
git stash
--include-untracked
# 暂存所有文件(包括被忽略的)
git stash
-a
git stash
--all
```
### 查看暂存
```
bash
# 查看暂存列表
git stash list
# 查看暂存的详细内容
git stash show
# 查看指定暂存的差异
git stash show
-p
stash@
{
0
}
```
### 恢复暂存
```
bash
# 恢复最新的暂存
git stash pop
# 恢复指定的暂存
git stash pop stash@
{
编号
}
# 应用暂存但不删除
git stash apply
git stash apply stash@
{
编号
}
```
### 删除暂存
```
bash
# 删除指定暂存
git stash drop stash@
{
编号
}
# 清空所有暂存
git stash clear
```
### 创建分支从暂存
```
bash
# 从暂存创建新分支
git stash branch 新分支名 stash@
{
编号
}
```
---
## 高级操作
### 变基(Rebase)
```
bash
# 将当前分支变基到指定分支
git rebase 目标分支
# 交互式变基(修改历史提交)
git rebase
-i
HEAD~3
# 继续变基
git rebase
--continue
# 跳过当前提交
git rebase
--skip
# 取消变基
git rebase
--abort
```
### 拣选提交(Cherry-pick)
```
bash
# 应用指定提交到当前分支
git cherry-pick 提交哈希值
# 应用多个提交
git cherry-pick 提交1 提交2 提交3
# 应用提交范围
git cherry-pick 提交1..提交2
# 只应用但不提交
git cherry-pick
-n
提交哈希值
```
### 子模块管理
```
bash
# 添加子模块
git submodule add 仓库地址 路径
# 初始化子模块
git submodule init
# 更新子模块
git submodule update
# 克隆包含子模块的仓库
git clone
--recursive
仓库地址
# 更新所有子模块到最新
git submodule update
--remote
```
### 搜索内容
```
bash
# 在工作区搜索文本
git
grep
"搜索内容"
# 在指定提交中搜索
git
grep
"搜索内容"
提交哈希值
# 显示行号
git
grep
-n
"搜索内容"
# 统计匹配次数
git
grep
-c
"搜索内容"
```
### 二分查找(Bisect)
```
bash
# 启动二分查找
git bisect start
# 标记当前版本为坏版本
git bisect bad
# 标记某个版本为好版本
git bisect good 提交哈希值
# 标记当前版本为好版本
git bisect good
# 重置二分查找
git bisect reset
```
### 工作树(Worktree)
```
bash
# 添加新工作树
git worktree add 路径 分支名
# 列出所有工作树
git worktree list
# 删除工作树
git worktree remove 路径
# 清理工作树
git worktree prune
```
### 引用日志(Reflog)
```
bash
# 查看引用日志
git reflog
# 查看指定分支的引用日志
git reflog show 分支名
# 恢复到引用日志中的某个状态
git reset
--hard
HEAD@
{
编号
}
```
---
## 💡 常用场景示例
### 场景1:修改最后一次提交
```
bash
# 修改文件后
git add 修改的文件
git commit
--amend
--no-edit
# 不修改提交信息
```
### 场景2:合并多个提交
```
bash
# 合并最近3个提交
git rebase
-i
HEAD~3
# 在编辑器中将第2、3个提交的pick改为squash
```
### 场景3:撤销已推送的提交
```
bash
# 方式1:创建新提交撤销
git revert 提交哈希值
git push origin 分支名
# 方式2:强制推送(危险,需谨慎)
git reset
--hard
HEAD~1
git push
-f
origin 分支名
```
### 场景4:同步fork的仓库
```
bash
# 添加上游仓库
git remote add upstream 原仓库地址
# 获取上游更新
git fetch upstream
# 合并上游主分支
git merge upstream/main
# 推送到自己的仓库
git push origin main
```
### 场景5:临时切换分支
```
bash
# 暂存当前工作
git stash
# 切换分支并工作
git checkout 其他分支
# ... 工作 ...
git commit
-m
"完成其他分支工作"
# 切回原分支
git checkout 原分支
# 恢复工作
git stash pop
```
### 场景6:找回删除的提交
```
bash
# 查看历史操作
git reflog
# 恢复到删除前的状态
git reset
--hard
HEAD@
{
编号
}
```
---
## ⚠️ 注意事项
1.
**不要在公共分支使用 `git push -f`**
- 会影响其他协作者
2.
**不要修改已推送的提交历史**
- 除非你知道后果
3.
**定期备份重要分支**
- 防止误操作
4.
**合并前先拉取最新代码**
- 避免冲突
5.
**提交前检查状态**
- 使用
`git status`
和
`git diff`
6.
**编写清晰的提交信息**
- 方便追踪和回溯
7.
**敏感信息不要提交**
- 使用
`.gitignore`
排除
8.
**大文件使用 Git LFS**
- 避免仓库过大
---
## 📚 常用别名配置
```
bash
# 添加常用别名
git config
--global
alias.st status
git config
--global
alias.co checkout
git config
--global
alias.br branch
git config
--global
alias.ci commit
git config
--global
alias.unstage
'reset HEAD --'
git config
--global
alias.last
'log -1 HEAD'
git config
--global
alias.visual
'log --graph --oneline --all'
git config
--global
alias.amend
'commit --amend --no-edit'
```
使用别名后:
```
bash
git st
# 等同于 git status
git co main
# 等同于 git checkout main
git br
-a
# 等同于 git branch -a
git visual
# 查看图形化历史
```
---
**最后更新:2024年**
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment