Git 关键命令和工作流,涵盖版本控制、分支和协作。
Git 基本要求是一项面向实际任务的技能,主要用于版本控制和协作的Essitual Git 命令是一项面向实际任务的技能,主要用于Initial Setup.;Basic Workingflow.;
Branch management.;Merging.;Remote O;它将相关步骤、工具调用和结果整理方式集中到统一流程中,帮助使用者更快完成目标并减少重复操作。从功能定位来看,该技能强调把分散的操作要求整理成清晰、可复用的处理流程,使用户能够围绕既定目标快速准备输入、选择执行方式并获得结构化结果。
实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。该技能适合用于一次性任务,也可以接入自动化工作流,与其他技能或上层代理配合完成更完整的业务链路;在组合使用时,应明确每一步的输入输出关系,并避免不同步骤之间出现参数冲突。
用于版本控制与协作的核心 Git 命令。
# 配置用户信息 git config --global user.name "Your Name" git config --global user.email "your@email.com" # 初始化仓库 git init # 克隆仓库 git clone https://github.com/user/repo.git git clone https://github.com/user/repo.git custom-name
# 查看状态 git status # 将文件加入暂存区 git add file.txt git add . git add -A # 包含删除在内的所有变更 # 提交变更 git commit -m "Commit message" # 一步完成暂存并提交 git commit -am "Message" # 修改上一次提交 git commit --amend -m "New message" git commit --amend --no-edit # 保留原有提交信息
# 显示未暂存的变更 git diff # 显示已暂存的变更 git diff --staged # 显示特定文件的变更 git diff file.txt # 显示两次提交之间的变更 git diff commit1 commit2
# 列出本地分支 git branch git branch -a # 同时列出远程分支 # 创建分支 git branch feature-name # 切换分支 git checkout feature-name git switch feature-name # 现代替代命令 # 创建并切换到新分支 git checkout -b feature-name git switch -c feature-name # 删除分支 git branch -d branch-name git branch -D branch-name # 强制删除 # 重命名分支 git branch -m old-name new-name
# 将分支合并至当前分支 git merge feature-name # 合并时不使用快进(fast-forward)模式 git merge --no-ff feature-name # 中止合并操作 git merge --abort # 显示合并冲突的文件列表 git diff --name-only --diff-filter=U
# 列出远程仓库 git remote -v # 添加远程仓库 git remote add origin https://github.com/user/repo.git # 更改远程仓库 URL git remote set-url origin https://github.com/user/new-repo.git # 移除远程仓库 git remote remove origin
# 从远程获取更新(不合并) git fetch origin # 拉取变更(获取 + 合并) git pull # 使用变基方式拉取 git pull --rebase # 推送变更 git push # 推送新分支(并设置上游跟踪) git push -u origin branch-name # 强制推送(请谨慎使用!) git push --force-with-lease
# 显示提交历史 git log # 每次提交仅显示一行 git log --oneline # 以图形化方式显示简明历史(含所有分支) git log --graph --oneline --all # 显示最近 N 次提交 git log -5 # 按作者筛选提交 git log --author="Name" # 按日期范围筛选提交 git log --since="2 weeks ago" git log --until="2024-01-01" # 查看某文件的历史 git log -- file.txt
# 搜索提交信息中包含关键词的记录 git log --grep="bug fix" # 搜索代码中新增或删除了指定字符串的提交 git log -S "function_name" # 显示每行代码最后由谁修改 git blame file.txt # 定位引入缺陷的提交(二分查找) git bisect start git bisect bad git bisect good commit-hash
# 丢弃某个文件的修改 git restore file.txt git checkout -- file.txt # 旧方式 # 丢弃所有未暂存的修改 git restore .
# 取消暂存某个文件 git restore --staged file.txt git reset HEAD file.txt # 旧方式 # 取消暂存所有文件 git reset
# 撤销最后一次提交(保留工作区和暂存区变更) git reset --soft HEAD~1 # 撤销最后一次提交(同时丢弃工作区和暂存区变更) git reset --hard HEAD~1 # 回退某次提交(创建一个反向的新提交) git revert commit-hash # 重置当前分支至指定提交(危险操作) git reset --hard commit-hash
# 储藏当前工作区与暂存区的变更
git stash
# 带描述信息储藏
git stash save "Work in progress"
# 列出所有储藏
git stash list
# 应用最新的储藏(不删除)
git stash apply
# 应用并删除最新的储藏
git stash pop
# 应用指定的储藏
git stash apply stash@{2}
# 删除指定储藏
git stash drop stash@{0}
# 清空所有储藏
git stash clear
# 将当前分支变基到 main 分支 git rebase main # 交互式变基(最近 3 次提交) git rebase -i HEAD~3 # 解决冲突后继续变基 git rebase --continue # 跳过当前提交的变基 git rebase --skip # 中止变基操作 git rebase --abort
# 列出所有标签 git tag # 创建轻量标签 git tag v1.0.0 # 创建附注标签 git tag -a v1.0.0 -m "Version 1.0.0" # 为指定提交打标签 git tag v1.0.0 commit-hash # 推送单个标签 git push origin v1.0.0 # 推送所有标签 git push --tags # 删除本地标签 git tag -d v1.0.0 # 删除远程标签 git push origin --delete v1.0.0
# 应用某次提交的变更 git cherry-pick commit-hash # 挑选提交但不自动提交(需手动 git commit) git cherry-pick -n commit-hash
# 添加子模块 git submodule add https://github.com/user/repo.git path/ # 初始化子模块(读取 .gitmodules) git submodule init # 更新子模块至其对应提交 git submodule update # 克隆时递归初始化子模块 git clone --recursive https://github.com/user/repo.git
# 预览将被删除的未跟踪文件(试运行) git clean -n # 删除未跟踪文件 git clean -f # 删除未跟踪文件及目录 git clean -fd # 同时删除被 .gitignore 忽略的文件 git clean -fdx
功能分支工作流:
git checkout -b feature/new-feature # 进行开发修改 git add . git commit -m "Add new feature" git push -u origin feature/new-feature # 创建 PR,待合并后: git checkout main git pull git branch -d feature/new-feature
热修复工作流:
git checkout main git pull git checkout -b hotfix/critical-bug # 修复缺陷 git commit -am "Fix critical bug" git push -u origin hotfix/critical-bug # 合并完成后: git checkout main && git pull
同步 Fork 仓库:
git remote add upstream https://github.com/original/repo.git git fetch upstream git checkout main git merge upstream/main git push origin main
添加至 ~/.gitconfig 文件:
[alias]
st = status
co = checkout
br = branch
ci = commit
unstage = reset HEAD --
last = log -1 HEAD
visual = log --graph --oneline --all
amend = commit --amend --no-edit
.gitignore 排除无需纳入版本控制的文件--force)git pull 同步最新变更main 分支上开展开发rebase 整理提交历史--force-with-lease 替代 --force撤销误提交:
git reset --soft HEAD~1
恢复已删除分支:
git reflog git checkout -b branch-name
修正错误的提交信息:
git commit --amend -m "Correct message"
解决合并冲突:
# 编辑冲突文件完成手动解决 git add resolved-files git commit # 或 git merge --continue
官方文档:https://git-scm.com/doc
《Pro Git》电子书:https://git-scm.com/book
可视化 Git 指南:https://marklodato.github.io/visual-git-guide/