Git Essentials - Git 基础命令与工作流
v1.0.0提供常用的 Git 命令和工作流示例,帮助开发者完成版本控制、分支管理、远程协作等操作,适用于新手及日常使用。
33· 2.8万·0 当前·0 累计
下载技能包
License
MIT-0
运行时依赖
无特殊依赖
版本
latestv1.0.0
git commit -m "提交信息"
安装命令
点击复制官方npx clawhub@latest install git-essentials
镜像加速npx clawhub@latest install git-essentials --registry https://cn.longxiaskill.com 镜像可用
国内专用无需额外安装
本土化适配说明
无需额外安装
技能文档
Essential Git commands for version control and collaboration.
初始设置
配置用户
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
拉取并 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
常用工作流示例
功能分支工作流
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
常用别名(Alias)
将以下内容添加到~/.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排除不需要的文件 - 切勿对共享分支使用强制推送
- 开始工作前先 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/