我怎样才能 git stash 一个特定的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5506339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I git stash a specific file?
提问by ynkr
How can I stash a specific file leaving the others currently modified out of the stash I am about to save?
如何存储特定文件,而将当前修改的其他文件从我将要保存的存储中删除?
For example, if git status gives me this:
例如,如果 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")
and I only want to stash app/views/cart/welcome.thtml, how would I do that? Something like (but of course this does not work):
我只想存储 app/views/cart/welcome.thtml,我该怎么做?类似的东西(但当然这不起作用):
git stash save welcome_cart app/views/cart/welcome.thtml
回答by svick
EDIT:Since git 2.13, there is a command to save a specific path to the stash: git stash push <path>
. For example:
编辑:从 git 2.13 开始,有一个命令可以保存特定路径到 stash: git stash push <path>
。例如:
git stash push -m welcome_cart app/views/cart/welcome.thtml
OLD ANSWER:
旧答案:
You can do that using git stash --patch
(or git stash -p
) -- you'll enter interactive mode where you'll be presented with each hunk that was changed. Use n
to skip the files that you don't want to stash, y
when you encounter the one that you want to stash, and q
to quit and leave the remaining hunks unstashed. a
will stash the shown hunk and the rest of the hunks in that file.
您可以使用git stash --patch
(或git stash -p
)来执行此操作——您将进入交互模式,在该模式下,您将看到每个更改的大块头。使用n
跳过文件,您不想藏起来,y
当你遇到你想藏匿的一个,并q
退出,留下剩余的帅哥unstashed。 a
将存储显示的大块头和该文件中的其余大块头。
Not the most user-friendly approach, but it gets the work done if you really need it.
不是最用户友好的方法,但如果您真的需要它,它可以完成工作。
回答by skalee
I usually add to index changes I don't want to stash and then stash with --keep-index option.
我通常会添加我不想存储的索引更改,然后使用 --keep-index 选项存储。
git add app/controllers/cart_controller.php
git stash --keep-index
git reset
Last step is optional, but usually you want it. It removes changes from index.
最后一步是可选的,但通常是您想要的。它从索引中删除更改。
WarningAs noted in the comments, this puts everything into 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.
警告正如评论中所指出的,这会将所有内容都放入存储中,包括暂存和未暂存。--keep-index 在存储完成后只留下索引。当您稍后弹出存储时,这可能会导致合并冲突。