Skip to content

Stash Workflow 练习

简单 约 15 分钟

你将构建什么

仓库哪里你 simulate interrupted workflow: 你是 mid-feature, urgent bug 需要 fixing on 主要, 你 stash 你的工作, fix bug, 然后 pop stash 到恢复.

学习目标

  • Stash uncommitted 更改使用 git stash
  • 切换分支 and fix urgent issue
  • Restore stashed 更改使用 git stash pop
  • 理解 stash stack (列表, apply, drop)

前置要求

  • git init, 添加, 提交, 分支, checkout

步骤

  1. 1

    设置场景

    创建仓库使用初始提交 on 主要, 然后开始工作 on feature 分支使用 uncommitted 更改.

    代码
    mkdir stash-practice
    cd stash-practice
    git init
    echo "console.log('app')" > app.js
    git add .
    git commit -m "Initial app"
    git checkout -b feature/new-ui
    echo "// new UI work in progress" >> app.js
    echo "const ui = 'new';" >> app.js

    预期结果

    你是 on feature/新-ui 使用 uncommitted 更改到 app.js.

  2. 2

    Stash 你的工作到 fix urgent bug

    urgent bug 是 reported on 主要. Stash 你的 feature 工作, 切换到 主要, and fix bug. Stashing 保存你的更改 temporarily and cleans 工作 directory.

    代码
    git stash
    git checkout main
    echo "// bugfix: fix crash" >> app.js
    git add app.js
    git commit -m "Fix urgent crash bug"

    预期结果

    你的 feature 更改是 stashed; 主要有 新 bugfix 提交.

  3. 3

    返回到 你的 feature and restore 工作

    切换后面到 feature 分支 and pop stash 到 restore 你的 uncommitted 更改. 你的工作-in-progress 是后面精确地 as 你左 它.

    代码
    git checkout feature/new-ui
    git stash pop
    git status

    预期结果

    你的 stashed 更改是 restored; app.js shows 新 UI 工作.

  4. 4

    Continue and 提交你的 feature

    现在那个你的工作是 restored, finish feature and 提交它. stash workflow let 你 handle interruption 不使用 losing 你的工作.

    代码
    git add app.js
    git commit -m "Add new UI work in progress"

    预期结果

    你的 feature 工作是 committed on feature 分支.

  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 显示预期的提交历史。

自己动手试试

你学到了什么

你练习 stash workflow — 真实-世界技巧 for handling interruptions 不使用 losing uncommitted 工作. Stash, 切换, fix, 返回, pop.

相关资源

相关课程

  • git stash

    学习 stash, pop, 列表, and drop.

相关速查表

查阅不认识的术语

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

查看术语表

解读错误信息

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

查看错误

← 返回主题