如何仅隐藏某些文件?

本文翻译自:How to unstash only certain files?

I stashed my changes. 我藏了零钱。 Now I want to unstash only some files from the stash. 现在,我只想取消隐藏部分文件。 How can I do this? 我怎样才能做到这一点?


#1楼

参考:https://stackoom.com/question/1230T/如何仅隐藏某些文件


#2楼

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 checkout or git show to restore a specific file. 如下所述 ,并在“ 如何从git存储中提取单个文件(或更改为文件)? ”中进行了详细介绍,您可以应用git checkoutgit show来还原特定文件。

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

That does overwrite filename : make sure you didn't have local modifications, or you might want to merge the stashed file instead . 这确实会覆盖 filename :确保您没有本地修改,或者您可能想合并隐藏的文件

(As commented by Jaime M. , for certain shell like tcsh where you need to escape the special characters, the syntax would be: git checkout 'stash@{0}' -- <filename> ) (如Jaime 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} ))。

yucer suggests 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>

Vivek adds 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 NOT apply (just) the stashed changes for that file. 貌似“ git checkout stash@{0} -- <filename> ”还原时进行藏匿在该文件的时间版本-它适用(只),该文件的藏匿变化。
To do the latter: 要执行后者:

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

(as commented by peterflynn , you might need | git apply -p1 in some cases, removing one ( p1 ) leading slash from traditional diff paths) (如peterflynn评论 ,在某些情况下,您可能需要| git apply -p1 ,从传统的diff路径中删除一个( p1 )前导斜线)


As commented: "unstash" ( git stash pop ), then: 如注释:“ unstash”( git stash pop ),然后:

  • add what you want to keep to the index ( git add ) 将要保留的内容git add到索引( git add
  • stash the rest: git stash --keep-index git stash --keep-index其余部分: 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 ". 如何隐藏已更改的多个文件中的仅一个文件 ”中对此进行了说明。


#3楼

If you git stash pop (with no conflicts) it will remove the stash after it is applied. 如果您使用git stash pop (没有冲突),它将在应用后删除该stash。 But if you git stash apply it will apply the patch without removing it from the stash list. 但是,如果您使用git stash apply ,它将应用补丁而不将其从存储列表中删除。 Then you can revert the unwanted changes with git checkout -- files... 然后您可以使用git checkout -- files...还原不需要的更改git checkout -- files...


#4楼

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

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

#5楼

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

#6楼

另一种方式:

git diff stash@{N}^! -- path/to/file1 path/to/file2  | git apply -R
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值