在 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 命令。

Author: 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