提问人:morpheus 提问时间:3/7/2013 最后编辑:alexmorpheus 更新时间:9/28/2023 访问量:248345
如何仅解锁某些文件?
How to unstash only certain files?
答:
如下所述,并在“如何从 git 存储中提取单个文件(或对文件的更改)?”中详细说明,您可以应用 use git checkout
或 git show
来还原特定文件。
git checkout stash@{0} -- <filename>
在 Git 2.23+(2019 年 8 月)中,使用 git restore
,它取代了令人困惑的 git checkout
命令:
git restore --source=stash@{0} -- <filename>
这确实覆盖了:确保您没有进行本地修改,或者您可能希望合并隐藏的文件。filename
(正如 Jaime M. 所评论的那样,对于某些 shell,如 tcsh,您需要转义特殊字符,语法为:git checkout 'stash@{0}' -- <filename>
)
或将其保存在另一个文件名下:
git show stash@{0}:<full filename> > <newfile>
(请注意,这里是相对于项目顶级目录的文件的完整路径名(想想:相对于))。
<full filename>
stash@{0}
如果要手动选择要从该文件应用的更改:
git difftool stash@{0}..HEAD -- <filename>
# or
git checkout -p stash@{0} -- <filename>
看起来 “” 恢复了执行存储时的文件版本 -- 它不应用(仅)该文件的隐藏更改。
要执行后者,请执行以下操作:git checkout stash@{0} -- <filename>
git diff stash@{0}^1 stash@{0} -- <filename> | git apply
(正如 Peterflynn 所评论的那样,在某些情况下,您可能需要从传统的 diff 路径中删除一个 () 前导斜杠)| git apply -p1
p1
如评论:“unstash”(),则:git stash pop
- 将要保留的内容添加到索引 (
git add
) - 存放其余部分:
git stash --keep-index
最后一点是允许您在存储其他文件的同时保留一些文件。
“如何从已更改的多个文件中仅存储一个文件”中对此进行了说明。
评论
git stash pop
unstash
pop
如果您(没有冲突),它将在应用后删除存储。但是,如果您,它将应用补丁而不会将其从存储列表中删除。然后,您可以使用以下命令恢复不需要的更改git stash pop
git stash apply
git checkout -- files...
评论
git stash pop
我认为 VonC 的答案可能是你想要的,但这里有一种方法可以进行选择性的“git apply”:
git show stash@{0}:MyFile.txt > MyFile.txt
评论
checkout
>
运算符默认为 UTF-16(实际上是 UCS-2),这可能不是您想要的。@Balamurugan A 的答案不受这些问题的影响。git show stash@`{0`}:Path/To/MyFile.txt |sc Path/To/MyFile.txt
sc
git checkout stash@{N} <File(s)/Folder(s) path>
例如。 要仅恢复上次存储的 ./test.c 文件和 ./include 文件夹,
git checkout stash@{0} ./test.c ./include
评论
-a
还有一种方式:
git diff stash@{N}^! -- path/to/file1 path/to/file2 | git apply -R
评论
checkout
show
path/to/file2
-R
git diff stash@{N}^! -- path/to/file | git apply -
...| git apply -3 -
diff stash@{N}^!
已经生成了前向差异,所以省略-R
git diff stash@{N} -- path/to/file | git apply -R
首先列出所有藏匿处
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'
然后显示哪些文件在存储中(让我们选择存储 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
然后应用您喜欢的文件:
git checkout stash@{1} -- <filename>
或整个文件夹:
git checkout stash@{1} /errors
它也可以在没有的情况下工作,但建议使用它们。看这个帖子。--
通常将双连字符识别为信号 停止选项解释并处理以下所有参数 按照字面。
评论
git stash pop
对于 Windows 用户:大括号在 PowerShell 中具有特殊含义。您可以用单引号括起来,也可以用反引号转义。例如:
git checkout 'stash@{0}' YourFile
如果没有它,您可能会收到错误:
Unknown switch 'e'
对于考试
git stash show --name-only
结果
ofbiz_src/.project
ofbiz_src/applications/baseaccounting/entitydef/entitymodel_view.xml
ofbiz_src/applications/baselogistics/webapp/baselogistics/delivery/purchaseDeliveryDetail.ftl
ofbiz_src/applications/baselogistics/webapp/baselogistics/transfer/listTransfers.ftl
ofbiz_src/applications/component-load.xml
ofbiz_src/applications/search/config/elasticSearch.properties
ofbiz_src/framework/entity/lib/jdbc/mysql-connector-java-5.1.46.jar
ofbiz_src/framework/entity/lib/jdbc/postgresql-9.3-1101.jdbc4.jar
然后弹出存储在特定文件中
git checkout stash@{0} -- ofbiz_src/applications/baselogistics/webapp/baselogistics/delivery/purchaseDeliveryDetail.ftl
其他相关命令
git stash list --stat
get stash show
对于Windows用户来说,为了避免在内部引用的需要,如下图所示Unknown switch 'e'
stash@{0}
git restore --source='stash@{0}' -- <filename>
git stash apply // apply all files
git reset . // reset all
git add <wanted_file> // add only wanted file(s)
git checkout . // negate changes of the other files
评论