在 Git 中应用缓存

John Wachira 2022年4月22日
在 Git 中应用缓存

本文将介绍在 Git 中应用缓存的不同方法。它将显示应用特定 Git 缓存的步骤。

在 Git 中应用缓存

如果你不知道,我们使用 git stash list 来显示我们的 Git 缓存。

pc@JOHN MINGW64 ~/Git (main)
$ git stash list
stash@{0}: WIP on main: 78129a6 Revert "$git status"
stash@{1}: WIP on main: 195e5c3 $git status

你可以看到我们的 Git 分支中有两个缓存区。

运行 git stash apply 将指向堆栈的顶部。如下所示,如果要应用特定的缓存,则需要添加参数。

$ git stash apply stash@{stash_index}

在我们的例子中,我们想要应用索引 1 缓存。我们运行下面的命令。

$ git stash apply stash@{1}
Removing text.txt.txt
CONFLICT (modify/delete): Tutorial.txt deleted in Updated upstream and modified in Stashed changes. Version Stashed changes of Tutorial.txt left in tree.
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    text.txt.txt
        modified:   text.txt.txt.bak
Unmerged paths:
  (use "git restore --staged <file>..." to unstage)
  (use "git add/rm <file>..." as appropriate to mark resolution)
        deleted by us:   Tutorial.txt
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .bash_history.bak
        Delftscopetech/
        New folder/
        file.patch

但是,在应用时,此命令不会从你的列表中删除缓存。你应该运行一个 git stash pop 命令。

git stash applygit stash pop 之间存在差异,后者应用更改并从你的列表中删除缓存。

让我们看一个例子。

pc@JOHN MINGW64 ~/Git/Delftscopetech (main)
$ git stash pop stash@{0}
Removing README.md
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    README.md
no changes added to commit (use "git add" and/or "git commit -a")
Dropped stash@{0} (1531151482186b97f47e9b852ac29ddd194bd099)

我们可以看到 Git 应用了我们的缓存并将其从上面的输出中删除。

另一种方法是运行 git stash drop stash@{Index}。要清除所有缓存,请运行 git stash clear 命令。

作者: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

相关文章 - Git Stash