我如何保存一个特定的文件,而不保存当前修改的其他文件?

例如,如果git状态告诉我:

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

当前回答

简单快捷的解决方案:

git stash -- finename.ext

在您的情况下,git stash--app/views/cart/welcome.thtml

其他回答

在vs代码的源代码控制选项卡中,按住shift键,然后选择要隐藏的文件,然后右键单击并选择隐藏更改选项。

对于隐藏一个文件:

git stash -- filename.txt

对于存储多个文件:

git stash -- filename1.txt filename2.txt

简单快捷的解决方案:

git stash -- finename.ext

在您的情况下,git stash--app/views/cart/welcome.thtml

如果您使用的是visual studio代码,则有一种更简单的方法来隐藏选定的文件。

确保您已在VSCode中安装GitLens扩展转到“源代码管理”选项卡选择要隐藏的文件右键单击它,您将看到许多选项。单击Stash Changes

现在它会要求您添加一些隐藏消息。添加可理解的消息并点击回车。

@斯维克发布了一个很棒的答案。我想把我所有的.java文件都藏起来,让build.gradle保持不变,所以我跑了:

git stash push *.java