Help us learn about your current experience with the documentation. Take the survey.

稍后暂存更改

当你想切换到另一个分支,并且想要存储尚未准备好提交的更改时,使用 git stash

  • 暂存未提交的更改且不带消息:

    git stash
  • 暂存未提交的更改并附带消息:

    git stash save "this is a message to display on the list"
  • 从暂存中检索更改并应用到你的分支:

    git stash apply
  • 将暂存中的特定更改应用到你的分支:

    git stash apply stash@{3}
  • 查看暂存中的所有更改:

    git stash list
  • 查看带有更多信息的暂存更改列表:

    git stash list --stat
  • 从暂存中删除最近暂存的更改:

    git stash drop
  • 从暂存中删除特定更改:

    git stash drop <name>
  • 删除暂存中的所有更改:

    git stash clear
  • 应用最近暂存的更改并从暂存中删除它:

    git stash pop

如果你在暂存更改后又做了大量修改,当将这些之前的更改应用到你的分支时可能会发生冲突。你必须解决这些冲突,然后才能从暂存中应用更改。

Git 暂存示例工作流

亲自尝试使用 Git 暂存功能:

  1. 在 Git 仓库中修改一个文件。

  2. 暂存修改:

    git stash save "Saving changes from edit this file"
  3. 查看暂存列表:

    git stash list
  4. 确认没有待处理的更改:

    git status
  5. 应用暂存的更改并从暂存中删除该更改:

    git stash pop
  6. 查看暂存列表以确认更改已被删除:

    git stash list