【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 开发项目中的典型工作流程,包括分支管理、提交、合并和远程操作等核心功能。

相关推荐
NocoBase2 小时前
【2.0 教程】第 1 章:认识 NocoBase ,5 分钟跑起来
数据库·人工智能·开源·github·无代码
量子位7 小时前
字节版龙虾架构火爆GitHub!开源获35k+ Star,内置Skill全家桶,原生适配飞书
github·ai编程
悠然大月季7 小时前
git 怎么导出提交历史,文件是乱码
git·git导出历史记录·git导出历史乱码
chenshiming8029 小时前
在cursor下执行GIT回退版本
git
汪海游龙11 小时前
开源项目 Trending AI 招募 Google Play 内测人员(12 名)
android·github
HealthScience12 小时前
github怎么授权ssh(私人库授权)
运维·ssh·github
打点计时器12 小时前
Git快速上手教程
git
我才是一卓13 小时前
linux 安装简易 git 服务端并使用
linux·运维·git
CoderJia程序员甲13 小时前
GitHub 热榜项目 - 日榜(2026-03-22)
人工智能·ai·大模型·github·ai教程