当前位置: 首页 > news >正文

Git 命令速查手册

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Git 命令速查手册</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; line-height: 1.6; color: #333; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); min-height: 100vh; padding: 20px; } .container { max-width: 900px; margin: 0 auto; background: #fff; border-radius: 16px; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3); overflow: hidden; } header { background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%); color: #fff; padding: 30px; text-align: center; } header h1 { font-size: 28px; margin-bottom: 10px; font-weight: 600; } header p { opacity: 0.9; font-size: 14px; } nav { background: #f8f9fa; padding: 15px; border-bottom: 1px solid #e9ecef; } nav ul { BackPlayProblemCirOperation list-style: none; display: flex; flex-wrap: wrap; gap: 10px; } nav a { text-decoration: none; color: #667eea; padding: 6px 12px; border-radius: 20px; font-size: 13px; transition: all 0.3s; } nav a:hover { background: #667eea; color: #fff; } .content { padding: 30px; } section { margin-bottom: 35px; } section h2 { color: #2c3e50; font-size: 20px; margin-bottom: 20px; padding-bottom: 10px; border-bottom: 2px solid #667eea; display: flex; align-items: center; gap: 10px; } section h2::before { content: ''; width: 4px; height: 20px; background: #667eea; border-radius: 2px; } pre { background: #2d2d2d; color: #ccc; padding: 16px; border-radius: 8px; overflow-x: auto; margin: 15px 0; font-family: 'Consolas', 'Monaco', monospace; font-size: 14px; line-height: 1.5; } code { background: #f4f4f4; padding: 2px 6px; border-radius: 4px; font-family: 'Consolas', monospace; font-size: 14px; color: #e74c3c; } pre code { background: none; padding: 0; color: #ccc; } .command-group { margin-bottom: 12px; } .command-group .desc { color: #666; font-size: 14px; margin-bottom: 5px; } .command-group code { color: #3498db; } table { width: 100%; border-collapse: collapse; margin-top: 15px; } table th, table td { padding: 12px; text-align: left; border-bottom: 1px solid #e9ecef; } table th { background: #f8f9fa; font-weight: 600; color: #2c3e50; } table tr:hover { background: #f8f9fa; } .tips { background: #fff3cd; border-left: 4px solid #ffc107; padding: 15px; margin-top: 20px; border-radius: 0 8px 8px 0; } .tips p { margin: 8px 0; font-size: 14px; color: #856404; } footer { background: #f8f9fa; padding: 20px; text-align: center; border-top: 1px solid #e9ecef; color: #666; font-size: 14px; } .copy-btn { float: right; background: #667eea; color: white; border: none; padding: 6px 12px; border-radius: 4px; cursor: pointer; font-size: 12px; margin-top: -40px; transition: background 0.3s; } .copy-btn:hover { background: #5a6fd6; } @media (max-width: 768px) { body { padding: 10px; } header h1 { font-size: 22px; } .content { padding: 20px; } section h2 { font-size: 18px; } nav ul { justify-content: center; } } </style> </head> <body> <div class="container"> <header> <h1>Git 命令速查手册</h1> <p>开发者必备的 Git 操作指南,按场景分类整理</p> </header> <nav> <ul> <li><a href="#init">仓库初始化</a></li> <li><a href="#workflow">基础工作流</a></li> <li><a href="#branch">分支操作</a></li> <li><a href="#remote">远程同步</a></li> <li><a href="#undo">撤销回退</a></li> <li><a href="#tag">标签管理</a></li> <li><a href="#advanced">高级技巧</a></li> <li><a href="#error">报错速查</a></li> <li><a href="#config">配置别名</a></li> </ul> </nav> <div class="content"> <section id="init"> <h2>一、仓库初始化与克隆</h2> <div class="command-group"> <div class="desc">初始化本地仓库</div> <pre><code>git init</code></pre> </div> <div class="command-group"> <div class="desc">克隆远程仓库</div> <pre><code>git clone https://github.com/user/repo.git # 克隆并指定本地目录名 git clone https://github.com/user/repo.git myproject</code></pre> </div> </section> <section id="workflow"> <h2>二、基础工作流(每天必用)</h2> <div class="command-group"> <div class="desc">查看工作区状态(高频)</div> <pre><code>git status</code></pre> </div> <div class="command-group"> <div class="desc">添加文件到暂存区</div> <pre><code>git add filename # 指定文件 git add . # 当前目录所有变更 git add -A # 整个仓库所有变更(包括删除)</code></pre> </div> <div class="command-group"> <div class="desc">提交变更</div> <pre><code>git commit -m "feat: 新增用户登录模块" git commit -am "fix: 修复并直接提交已跟踪文件"</code></pre> </div> <div class="command-group"> <div class="desc">查看提交历史</div> <pre><code>git log --oneline --graph --decorate # 简洁图形化 git reflog # 查看所有分支操作记录(救命命令)</code></pre> </div> </section> <section id="branch"> <h2>三、分支操作(协作核心)</h2> <div class="command-group"> <div class="desc">查看分支</div> <pre><code>git branch # 本地分支 git branch -r # 远程分支 git branch -a # 所有分支</code></pre> </div> <div class="command-group"> <div class="desc">创建并切换分支</div> <pre><code>git checkout -b feature/user-auth # 等价于 git branch feature/user-auth git checkout feature/user-auth</code></pre> </div> <div class="command-group"> <div class="desc">切换分支</div> <pre><code>git checkout main</code></pre> </div> <div class="command-group"> <div class="desc">合并分支</div> <pre><code>git merge feature/user-auth # 合并到当前分支 git merge --no-ff feature/user-auth # 保留分支历史</code></pre> </div> <div class="command-group"> <div class="desc">删除分支</div> <pre><code>git branch -d feature/user-auth # 安全删除(已合并) git branch -D feature/user-auth # 强制删除(未合并)</code></pre> </div> <div class="command-group"> <div class="desc">重命名分支</div> <pre><code>git branch -m old-name new-name</code></pre> </div> </section> <section id="remote"> <h2>四、远程同步(协作必备)</h2> <div class="command-group"> <div class="desc">查看远程仓库</div> <pre><code>git remote -v</code></pre> </div> <div class="command-group"> <div class="desc">添加远程仓库</div> <pre><code>git remote add origin https://github.com/user/repo.git</code></pre> </div> <div class="command-group"> <div class="desc">推送</div> <pre><code>git push -u origin main # 首次推送需加 -u 建立关联 git push # 后续直接push git push origin feature/user-auth # 推送指定分支</code></pre> </div> <div class="command-group"> <div class="desc">拉取</div> <pre><code>git pull # 拉取并自动合并(相当于 fetch + merge) git fetch origin # 仅拉取不合并(安全) git fetch --prune # 同步远程已删除的分支</code></pre> </div> <div class="command-group"> <div class="desc">追踪远程分支</div> <pre><code>git checkout -b local-branch origin/remote-branch</code></pre> </div> </section> <section id="undo"> <h2>五、撤销与回退(救命命令)</h2> <div class="command-group"> <div class="desc">工作区撤销(未add)</div> <pre><code>git checkout -- filename # 丢弃工作区修改 git checkout -- . # 丢弃所有修改</code></pre> </div> <div class="command-group"> <div class="desc">暂存区撤销(已add未commit)</div> <pre><code>git reset HEAD filename # 撤销add git reset HEAD # 撤销所有add</code></pre> </div> <div class="command-group"> <div class="desc">回退版本(已commit)</div> <pre><code>git reset --soft HEAD~1 # 回退到上一个版本,保留工作区和暂存区 git reset --mixed HEAD~1 # 默认,回退并清空暂存区 git reset --hard HEAD~1 # 彻底回退(危险,会丢失修改)</code></pre> </div> <div class="command-group"> <div class="desc">回退到指定版本</div> <pre><code>git reset --hard commit-id # 通过git log查看id(前7位即可)</code></pre> </div> <div class="command-group"> <div class="desc">远程回退(已push)</div> <pre><code>git revert commit-id # 生成新提交抵消旧提交(安全)</code></pre> </div> </section> <section id="tag"> <h2>六、标签管理(版本发布)</h2> <div class="command-group"> <div class="desc">创建标签</div> <pre><code>git tag v1.0.0 # 当前提交打标签 git tag v1.0.0 commit-id # 指定提交打标签 git tag -a v1.0.0 -m "发布1.0.0正式版" # 带说明的标签</code></pre> </div> <div class="command-group"> <div class="desc">查看标签</div> <pre><code>git tag # 列表 git show v1.0.0 # 查看标签信息</code></pre> </div> <div class="command-group"> <div class="desc">推送标签</div> <pre><code>git push origin v1.0.0 # 推送单个标签 git push origin --tags # 推送所有标签</code></pre> </div> <div class="command-group"> <div class="desc">删除标签</div> <pre><code>git tag -d v1.0.0 # 删除本地 git push origin :refs/tags/v1.0.0 # 删除远程</code></pre> </div> </section> <section id="advanced"> <h2>七、高级技巧(效率提升)</h2> <div class="command-group"> <div class="desc">暂存当前工作</div> <pre><code>git stash # 保存当前工作区 git stash list # 查看stash列表 git stash apply # 恢复最近stash(不删除) git stash pop # 恢复并删除最近stash git stash drop stash@{0} # 删除指定stash</code></pre> </div> <div class="command-group"> <div class="desc">交互式添加(分块提交)</div> <pre><code>git add -p # 按块选择要添加的内容</code></pre> </div> <div class="command-group"> <div class="desc">查看某行代码最后修改者</div> <pre><code>git blame filename # 逐行显示作者和提交</code></pre> </div> <div class="command-group"> <div class="desc">查找提交</div> <pre><code>git log --grep="关键词" # 按提交信息搜索 git log -S "代码内容" # 按代码变动搜索</code></pre> </div> <div class="command-group"> <div class="desc">清理无效文件</div> <pre><code>git clean -fd # 删除未跟踪的文件和目录(危险)</code></pre> </div> </section> <section id="error"> <h2>八、常见报错速查</h2> <table> <thead> <tr> <th>报错信息</th> <th>快速解决</th> </tr> </thead> <tbody> <tr> <td>refusing to merge unrelated histories</td> <td><code>git pull origin main --allow-unrelated-histories</code></td> </tr> <tr> <td>error: failed to push some refs</td> <td>先 <code>git pull</code> 解决冲突再 push</td> </tr> <tr> <td> Your branch is behind 'origin/main' by X commits</td> <td><code>git pull</code> 或 <code>git fetch + git merge</code></td> </tr> <tr> <td>fatal: not a git repository</td> <td>当前目录未初始化,先 <code>git init</code> 或 <code>git clone</code></td> </tr> </tbody> </table> </section> <section id="config"> <h2>九、配置与别名(个性化)</h2> <div class="command-group"> <div class="desc">全局配置</div> <pre><code>git config --global user.name "Your Name" git config --global user.email "you@example.com" git config --global core.editor "code --wait" # 设置VS Code为编辑器</code></pre> </div> <div class="command-group"> <div class="desc">常用别名(效率翻倍)</div> <pre><code>git config --global alias.st status git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --global alias.unstage 'reset HEAD --' git config --global alias.last 'log -1 HEAD' git config --global alias.visual '!gitk'</code></pre> </div> <div class="command-group"> <div class="desc">使用后:</div> <pre><code>git st # 等价于 git status git co main # 等价于 git checkout main</code></pre> </div> </section> <section> <h2>十、工作流程图解</h2> <div class="tips"> <p><strong>工作区</strong> → <code>git add</code> → <strong>暂存区</strong> → <code>git commit</code> → <strong>本地仓库</strong> → <code>git push</code> → <strong>远程仓库</strong></p> <p style="text-align: center; margin-top: 15px;"> ↑ ↓<br> <code>git checkout --</code> <code>git pull / git fetch</code> </p> </div> </section> <div class="tips"> <p>💡 <strong>重要提示:</strong></p> <p>• 任何撤销操作前先用 <code>git status</code> 确认状态</p> <p>• 重要操作前创建临时分支备份:<code>git branch backup</code></p> <p>• 团队协作前先 <code>git pull</code> 再 <code>git push</code></p> <p>• <strong>commit 信息遵循规范(如Angular规范):type: subject</strong></p> </div> </div> <footer> <p>Git 命令速查手册 | 建议收藏备用</p> <p style="margin-top: 8px; font-size: 12px;">需要针对某个场景的详细指南?请告诉我具体需求!</p> </footer> </div> <script> // 简单的平滑滚动 document.querySelectorAll('nav a').forEach(anchor => { anchor.addEventListener('click', function(e) { e.preventDefault(); const targetId = this.getAttribute('href'); const targetElement = document.querySelector(targetId); if (targetElement) { targetElement.scrollIntoView({ behavior: 'smooth' }); } }); }); </script> </body> </html>
http://www.jsqmd.com/news/890889/

相关文章:

  • 收藏!AI来了怕失业?前端老兵9年经验告诉你:会用AI才是核心竞争力!
  • 基于复数神经网络与对比预测编码的射频指纹识别技术详解
  • 深度学习地震速度建模:循环学习率与双注意力机制提升反演精度
  • 清洁方便、操作简单:高性价比全自动咖啡机怎么挑 - 品牌2025
  • Godot中落地强化学习AI的完整工程指南
  • 2026全国金属加工制品,聚焦西北区域优质企业 - 深度智识库
  • 浙江省舟山市寄快递省钱指南:海岛寄件不花冤枉钱,全国通用高性价比平台合集 - 时讯资讯
  • 苹果手机怎么把照片抠图?2026 保姆级教程,iPhone 自带抠图功能+小程序一看就会 - AI测评专家
  • 2026年5月成都黄金回收高价上门无手续费 - 润富黄金珠宝行
  • 第十七章:AI产品独有的指标体系
  • AI与大模型新闻日报20260524
  • 小红书内容采集神器XHS-Downloader:3种模式+4种场景的完整实战指南
  • 重庆母婴除甲醛CMA甲醛检测治理公司哪家好权威机构 - 五金回收
  • 手表回收套路深?广州五家正规店实地验证 - 合扬奢侈品交易中心
  • 从零部署到生产就绪,AI工具API集成全流程拆解,含12个可复用代码模板
  • 2026年新疆企业如何低成本获客:AI GEO优化、抖音搜索排名、短视频运营完全对比指南 - 精选优质企业推荐官
  • 破解业财税脱节:联拓智能软件3S一体化转型方法论如何赋能增长? - 速递信息
  • 企业法务诉讼管理系统推荐:从选型到落地的实战指南
  • 【DB_MySQL】MySQL多表关联更新
  • 【Lovable美容平台搭建实战指南】:20年架构师亲授高并发、合规性与AI美肤集成的7大避坑法则
  • 领域泛化新思路:质心相似度损失与自适应梯度融合提升语音语言识别鲁棒性
  • 告别速溶!机场全自动咖啡机让你轻松享受现磨风味 - 品牌2025
  • 湖南省怀化CPPMSCMP官网报考入口,官方授权双证报考中心 - 众智商学院课程中心
  • 收藏!小白程序员必看:5种AI Agent协调模式详解,轻松入门大模型开发
  • 审核员面试一般问什么? - 众智商学院职业教育
  • 构建多Agent系统时利用Taotoken统一调度不同模型的能力
  • 软启动厂家怎么选择?2025软启动厂家选购指南 - 速递信息
  • BIM模型精度(LOD)实战指南:从概念到竣工的精度演进与应用
  • 抚州黄金回收哪家靠谱长悦全城上门35年老店值得信赖 - 专业黄金回收
  • 许昌口碑好的别墅装修公司有哪些 - 小张小张111