如何 git stash 特定文件?
如何存储特定文件,而将当前修改的其他文件从我要保存的存储中保留出来?
例如,如果 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
编辑:从 git 2.13 开始,有一个命令可以将特定路径保存到存储:
git stash push
。例如:旧答案:
您可以使用
git stash --patch
(或git stash -p
)来做到这一点 - 您将输入交互模式中,您将看到每个已更改的块。使用n
跳过您不想存储的文件,使用y
当您遇到要存储的文件时使用q
退出并留下剩下的帅哥。a
会将显示的块和其余块存储在该文件中。这不是最用户友好的方法,但如果您确实需要它,它可以完成工作。
EDIT: Since git 2.13, there is a command to save a specific path to the stash:
git stash push <path>
. For example:OLD ANSWER:
You can do that using
git stash --patch
(orgit stash -p
) -- you'll enter interactive mode where you'll be presented with each hunk that was changed. Usen
to skip the files that you don't want to stash,y
when you encounter the one that you want to stash, andq
to quit and leave the remaining hunks unstashed.a
will stash the shown hunk and the rest of the hunks in that file.Not the most user-friendly approach, but it gets the work done if you really need it.
我通常添加到我不想隐藏的索引更改,然后使用
--keep-index
选项进行隐藏。最后一步是可选的,但通常是您需要的。它从索引中删除更改。
警告
正如评论中所述, git stash --keep-index 将所有内容推送到存储中,包括暂存的和未暂存的。
--keep-index
只是在存储完成后保留索引。当您稍后弹出存储时,这可能会导致合并冲突。I usually add to index changes I don't want to stash and then stash with
--keep-index
option.The last step is optional, but usually, you want it. It removes changes from the index.
Warning
As noted in the comments,
git stash --keep-index
pushes everything onto the stash, both staged and unstaged. The--keep-index
just leaves the index alone after the stash is done. This can cause merge conflicts when you later pop the stash.存储一个文件:
要在命令中给出一条消息而不是在提示时输入消息,请在文件部分之前添加 -m,例如
git stash -m "stash-message" -- filename1.txt
对于 存储多个文件:
For stashing one file:
To give a message in the command rather than enter it when prompted, add -m before the file part, e.g.
git stash -m "stash-message" -- filename1.txt
For stashing more than one file:
要添加到 svick 的答案中,
-m
选项只是将一条消息添加到您的存储中,并且完全是可选的。因此,该命令完全有效。例如,如果我只想将更改存储在 src/ 目录中,我可以运行
To add to svick's answer, the
-m
option simply adds a message to your stash, and is entirely optional. Thus, the commandis perfectly valid. So for instance, if I want to only stash changes in the
src/
directory, I can just run如果您使用 Visual Studio 代码,则有一种更简单的方法来存储选定的文件。
If you are using visual studio code there is a simpler way to stash selected files.
简短而简单的解决方案:
在您的情况下
git stash -- app/views/cart/welcome.thtml
Short and Simple solution:
in your case
git stash -- app/views/cart/welcome.thtml
(确保您的更改可以隐藏)
如果您只想存储 dl.go
显示您的stash list
然后签出您的分支并应用 stash
(make sure your changes that can stash)
If you want to stash only dl.go
Show your stash list
Then checkout your branch and apply stash
从 git
2.35.0
开始,您可以使用--staged | 存储暂存的更改。 -S
标志。例如:
https://git-scm .com/docs/git-stash/2.35.0#Documentation/git-stash.txt---staged
这个新功能的好处在于,它不仅可以存储特定的未跟踪文件,而且还可以可以将代码更改的特定部分隐藏在跟踪文件中;当您使用
--patch | 将它们添加到舞台时-p
标志。Since git
2.35.0
you can stash staged changes using--staged | -S
flag.For instance:
https://git-scm.com/docs/git-stash/2.35.0#Documentation/git-stash.txt---staged
The nice part about this new feature is that not only it's possible to stash specific untracked files, but also it's possible to stash specific part of the code changes in tracked files; when you add those to stage using
--patch | -p
flag.如果您可以使用 GIT GUI 客户端,那么自 2020 年 5 月起 Fork 就可以无缝地完成此操作。GIF部分隐藏功能将比任何文字更好地显示这一点:
请注意,Fork(这对 Google 来说是一个很难理解的名字!)不是免费软件,评估期后费用为 50 美元,但您可以像对待 WinRAR 或 WinZip 一样忽略弹出窗口。
If you're OK with using a GIT GUI client, Fork can pretty seamlessly do this as of May 2020. A GIF of the partial stash functionality will show this better than any words:
Note that Fork (which is a difficult name to Google for!) is not free software and costs $50 after the evaluation period, but you can just ignore the popups like you do for WinRAR or WinZip.
@svick 发布了一个很好的答案。我想隐藏所有 .java 文件并保持 build.gradle 不变,所以我运行:
@svick has posted a great answer. I wanted to stash all my .java files and leave build.gradle untouched so I ran:
我首选的方法(我认为最简单的方法)很简单:
git stash --
或
git stash -- path/to/directory/file.py< /代码>
My preferred method (the easiest in my opinion) is simply:
git stash -- <path/to/directory>
or
git stash -- path/to/directory/file.py
现在已存储未暂存的文件。
查看包含您命名的存储的存储列表:
要恢复存储的文件:
存储在 WIP220412-1119am 中的更改现已恢复。
并且存储列表也保留下来,(而不是“git stash pop”,您可以通过这种方式保留列表。)
The unstaged files are now stashed.
See the stash list with your named stash:
To restore the stashed files:
The changes stashed in WIP220412-1119am are now restored.
And the stash list remains as well, (instead of "git stash pop", you can retain the list this way.)
我认为使用 git stash push 会很好!它还支持模式。
git stash pushwelcome.*ml
将存储任何以welcome.
开头并以ml
结尾的文件。这适合你。I think using
git stash push <path>
will be nice! it also supports patterns.git stash push welcome.*ml
will stash any start withwelcome.
and end withml
files. This is suitable for you.在 VS-Code 的“源代码管理”选项卡中,按住 Shift 键,然后选择要存储的文件,然后右键单击并选择“存储更改”选项。
In Source Control tab of vs-code, hold shift key and then select the files you want to stash, then right click and choose stash changes option.