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

相关推荐
QT 小鲜肉29 分钟前
【Linux命令大全】002.文件传输之uucico命令(实操篇)
linux·运维·服务器·chrome·笔记·github
wordbaby2 小时前
公私分明:为什么你不应该共用 SSH Key(附多账号最佳实践指南)
前端·git·ssh
逛逛GitHub3 小时前
新挖掘了 3 个牛哄哄的 GitHub 教程库,有点意思啊。
github
Albert Edison3 小时前
【Git】分支管理
大数据·git·elasticsearch
Forest_HAHA4 小时前
<4>,Git多人协作
git
CNRio6 小时前
Day 43:Git的高级技巧:使用Git的rebase简化提交历史
大数据·git·elasticsearch
少云清6 小时前
【接口测试】2_持续集成 _Git与Gitee
git·ci/cd·gitee
Star_KeyW6 小时前
【超详细】Git基础命令使用
git
一点一木6 小时前
🚀 2025 年 12 月 GitHub 十大热门项目排行榜 🔥
前端·人工智能·github
学好statistics和DS7 小时前
SSH, GitHub
运维·ssh·github