0Pricing
Git Advanced: Monorepo, Submodules & Workflows · 课时

暂存与拣选更改

掌握 `git stash` 以临时保存更改,并使用 `git cherry-pick` 跨分支应用特定提交

暂存与拣选更改 是 CoddyKit 上的免费 Git Advanced: Monorepo, Submodules & Workflows 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Git Advanced: Monorepo, Submodules & Workflows 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Git Advanced: Monorepo, Submodules & Workflows 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Use Git Stash?

Mid-feature and an urgent bug fix lands? git stash temporarily shelves your uncommitted changes so you can switch branches without committing half-done work.

Saving Your Changes Temporarily

Run git stash to save your modified and staged changes, then Git resets your working directory to a clean HEAD. Your work is safely tucked away.

git status
# On branch feature-x
# Changes to be committed:
#   new file:   new_feature.txt
# Changes not staged for commit:
#   modified:   index.html

git stash
# Saved working directory and index state WIP on feature-x: ...
# HEAD is now at ...

git status
# On branch feature-x
# nothing to commit, working tree clean

Viewing Your Stash Stack

You can hold multiple stashes. git stash list shows them all, each indexed — stash@{0} being the most recent.

git stash list
# stash@{0}: WIP on feature-x: ...
# stash@{1}: WIP on bugfix-branch: ...

Bringing Stash Back: Apply

git stash apply restores your most recent stash into the working directory. Want an older one? Name it, like git stash apply stash@{1}.

git checkout bugfix-branch
git stash apply stash@{1}
# On branch bugfix-branch
# Changes not staged for commit:
#   modified:   bug.js

Apply vs. Pop: What's the Difference?

apply vs pop: apply restores changes but keeps the stash in the list; pop restores them and removes the stash. Use pop when you're done with it.

git stash list
# stash@{0}: WIP on feature-x: ...

git stash pop
# On branch feature-x
# Changes not staged for commit:
#   modified:   index.html
# Dropped stash stash@{0} (...

git stash list
# (empty output, stash is gone)

Cleaning Up Stashes

Clean up stashes you no longer need: git stash drop <id> removes one, git stash clear removes all. Clear can't be undone, so be careful.

git stash list
# stash@{0}: WIP on feature-x: ...
# stash@{1}: WIP on old-bug: ...

git stash drop stash@{1}
# Dropped stash stash@{1} (...

git stash list
# stash@{0}: WIP on feature-x: ...

Introducing Git Cherry-Pick

Need just one commit from another branch, not its whole history? git cherry-pick applies a single commit's changes onto your current branch.

Applying a Single Commit

Find the commit hash with git log --oneline, switch to your target branch, then git cherry-pick <hash> to bring just that change over.

git log --oneline
# a1b2c3d Fix: Critical typo in header
# e4f5g6h Feat: Add user profiles

git checkout main
# Switched to branch 'main'

git cherry-pick a1b2c3d
# [main] Fix: Critical typo in header
#  1 file changed, 1 insertion(+), 1 deletion(-)

Resolving Cherry-Pick Conflicts

Cherry-picking can conflict like a merge. Resolve manually, git add the files, then git cherry-pick --continue — or --abort to cancel.

git cherry-pick a1b2c3d
# Auto-merging header.js
# CONFLICT (content): Merge conflict in header.js
# error: could not apply a1b2c3d...

# (Resolve conflicts, then:)
git add header.js
git cherry-pick --continue

Stash & Cherry-Pick Quiz

You are on the feature-A branch, with uncommitted changes. An urgent fix needs to go into main. You stash your changes, switch to main, and fix the bug with a commit (hash: 789abc). Now you want to apply this fix to feature-A and return to your work.

Lesson Summary

Recap: git stash shelves uncommitted work so you can switch branches cleanly, and git cherry-pick grabs a single commit from one branch onto another.

常见问题解答

「暂存与拣选更改」课时是免费的吗?

是的 — 「暂存与拣选更改」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Git Advanced: Monorepo, Submodules & Workflows 课程的其余内容,请升级到 CoddyKit PRO。 Git Advanced: Monorepo, Submodules & Workflows 课程共包含 4 节课。

「暂存与拣选更改」这节课中我会学到什么?

掌握 `git stash` 以临时保存更改,并使用 `git cherry-pick` 跨分支应用特定提交 你通过在浏览器中直接运行的动手代码来练习 Git Advanced: Monorepo, Submodules & Workflows,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Git Advanced: Monorepo, Submodules & Workflows 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Git Advanced: Monorepo, Submodules & Workflows 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「暂存与拣选更改」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Git Advanced: Monorepo, Submodules & Workflows 课中编写并运行代码吗?

能。每节 Git Advanced: Monorepo, Submodules & Workflows 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 交互式变基与修订提交
  2. 暂存与拣选更改
  3. 使用 Reflog 恢复
  4. 二分查找:追踪问题提交
← 返回 Git Advanced: Monorepo, Submodules & Workflows