Git
2026/5/29工具通用工具工具通用工具Git大约 4 分钟
Git
是什么
Git 是版本控制工具,用来管理代码、题解、脚本和知识库修改历史。
CTF 学习中 Git 常用于:
- 下载题库或工具源码
- 管理解题脚本
- 保存 WP 修改历史
- 协作维护知识库
安装与配置
Linux:
sudo apt update
sudo apt install gitmacOS:
brew install gitWindows:
https://git-scm.com/download/win基础配置:
git config --global user.name "your-name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main基本用法
克隆仓库
git clone https://github.com/example/repo.git查看状态
git status提交修改
git add .
git commit -m "add web notes"查看历史
git log --oneline --graph --all拉取更新
git pull分支操作
创建分支
git branch feature-web
git checkout feature-web
# 或一步完成
git checkout -b feature-web查看分支
git branch # 本地分支
git branch -a # 所有分支
git branch -v # 分支详情切换分支
git checkout main
git checkout feature-web合并分支
git checkout main
git merge feature-web删除分支
git branch -d feature-web # 删除已合并分支
git branch -D feature-web # 强制删除
git push origin --delete feature-web # 删除远程分支推送分支
git push origin feature-web
git push -u origin feature-web # 设置上游并推送冲突解决
查看冲突
git status冲突文件会显示 both modified。
冲突标记
<<<<<<< HEAD
当前分支的内容
=======
要合并分支的内容
>>>>>>> feature-web解决步骤
# 1. 编辑冲突文件,删除标记,保留正确内容
# 2. 添加解决后的文件
git add conflicted-file.md
# 3. 完成合并
git commit -m "resolve merge conflict"使用工具解决
# 使用 VS Code
code conflicted-file.md
# 使用 mergetool
git mergetool远程操作
添加远程
git remote add origin https://github.com/user/repo.git查看远程
git remote -v推送
git push origin main
git push -u origin main # 设置上游拉取
git pull origin main
git pull --rebase origin main # 变基拉取抓取
git fetch origin
git fetch --all暂存操作
暂存修改
git stash
git stash push -m "work in progress"查看暂存
git stash list恢复暂存
git stash pop # 恢复并删除
git stash apply # 恢复不删除
git stash apply stash@{1}删除暂存
git stash drop stash@{0}
git stash clear回退操作
查看提交历史
git log --oneline
git log --oneline -10
git log --graph --oneline --all回退到某个提交
git reset --soft HEAD~1 # 保留修改在暂存区
git reset --mixed HEAD~1 # 保留修改在工作区
git reset --hard HEAD~1 # 丢弃所有修改撤销工作区修改
git checkout -- file.md
git restore file.md撤销暂存
git reset HEAD file.md
git restore --staged file.md创建新提交撤销
git revert HEAD
git revert <commit-hash>CTF常用技巧
下载工具源码
git clone https://github.com/sqlmapproject/sqlmap.git
git clone https://github.com/vulhub/vulhub.git搜索历史中的秘密
如果题目给了 .git 泄露:
git log --oneline
git show <commit>
git diff HEAD~1 HEAD恢复被删文件
git checkout <commit> -- path/to/file公开知识库维护时,不要随便强推或重写历史。
.git 泄露利用
# 下载 .git 目录
wget -r http://target.com/.git/
# 使用工具恢复
python3 GitHacker.py http://target.com/
# 查看提交历史
git log
git show HEAD
git diff HEAD~1 HEAD搜索敏感信息
# 搜索提交历史中的关键字
git log --all --grep="password"
git log --all --grep="flag"
git log --all --grep="secret"
# 搜索文件变更
git log -p --all -S "password"常见问题
克隆很慢
可以使用浅克隆:
git clone --depth 1 https://github.com/example/repo.git中文文件名显示异常
git config --global core.quotepath false不小心提交了敏感信息
先停止公开推送,通知维护者处理历史记录。不要只删当前文件。
分支合并冲突
1. git status 查看冲突文件
2. 编辑文件解决冲突
3. git add 添加解决后的文件
4. git commit 完成合并推送被拒绝
# 先拉取再推送
git pull origin main
git push origin main
# 或使用 rebase
git pull --rebase origin main
git push origin main误操作恢复
# 查看操作历史
git reflog
# 恢复到某个操作前
git reset --hard HEAD@{2}关联
- 环境与工具最小准备
- 写WP不是写结果
- Python环境
- Docker