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 官方网站下载安装。
技能文档
适用于版本控制和协作的 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
修改远程地址
git remote set-url origin https://github.com/user/new-repo.git
删除远程仓库
git remote remove origin
与远程同步
从远程获取数据
git fetch origin
拉取并合并(fetch + merge)
git pull
拉取时使用 rebase
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
暂存栈(Stash)
保存改动
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
清理未跟踪文件
预览将要删除的文件
git clean -n
删除未跟踪文件
git clean -f
删除未跟踪文件和目录
git clean -fd
同时删除被 .gitignore 忽略的文件
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
# 修复代码
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
有用的别名(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
小技巧
- 经常提交,后期通过交互式 rebase 完善历史
- 编写有意义的提交信息
- 使用 .gitignore 忽略不需要的文件
- 切勿对共享分支使用强制推送(force)
- 开始工作前先 pull 最新代码
- 使用 feature 分支而非直接在 main 上开发
- 合并前先 rebase feature 分支
- 使用
--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/