小编典典

我怎样才能 git stash 一个特定的文件?

git

何存储特定文件,而将当前修改的其他文件留在我要保存的存储中?

例如,如果 git status 给我这个:

younker % gst      
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# 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:   app/controllers/cart_controller.php
#   modified:   app/views/cart/welcome.thtml
#
no changes added to commit (use "git add" and/or "git commit -a")

我只想存储 app/views/cart/welcome.thtml,我该怎么做?类似的东西(但当然这不起作用):

git stash save welcome_cart app/views/cart/welcome.thtml

阅读 1232

收藏
2022-02-13

共1个答案

小编典典

编辑:从 git 2.13 开始,有一个命令可以将特定路径保存到 stash: git stash push <path>。例如:

git stash push -m welcome_cart app/views/cart/welcome.thtml

旧答案:

您可以使用git stash --patch(或git stash -p) 来做到这一点——您将进入交互模式,您将看到每个被更改的大块。用于n跳过不想存储的文件,y当您遇到要存储的文件时,q退出并保留剩余的大块不存储。 a将在该文件中存储显示的大块和其余的大块。

不是最用户友好的方法,但如果你真的需要它,它可以完成工作。

2022-02-13