git 如何仅取消隐藏某些文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15264553/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:40:50  来源:igfitidea点击:

How to unstash only certain files?

gitgit-stash

提问by morpheus

I stashed my changes. Now I want to unstash only some files from the stash. How can I do this?

我把我的零钱藏起来了。现在我只想从存储中取出一些文件。我怎样才能做到这一点?

回答by VonC

As mentioned below, and detailed in "How would I extract a single file (or changes to a file) from a git stash?", you can apply use git checkoutor git showto restore a specific file.

正如下面提到的,并在详细的“我怎么会提取一个混帐藏匿单个文件(或对文件的更改)?”,你可以申请使用git checkoutgit show恢复特定文件。

git checkout stash@{0} -- <filename>

That does overwritefilename: make sure you didn't have local modifications, or you might want to merge the stashed file instead.

那确实会覆盖filename:确保您没有进行本地修改,或者您可能想要合并隐藏的文件

(As commentedby Jaime M., for certain shell like tcsh where you need to escape the special characters, the syntax would be: git checkout 'stash@{0}' -- <filename>)

(正如评论海梅M.,对于某些壳例如tcsh,你需要逃跑的特殊字符,语法为: git checkout 'stash@{0}' -- <filename>

or to save it under another filename:

或将其保存在另一个文件名下:

git show stash@{0}:<full filename>  >  <newfile>

(note that here <full filename>is full pathname of a file relative to top directory of a project (think: relative to stash@{0})).

(请注意,这里<full filename>是相对于项目顶级目录的文件的完整路径名(想想:相对于stash@{0}))。

yucersuggests in the comments:

yucer在评论中建议:

If you want to select manually which changes you want to apply from that file:

如果要手动选择要从该文件应用哪些更改:

git difftool stash@{0}..HEAD -- <filename>

Vivekadds in the comments:

Vivek在评论中补充道:

Looks like "git checkout stash@{0} -- <filename>" restores the version of the file as of the time when the stash was performed -- it does NOTapply (just) the stashed changes for that file.
To do the latter:

像看起来“ git checkout stash@{0} -- <filename>”恢复文件的版本的时候,进行了藏匿-它确实适用(只),该文件的藏匿变化。
做后者:

git diff stash@{0}^1 stash@{0} -- <filename> | git apply

(as commentedby peterflynn, you might need | git apply -p1in some cases, removing one (p1) leading slash from traditional diff paths)

(正如peterflynn评论的,在某些情况下,您可能需要从传统的差异路径中删除一个 ( ) 前导斜杠)| git apply -p1p1



As commented: "unstash" (git stash pop), then:

正如评论:“unstash”(git stash pop),然后:

  • add what you want to keep to the index (git add)
  • stash the rest: git stash --keep-index
  • 将要保留的内容添加到索引 ( git add)
  • 把剩下的藏起来: git stash --keep-index

The last point is what allows you to keep some file while stashing others.
It is illustrated in "How to stash only one file out of multiple files that have changed".

最后一点是允许您保留一些文件同时隐藏其他文件。
它在“如何从已更改的多个文件中仅存储一个文件”中进行了说明。

回答by Balamurugan A

git checkout stash@{N} <File(s)/Folder(s) path> 

Eg. To restore only ./test.c file and ./include folder from last stashed,

例如。要仅从上次隐藏的 ./test.c 文件和 ./include 文件夹中恢复,

git checkout stash@{0} ./test.c ./include

回答by Mike Monkiewicz

I think VonC's answer is probably what you want, but here's a way to do a selective "git apply":

我认为 VonC 的答案可能是您想要的,但这里有一种方法可以进行选择性的“git apply”:

git show stash@{0}:MyFile.txt > MyFile.txt

回答by Black

First list all the stashes

首先列出所有藏匿处

git stash list

stash@{0}: WIP on Produktkonfigurator: 132c06a5 Cursor bei glyphicon plus und close zu zeigende Hand ?ndern
stash@{1}: WIP on Produktkonfigurator: 132c06a5 Cursor bei glyphicon plus und close zu zeigende Hand ?ndern
stash@{2}: WIP on master: 7e450c81 Merge branch 'Offlineseite'

Then show which files are in the stash (lets pick stash 1):

然后显示哪些文件在 stash 中(让我们选择 stash 1):

git stash show 1 --name-only

//Hint: you can also write
//git stash show stash@{1} --name-only

 ajax/product.php
 ajax/productPrice.php
 errors/Company/js/offlineMain.phtml
 errors/Company/mage.php
 errors/Company/page.phtml
 js/konfigurator/konfigurator.js

Then apply the file you like to:

然后应用你喜欢的文件:

git checkout stash@{1} -- <filename>

or whole folder:

或整个文件夹:

git checkout stash@{1} /errors


It also works without --but it is recommended to use them. See thispost.

它也可以不使用,--但建议使用它们。看到这个帖子。

It is also conventional to recognize a double hyphen as a signal to stop option interpretation and treat all following arguments literally.

将双连字符识别为停止选项解释并按字面处理所有以下参数的信号也是惯例。

回答by Ben Hymanson

If you git stash pop(with no conflicts) it will remove the stash after it is applied. But if you git stash applyit will apply the patch without removing it from the stash list. Then you can revert the unwanted changes with git checkout -- files...

如果您git stash pop(没有冲突),它将在应用后删除存储。但是,如果您git stash apply将应用补丁而不将其从存储列表中删除。然后您可以使用以下命令还原不需要的更改git checkout -- files...

回答by Janac Meena

For Windows users: curly braces have special meaning in PowerShell. You can either surround with single quotes or escape with backtick. For example:

对于 Windows 用户:大括号在 PowerShell 中具有特殊含义。您可以用单引号括起来或用反引号转义。例如:

git checkout 'stash@{0}' YourFile

git checkout 'stash@{0}' YourFile

Without it, you may receive an error:

没有它,您可能会收到错误消息:

Unknown switch 'e'

Unknown switch 'e'

回答by Lacho Tomov

One more way:

还有一种方法:

git diff stash@{N}^! -- path/to/file1 path/to/file2  | git apply -R