Skip to content

提交 History 练习

简单 约 15 分钟

你将构建什么

仓库使用 at 最少 three 提交, 每个添加 or 更改 file. 你将 读取 log and 验证那个 history tells 清晰 story 的什么 happened.

学习目标

  • 制作多个 sequential 提交使用 meaningful 消息
  • Use git log --oneline for compact history view
  • See 如何每个提交 captures distinct snapshot
  • 练习编写提交消息那个描述更改

前置要求

  • git init, 添加, and 提交 (创建仓库 Workflow 项目)

步骤

  1. 1

    设置仓库

    创建新 仓库 and 制作初始提交使用 README. 这个是 开始点 你将 构建 history on 顶部的.

    代码
    mkdir history-exercise
    cd history-exercise
    git init
    echo "# History Exercise" > README.md
    git add .
    git commit -m "Initial commit with README"

    预期结果

    仓库使用 one 提交 in 它的 history.

  2. 2

    添加其次 file and 提交

    创建新 file, stage 它, and 提交使用消息那个描述什么是 添加. 好消息 say 什么 and 为什么, 不只是 "update".

    代码
    echo "print('hello')" > hello.py
    git add hello.py
    git commit -m "Add hello.py greeting script"

    预期结果

    其次提交是 添加到 history.

  3. 3

    修改 existing file and 提交

    Append 行到 README.md, stage 它, and 提交. 修改 existing file 是只是 as 常见 as 添加新 ones — workflow 是 identical.

    代码
    echo "" >> README.md
    echo "## Files" >> README.md
    echo "- hello.py: prints a greeting" >> README.md
    git add README.md
    git commit -m "Document hello.py in README"

    预期结果

    第三提交记录 README 更新.

  4. 4

    读取 history

    运行 git log --oneline for compact one-行-每-提交 view. 然后运行 git log 到 see 完整消息, authors, and timestamps. 读取 history 是如何你 理解项目's story.

    代码
    git log --oneline
    git log

    预期结果

    Three 提交 listed, 大多数 recent 首先, 每个使用清晰消息.

  5. 5

    验证仓库状态

    使用 git status 和 git log 确认当前仓库状态干净、提交历史符合预期。在执行任何破坏性操作前,先看清当前状态是 Git 的最佳实践。

    代码
    git status
    # Should show: nothing to commit, working tree clean
    
    git log --oneline -5
    # Review the last 5 commits to confirm history
    git branch -v
    # List branches with last commit info

    预期结果

    git status 显示工作树干净;git log 显示预期的提交历史。

自己动手试试

你学到了什么

你构建 three-提交 history 使用 descriptive 消息 and 读取它 后面使用 git log — 相同 skill 你 use 每个 day 到理解什么更改 in 任何项目 and 为什么.

相关资源

相关课程

  • Git 基础

    提交, 添加, and log — commands 这个练习 drills.

  • 提交消息

    如何 编写清晰, useful 提交消息.

相关练习

相关工具

相关速查表

相关参考文档

相关错误

相关术语

  • 提交

    什么提交 represents.

  • 分支

    Independent 行的 工作.

  • 推送

    发送提交到 remote.

相关术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

  • 提交

    什么提交 represents.

  • 分支

    Independent 行的 工作.

  • 推送

    发送提交到 remote.

相关错误

用大白话解释常见错误——什么意思、为什么发生、如何修复。

← 返回主题