📜  git stash pop 特定 - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:30:56.608000             🧑  作者: Mango

使用 Git Stash Pop 特定命令恢复临时保存的工作进度

在 Git 中,我们可以使用 git stash 命令来临时保存当前的工作进度,以便在后续需要的时候将这些临时保存的工作进度恢复回来。然而,在实际使用中,我们可能只需要恢复某一次 stash 的内容,而不是全部恢复。

这时候,就可以使用 git stash pop <stash> 命令来恢复特定的 stash 。其中,<stash> 可以是 stash 对应的索引或者 stash 对应的 commit 标识。

使用示例

假设我们有如下的一些工作进度:

$ git status
...
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   file1.txt
    modified:   file2.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   file3.txt
...

此时,我们可以使用 git stash 命令来保存当前的工作进度:

$ git stash
Saved working directory and index state WIP on master: 1234567 Some changes

然后,我们可以继续进行新的修改:

$ echo "Some new changes" > file1.txt
$ git add file1.txt
$ git commit -m "Add some new changes"

此时,我们可以使用 git stash list 命令查看当前保存的 stash 列表:

$ git stash list
stash@{0}: WIP on master: 1234567 Some changes

如果我们需要恢复这个 stash ,只需要执行以下的命令即可:

$ git stash pop stash@{0}

这时候,我们的工作进度就会被恢复到之前保存的状态,包括所有添加和修改的文件和暂存区的状态:

$ git status
...
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   file1.txt
    modified:   file2.txt

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

    file3.txt
...

需要注意的是,这个命令会从 stash 列表中将该 stash 删除。如果你只想查看 stash 的内容,而不真正地让其进入工作进度,可以使用 git stash apply stash@{0} 命令来达到目的。

结论

git stash pop <stash> 命令可以让我们恢复特定的 stash,而不是全部恢复。在实际使用中,可以根据需要选择合适的恢复方式。