如何检查 Git 储藏是否已应用

GitGitBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在本次实验中,你将学习如何检查 Git 储藏 (stash) 是否已成功应用到你的工作目录。我们将探讨 git stash 命令与仓库状态之间的交互,展示如何验证已应用的更改,并将其与未应用的储藏区分开来。

通过实际操作步骤,你将使用 git status 来观察储藏和应用更改前后工作目录的状态。你还将使用 git stash list 来管理和检查你的储藏条目,确保你能够自信地确定已储藏工作的应用状态。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("Git")) -.-> git/BasicOperationsGroup(["Basic Operations"]) git(("Git")) -.-> git/DataManagementGroup(["Data Management"]) git/BasicOperationsGroup -.-> git/status("Check Status") git/DataManagementGroup -.-> git/stash("Save Changes Temporarily") git/DataManagementGroup -.-> git/restore("Revert Files") subgraph Lab Skills git/status -.-> lab-560105{{"如何检查 Git 储藏是否已应用"}} git/stash -.-> lab-560105{{"如何检查 Git 储藏是否已应用"}} git/restore -.-> lab-560105{{"如何检查 Git 储藏是否已应用"}} end

检查应用更改后的 Git 状态

在这一步中,我们将探讨 Git 的 stash 命令如何与工作目录进行交互,以及如何在应用储藏 (stash) 后检查仓库的状态。

首先,确保你位于项目目录中。打开终端并导航到 my-time-machine 目录:

cd ~/project/my-time-machine

现在,创建一个新文件并添加一些内容,以模拟存在未提交的更改:

echo "This is a new feature." > feature.txt
echo "Adding some more content." >> message.txt

现在,你已经创建了一个新文件 feature.txt 并修改了现有的 message.txt。使用 git status 查看 Git 如何识别这些更改:

git status

你应该会看到输出,表明 feature.txt 是未跟踪的文件,而 message.txt 已被修改:

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   message.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        feature.txt

no changes added to commit (use "git add" and/or "git commit -a")

现在,将这些更改储藏起来。储藏就像是暂时把当前的工作放在一边,这样你就可以切换到其他任务,而无需提交未完成的更改。

git stash save "Work in progress"

你应该会看到确认储藏已保存的输出:

Saved working tree and index state On master: Work in progress

现在,你的工作目录应该是干净的,就好像你没有进行那些更改一样。你可以使用 git status 来验证这一点:

git status

输出应该显示工作目录是干净的:

On branch master
nothing to commit, working tree clean

现在,应用刚刚创建的储藏。应用储藏会将储藏的更改恢复到你的工作目录中。

git stash apply

你应该会看到表明更改已应用的输出:

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   message.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        feature.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9)

最后,再次检查 git status,以查看应用储藏后工作目录的状态。

git status

输出应该显示,储藏中的更改现在已回到你的工作目录中,显示为已修改和未跟踪的文件,就像我们储藏它们之前一样。这证实了 git stash apply 会将更改恢复,但不会自动暂存或提交它们。

使用 git stash list 进行验证

在这一步中,你将学习如何使用 git stash list 命令查看已保存的储藏 (stash) 列表。该命令有助于跟踪你的临时工作。

确保你仍处于 ~/project/my-time-machine 目录中。

在上一步中,你创建了一个储藏。现在,让我们查看储藏列表。输入以下命令:

git stash list

你应该会看到类似以下的输出:

stash@{0}: On master: Work in progress

此输出表明你已保存了一个储藏。stash@{0} 是这个特定储藏的标识符。花括号中的数字表示它在储藏列表中的位置(0 表示最近的储藏)。"On master" 表示创建该储藏时所在的分支,而 "Work in progress" 是保存储藏时提供的备注信息。

如果你有多个储藏,它们将按顺序列出,stash@{0} 是最近的,stash@{1} 是次近的,依此类推。

git stash list 命令对于管理储藏至关重要。它能让你查看可用的储藏,并帮助你确定要应用或删除的储藏。

了解如何列出储藏是有效使用储藏功能来管理正在进行的工作的第一步。

测试未应用的储藏

在这一步中,我们将确认应用一个储藏不会将其从储藏列表中移除。这是 git stash applygit stash pop 之间的一个重要区别。

确保你仍处于 ~/project/my-time-machine 目录中。

在前面的步骤中,我们创建了一个储藏并应用了它。现在,让我们再次检查储藏列表,看看该储藏是否仍然存在。

git stash list

你应该会看到与之前相同的输出:

stash@{0}: On master: Work in progress

如你所见,即使应用了储藏,它仍然保留在储藏列表中。这意味着如果需要,你可以多次应用同一个储藏,尽管这不是常见的操作流程。

如果你想应用储藏并同时将其从列表中移除,你应该使用 git stash pop 命令,而不是 git stash applypop 操作就像是应用更改后立即删除该储藏。

让我们演示一下 git stash pop。首先,确保你的工作目录是干净的。由于我们已经应用了储藏,所以存在未提交的更改。为了进行演示,我们可以选择提交这些更改,或者丢弃它们。这里我们选择丢弃它们,使工作目录恢复干净状态。

git restore .

这个命令会丢弃工作目录中的所有更改。现在,检查状态:

git status

输出应该显示工作目录是干净的:

On branch master
nothing to commit, working tree clean

现在,让我们使用 git stash pop 来应用储藏并将其从列表中移除。

git stash pop

你应该会看到表明储藏已应用并被删除的输出:

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   message.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        feature.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9)

最后,让我们再次检查储藏列表:

git stash list

这次,你应该看不到任何输出,这表明储藏列表为空:

这证实了 git stash pop 在应用储藏后会将其移除,而 git stash apply 会将其保留在列表中。理解 applypop 之间的区别对于有效管理储藏至关重要。

总结

在本次实验中,你学习了如何检查 Git 储藏是否已成功应用到工作目录。首先,你在项目中创建并储藏了更改,然后使用 git status 来观察储藏和应用前后工作目录的状态。应用储藏后工作目录干净,表明更改已成功恢复。

你还学习了使用 git stash list 查看可用的储藏列表,并确认使用 git stash applygit stash pop 应用储藏后,该储藏会从列表中移除。这为验证特定储藏不再等待应用提供了清晰的方法。