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 命令。
Initial Setup
配置用户
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
Basic Workflow
Staging and committing
##### 查看状态 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 # 保持原提交信息
Viewing changes
##### 查看未暂存的改动 git diff
##### 查看已暂存的改动
git diff --staged
##### 查看指定文件的改动
git diff file.txt
##### 查看两次提交之间的差异
git diff commit1 commit2
Branching & Merging
Branch management
##### 列出分支 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
Merging
##### 将分支合并到当前分支 git merge feature-name
##### 禁用 fast-forward 合并
git merge --no-ff feature-name
##### 中止合并
git merge --abort
##### 查看冲突文件
git diff --name-only --diff-filter=U
Remote Operations
Managing remotes
##### 列出远程仓库 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
Syncing with remote
##### 从远程获取更新 git fetch origin
##### 拉取并合并(fetch + merge)
git pull
##### 使用 rebase 拉取
git pull --rebase
##### 推送改动
git push
##### 推送新分支并建立跟踪关系
git push -u origin branch-name
##### 强制推送(请谨慎使用!)
git push --force-with-lease
History & Logs
Viewing history
##### 查看提交历史 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
Searching history
##### 在提交信息中搜索 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
Undoing Changes
Working directory
##### 丢弃文件改动 git restore file.txt
git checkout -- file.txt # 旧方式
##### 丢弃所有改动
git restore .
Staging area
##### 取消暂存 git restore --staged file.txt
git reset HEAD file.txt # 旧方式
##### 取消全部暂存
git reset
Commits
##### 撤销最近一次提交但保留改动 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"
##### 列出所有暂存
git stash list
##### 应用最新暂存
git stash apply
##### 应用并移除暂存
git stash pop
##### 应用指定暂存
git stash apply stash@{2}
##### 删除暂存
git stash drop stash@{0}
##### 清空所有暂存
git stash clear
Rebasing
##### 对当前分支进行 rebase git rebase main
##### 交互式 rebase(最近 3 次提交)
git rebase -i HEAD~3
##### 解决冲突后继续
git rebase --continue
##### 跳过当前提交
git rebase --skip
##### 中止 rebase
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
Advanced Operations
Cherry-pick
##### 应用指定提交 git cherry-pick commit-hash
##### cherry-pick 但不提交
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
##### 包含被 .gitignore 忽略的文件
git clean -fdx
Common Workflows
Feature branch workflow:
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 workflow:
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
Syncing 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
Add to~/.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
- 频繁提交,后期完善(交互式 rebase)
- 编写有意义的提交信息
- 使用 .gitignore 排除不需要的文件
- 切勿对共享分支使用强制推送
- 开始工作前先 pull
- 使用 feature branch 而非直接在 main 上开发
- 合并前对 feature branch 进行 rebase
- 使用
--force-with-lease替代--force
Common Issues
Undo accidental commit:
git reset --soft HEAD~1
Recover deleted branch:
git reflog
git checkout -b branch-name
Fix wrong commit message:
git commit --amend -m "Correct message"
Resolve merge conflicts:
# 编辑文件解决冲突
git add resolved-files
git commit # 或 git merge --continue
Documentation
- Official docs: https://git-scm.com/doc
- Pro Git book: https://git-scm.com/book
- Visual Git guide: https://marklodato.github.io/visual-git-guide/