在Shell脚本中开启debug模式有以下几种常用方法:
- 命令行执行时开启
# 使用 -x 参数
bash -x script.sh
sh -x script.sh
# 或者
sh script.sh -x
- 脚本内部开启
在脚本开头添加
#!/bin/bash
set -x # 开启debug模式
# 你的脚本内容
echo "Hello World"
针对特定代码块开启
#!/bin/bash
echo "这部分不调试"
set -x # 开始调试
echo "这部分会被调试"
ls -la
set +x # 关闭调试
echo "这部分又不调试了"
- 使用不同的调试级别
#!/bin/bash
# 显示执行的命令和参数
set -x
# 显示错误信息
set -e # 出错时立即退出
# 显示未定义的变量
set -u # 使用未定义变量时报错
# 组合使用
set -exu # 同时开启多个选项
使用调试选项
#!/bin/bash
# -v:显示输入的每一行
# -x:显示执行的命令及其参数
# -n:只读取命令但不执行(语法检查)
# 组合使用
bash -vx script.sh
# 语法检查模式
bash -n script.sh
自定义调试函数
#!/bin/bash
DEBUG=true # 控制是否调试
debug() {
if [ "$DEBUG" = true ]; then
echo "[DEBUG] $(date): $@" >&2
fi
}
# 使用
debug "开始执行重要操作"
# 你的代码
debug "操作完成"
PS4变量定制调试输出
#!/bin/bash
# 自定义调试提示符
export PS4='+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: '
set -x
# 你的代码
echo "Hello"
Git开发人员常用命令大全
基础配置
# 用户配置
git config --global user.name "你的名字"
git config --global user.email "你的邮箱"
git config --global core.editor "vim" # 设置默认编辑器
git config --global color.ui auto # 启用颜色显示
git config --list # 查看所有配置
# 别名配置(提高效率)
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
常用命令速查
- 仓库操作
git init # 初始化仓库
git clone <url> # 克隆仓库
git clone -b <branch> <url> # 克隆指定分支
git remote -v # 查看远程仓库
git remote add origin <url> # 添加远程仓库
git remote remove origin # 删除远程仓库
- 日常开发流程
# 查看状态
git status # 查看工作区状态
git status -s # 简洁模式
# 添加文件
git add <file> # 添加指定文件
git add . # 添加所有文件
git add -p # 交互式添加(逐个确认)
# 提交
git commit -m "message" # 提交
git commit -am "message" # 跳过git add直接提交(只对已跟踪文件)
git commit --amend # 修改最后一次提交
git commit --amend --no-edit # 不修改注释,只合并暂存区改动
# 推送
git push origin <branch> # 推送到远程
git push -u origin <branch> # 首次推送并建立关联
git push --force-with-lease # 安全强制推送
git push origin --delete <branch> # 删除远程分支
- 分支操作
# 查看分支
git branch # 查看本地分支
git branch -r # 查看远程分支
git branch -a # 查看所有分支
git branch -vv # 查看分支跟踪关系
# 创建和切换
git branch <branch> # 创建分支
git checkout <branch> # 切换分支
git checkout -b <branch> # 创建并切换
git switch <branch> # 新语法:切换分支
git switch -c <branch> # 新语法:创建并切换
# 合并和变基
git merge <branch> # 合并分支
git merge --no-ff <branch> # 禁用快进合并
git merge --abort # 放弃当前的合并,回到合并前的状态
git rebase <branch> # 变基
git rebase -i HEAD~3 # 交互式变基(合并/修改历史)
# 删除分支
git branch -d <branch> # 删除本地分支
git branch -D <branch> # 强制删除未合并分支
- 查看历史
# 日志查看
git log # 查看提交历史
git log --oneline # 简洁模式
git log --graph --all # 图形化显示所有分支
git log -p <file> # 查看文件修改详情
git log --author="name" # 按作者过滤
git log --since="2 days ago" # 按时间过滤
git log --grep="keyword" # 按提交信息过滤
# 差异比较
git diff # 工作区 vs 暂存区
git diff --staged # 暂存区 vs 仓库
git diff <branch1> <branch2> # 比较两个分支
git diff <commit1> <commit2> # 比较两个提交
git diff <file> # 查看文件具体修改
- 撤销和恢复
# 工作区撤销
git checkout -- <file> # 丢弃工作区修改
git restore <file> # 新语法:丢弃工作区修改
# 暂存区撤销
git reset HEAD <file> # 取消暂存
git restore --staged <file> # 新语法:取消暂存
# 提交撤销
git reset --soft HEAD^ # 撤销提交,保留修改
git reset --mixed HEAD^ # 撤销提交,取消暂存
git reset --hard HEAD^ # 完全撤销,丢失修改
git revert HEAD # 生成新提交撤销前一个提交
# 重置到指定版本
git reset --hard <commit-hash> # 重置到指定提交
git reset --hard origin/master # 重置到远程版本
- 暂存和储藏
git stash # 暂存当前修改
git stash save "message" # 带说明的暂存
git stash list # 查看暂存列表
git stash apply # 应用最近暂存
git stash pop # 应用并删除最近暂存
git stash drop # 删除最近暂存
git stash clear # 清空所有暂存
git stash branch <branch> # 从暂存创建分支
- 标签管理
git tag # 查看标签
git tag -l "v1.*" # 搜索标签
git tag <tag-name> # 创建轻量标签
git tag -a <tag-name> -m "message" # 创建附注标签
git tag -d <tag-name> # 删除本地标签
git push origin <tag-name> # 推送标签
git push origin --tags # 推送所有标签
git push origin --delete <tag-name> # 删除远程标签
- 远程同步
# 拉取和获取
git fetch # 获取远程更新(不合并)
git fetch --all # 获取所有远程更新
git pull # 拉取并合并
git pull --rebase # 拉取并变基(推荐)
# 清理
git clean -n # 查看要删除的未跟踪文件
git clean -f # 删除未跟踪文件
git clean -fd # 删除未跟踪文件和目录
# 子模块
git submodule add <url> <path> # 添加子模块
git submodule update --init --recursive # 更新所有子模块
- 高级搜索和调试
# 文件搜索
git grep "pattern" # 在文件中搜索
git grep -n "pattern" # 显示行号
# 二分查找(找bug)
git bisect start # 开始二分查找
git bisect bad # 标记当前为坏版本
git bisect good <commit> # 标记好版本
git bisect reset # 结束二分查找
# 查看文件历史
git blame <file> # 查看每行谁修改的
git show <commit> # 查看提交详情
git show <commit>:<file> # 查看某版本的文件内容
- 实用技巧
# 保留特定文件
git checkout <branch> -- <file> # 从其他分支检出文件
# 找回丢失的提交
git reflog # 查看所有操作记录
git cherry-pick <commit> # 挑拣提交到当前分支
# 统计
git shortlog -sn # 查看贡献者排名
git rev-list --count HEAD # 统计提交次数
git diff --shortstat # 统计改了多少文件
日常开发流程
# 1. 更新主分支
git checkout master
git pull --rebase
# 2. 创建功能分支
git checkout -b feature/new-function
# 3. 开发并多次提交
git add .
git commit -m "完成功能A"
git add .
git commit -m "完成功能B"
# 4. 同步主分支更新
git fetch origin
git rebase origin/master
# 5. 推送代码
git push -u origin feature/new-function
# 6. 合并回主分支(在GitHub/Gitlab上提PR/MR)
紧急修复流程
# 从主分支创建hotfix分支
git checkout master
git checkout -b hotfix/urgent-fix
# 修复并提交
git add .
git commit -m "紧急修复bug"
# 部署测试
git push origin hotfix/urgent-fix
# 合并到主分支和开发分支
git checkout master
git merge hotfix/urgent-fix
git push
git checkout develop
git merge hotfix/urgent-fix
git push
常用组合命令
# 一键提交所有改动
git add . && git commit -m "message"
# 撤销上次提交并保留修改
git reset --soft HEAD^
# 修改最新提交信息
git commit --amend -m "new message"
# 强制更新本地分支(丢弃本地修改)
git fetch origin && git reset --hard origin/master
# 清理已合并的分支
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d