如何让我隐藏一个特定的文件?(复制)

时间:2022-09-06 07:29:09

Possible Duplicate:
How to stash only one file out of multiple files that have changed

可能的副本:如何将一个文件从多个已更改的文件中保存。

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状态给我以下信息:

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/view /cart/welcome。ttml,我该怎么做呢?比如(但这当然不起作用):

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

2 个解决方案

#1


1521  

EDIT: Since git 2.13, there is a command to save a specific path to the stash: git stash push <path>. For example: git stash push -m welcome_cart app/views/cart/welcome.thtml.

编辑:因为git 2.13,所以有一个命令可以保存到隐藏点的特定路径:git隐藏推 。例如:git cache push -m welcom_cart应用/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隐藏——patch(或git隐藏——p)——来实现这一点,您将进入交互模式,在这个模式中,您将看到每个修改过的代码块。使用n来跳过你不想隐藏的文件,当你遇到你想要隐藏的文件时使用y,使用q来退出,并保留剩下的大块文件。a将展示的大块头和其他大块头藏在那个文件里。

Not the most user-friendly approach, but it gets the work done if you really need it.

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

#2


242  

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.

最后一步是可选的,但通常您需要它。它从索引中删除更改。


Warning As 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.

正如评论中所提到的,这将把所有的东西都放入隐藏库中,分阶段和非分阶段。保持指数只是在储藏完成后单独留下指数。这可能导致合并冲突,当您稍后弹出隐藏。

#1


1521  

EDIT: Since git 2.13, there is a command to save a specific path to the stash: git stash push <path>. For example: git stash push -m welcome_cart app/views/cart/welcome.thtml.

编辑:因为git 2.13,所以有一个命令可以保存到隐藏点的特定路径:git隐藏推 。例如:git cache push -m welcom_cart应用/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隐藏——patch(或git隐藏——p)——来实现这一点,您将进入交互模式,在这个模式中,您将看到每个修改过的代码块。使用n来跳过你不想隐藏的文件,当你遇到你想要隐藏的文件时使用y,使用q来退出,并保留剩下的大块文件。a将展示的大块头和其他大块头藏在那个文件里。

Not the most user-friendly approach, but it gets the work done if you really need it.

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

#2


242  

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.

最后一步是可选的,但通常您需要它。它从索引中删除更改。


Warning As 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.

正如评论中所提到的,这将把所有的东西都放入隐藏库中,分阶段和非分阶段。保持指数只是在储藏完成后单独留下指数。这可能导致合并冲突,当您稍后弹出隐藏。