Git Essentials - Git 基础命令与版本控制工作流
v1.0.0提供常用的 Git 命令合集,帮助开发者完成版本控制、分支管理、合并、标签、子模块等常见工作流,适用于团队协作与个人项目。
33· 2.8万·0 当前·0 累计
下载技能包
License
MIT-0
运行时依赖
无特殊依赖
版本
latestv1.0.0
执行提交的示例命令:git commit -m "Commit message"
安装命令
点击复制官方npx clawhub@latest install git-essentials
镜像加速npx clawhub@latest install git-essentials --registry https://cn.longxiaskill.com 镜像可用
国内专用无需额外安装
本土化适配说明
无需额外安装
技能文档
关键 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
非快进合并
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
拉取并合并(fetch + merge)
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
找出引入 bug 的提交
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
暂存栈 (Stashing)
保存当前工作现场
git stash
带备注保存
git stash save "Work in progress"
列出所有 stash
git stash list
应用最新的 stash
git stash apply
应用并删除 stash
git stash pop
应用指定的 stash
git stash apply stash@{2}
删除指定 stash
git stash drop stash@{0}
清空所有 stash
git stash clear
变基 (Rebasing)
对当前分支进行变基
git rebase main
交互式变基(最近 3 次提交)
git rebase -i HEAD~3
解决冲突后继续变基
git rebase --continue
跳过当前提交
git rebase --skip
中止变基
git rebase --abort
标签 (Tags)
列出所有标签
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
高级操作
挑拣(Cherry-pick)
应用指定提交
git cherry-pick commit-hash
挑拣但不自动提交
git cherry-pick -n commit-hash
子模块 (Submodules)
添加子模块
git submodule add https://github.com/user/repo.git path/
初始化子模块
git submodule init
更新子模块
git submodule update
克隆时递归获取子模块
git clone --recursive https://github.com/user/repo.git
清理 (Clean)
预览将要删除的文件
git clean -n
删除未追踪的文件
git clean -f
删除未追踪的文件和目录
git clean -fd
同时删除被忽略的文件
git clean -fdx
常见工作流示例
Feature 分支工作流
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
Hotfix 工作流
git checkout main
git pull
git checkout -b hotfix/critical-bug
# 修复 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
常用别名 (Useful Aliases)
在~/.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
小贴士 (Tips)
- 经常提交,后续再完善(配合交互式变基)
- 编写有意义的提交信息
- 使用
.gitignore排除不需要的文件 - 切勿对共享分支使用强制推送
- 开始工作前先拉取最新代码
- 使用 feature 分支而非直接在 main 上开发
- 合并前对 feature 分支进行变基
- 使用
--force-with-lease替代--force
常见问题 (Common Issues)
撤销误提交
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/