【DevOps】Git 命令实战:一个简单的 Web 开发项目示例

Git 命令实战:一个简单的 Web 开发项目示例

让我们通过一个具体的 Web 开发项目来讲解 Git 的常用命令。假设我们要开发一个简单的个人博客网站。

1.项目初始化

  1. 创建项目并初始化 Git 仓库
bash 复制代码
mkdir my-blog
cd my-blog
git init
  1. 创建基础文件
bash 复制代码
touch index.html style.css README.md

2.开发流程

2.1 第一次提交

  1. 检查状态
bash 复制代码
git status
  1. 添加文件到暂存区
bash 复制代码
git add index.html style.css README.md
# 或者使用
git add .
  1. 提交更改
bash 复制代码
git commit -m "Initial commit: created basic project structure"

2.2 创建新功能分支

  1. 创建并切换到新分支
bash 复制代码
git checkout -b feature/homepage
  1. 在index.html中添加内容
html 复制代码
<!-- 编辑index.html -->
<!DOCTYPE html>
<html>
<head>
    <title>My Blog</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <header>
        <h1>Welcome to My Blog</h1>
    </header>
</body>
</html>
  1. 提交更改
bash 复制代码
git add index.html
git commit -m "Add basic homepage structure"

2.3 处理紧急修复

  1. 切换回主分支
bash 复制代码
git checkout main
  1. 创建修复分支
bash 复制代码
git checkout -b hotfix/readme-typo
  1. 修复 README.md 中的错误
bash 复制代码
# 编辑 README.md 文件
git add README.md
git commit -m "Fix typo in README"
  1. 合并修复到主分支
bash 复制代码
git checkout main
git merge hotfix/readme-typo
  1. 删除临时分支
bash 复制代码
git branch -d hotfix/readme-typo

2.4 继续开发功能

  1. 回到功能分支
bash 复制代码
git checkout feature/homepage
  1. 添加更多内容到CSS
css 复制代码
/* 编辑style.css */
body {
    font-family: Arial, sans-serif;
    line-height: 1.6;
    margin: 0;
    padding: 0;
}

header {
    background: #333;
    color: #fff;
    padding: 1rem;
    text-align: center;
}
  1. 提交CSS更改
bash 复制代码
git add style.css
git commit -m "Add basic styling for homepage"

2.5 合并功能到主分支

  1. 切换回主分支
bash 复制代码
git checkout main
  1. 合并功能分支
bash 复制代码
git merge feature/homepage
  1. 解决可能的冲突

(如果有冲突,Git 会提示你解决)

  1. 删除功能分支
bash 复制代码
git branch -d feature/homepage

3.远程仓库操作

  1. 添加远程仓库
bash 复制代码
git remote add origin https://github.com/yourusername/my-blog.git
  1. 推送更改到远程
bash 复制代码
git push -u origin main
  1. 从远程拉取更新
bash 复制代码
git pull origin main

4.其他常用操作

  1. 查看提交历史
bash 复制代码
git log --oneline --graph
  1. 撤销工作区更改
bash 复制代码
git checkout -- style.css  # 丢弃style.css的未暂存更改
  1. 修改最后一次提交
bash 复制代码
git commit --amend -m "New commit message"
  1. 暂存当前工作
bash 复制代码
git stash  # 临时保存未提交的更改
git stash pop  # 恢复暂存的更改

这个示例展示了 Git 在 Web 开发项目中的典型工作流程,包括分支管理、提交、合并和远程操作等核心功能。

相关推荐
wangruofeng7 小时前
7.1 万 Star 的 MinerU,把 PDF 变成 LLM 能读懂的 Markdown
github
wangruofeng7 小时前
75k Star 的 OCR 传奇——Tesseract 的 40 年,从 HP 实验室到 LSTM 神经网络
github
逛逛GitHub10 小时前
又挖到 3 个不错的 GitHub 项目,尤其是第 2 个。
github
逛逛GitHub10 小时前
GitHub 上 13 万星的爬虫神器,不要 API Key 就能用了。
github
kyriewen10 小时前
别再每次都 Google 了:我整理了前端日常最常踩的 10 个 Git 坑,附速查表
前端·javascript·git
一点一木17 小时前
🚀 2026 年 6 月 GitHub 十大热门项目排行榜 🔥
人工智能·github
OpenTiny社区2 天前
从零开发 AI 聊天页要两周?试试这款 Vue3 垂直对话组件库 TinyRobot,直接开箱即用
前端·vue.js·github
逛逛GitHub2 天前
2 万多 Star!Google 开源了这个神级 GitHub 项目。
github
逛逛GitHub2 天前
免费 Token 烧掉 5 万亿之后,他们出了个一站式创作平台。
github
用户805533698032 天前
RK-Forge外设系列开篇 - 把板子从「能启动」变成「能用」:Ethernet/SPI/MMC 三个纯接线外设
linux·github·嵌入式