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

例如,如果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 -- filename.txt

对于存储多个文件:

git stash -- filename1.txt filename2.txt

其他回答

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

git stash push *.java

准备好你不想隐藏的更改。将剩余的未分页文件存放在:

$ git stash save <give_it_a_name> --keep-index

未分页的文件现在被隐藏。查看包含您的指定存储的存储列表:

$ git stash list

stash@{0}: On mybranch: WIP220412-1119am
stash@{1}: On mybranch: WIP220312-749am

要还原隐藏的文件,请执行以下操作:

$ git stash apply stash@{<index_of_saved_stash>}
$ git stash apply stash@{0}

WIP220412-1119am中隐藏的更改现在已恢复。而隐藏列表也保留了下来(而不是“git stash pop”,你可以这样保留列表)

编辑:从git2.13开始,有一个命令可以保存存储的特定路径:git stash push<path>。例如:

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

旧答案:

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

这不是最方便用户的方法,但如果您确实需要,它可以完成工作。

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

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

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

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