我将如何从git存储中提取单个文件(或更改为文件)? [英] How would I extract a single file (or changes to a file) from a git stash?

查看:678
本文介绍了我将如何从git存储中提取单个文件(或更改为文件)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以从git存储中提取单个文件或diff文件,而无需弹出存储变更集。



Might任何人都可以提供一些关于这方面的建议/想法?

git stash manpage中可以阅读(在讨论部分,在Options描述之后):

lockquote
一个存储被表示为一个提交,它的树记录了
工作目录的状态,它的第一个父母是在创建
存储时在HEAD处的提交。


因此,您可以将存储空间 stash @ {0} 是第一个/最顶端的存储)作为合并提交,并使用: $ git diff stash @ {0} ^ 1 stash @ {0} - < filename>

解释: stash @ {0} ^ 1 捷径意味着给定藏匿的第一个父母,正如上面解释的那样,这是承认变化被隐藏起来的承诺。我们使用这种形式的git diff(有两个提交),因为 stash @ {0} / refs / stash 是一个合并提交,我们必须告诉git我们想要区分哪一个父类。更加神秘:

  $ git diff stash @ {0} ^! - <文件名> 

也应该有效(请参阅 git rev-parse 手册页来解释 rev ^!语法,在指定范围部分) 。

同样,您可以使用 git checkout 检查单个文件的存储:

  $ git checkout stash @ {0}  - <文件名> 

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

  $ git show stash @ {0}:<完整文件名> > < newfile中> 

  $ git show stash @ {0}:./& lt; relative filename> > < newfile中> 

note here< full filename>是完整的路径名称(相对于存储@ {0} )))。



b
$ b

您可能需要从shell扩展中保护隐藏@ {0} ,即使用stash @ {0 }'stash @ {0}'


I'd like to know if it is possible to extract a single file or diff of a file from a git stash without popping the stash changeset off.

Might anyone be able to provide some suggestions/ideas about this?

解决方案

In git stash manpage you can read that (in "Discussion" section, just after "Options" description):

A stash is represented as a commit whose tree records the state of the working directory, and its first parent is the commit at HEAD when the stash was created.

So you can treat stash (e.g. stash@{0} is first / topmost stash) as a merge commit, and use:

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

Explanation: stash@{0}^1 shortcut means first parent of given stash, which as stated in explanation above is commit at which changes were stashed away. We use this form of "git diff" (with two commits) because stash@{0} / refs/stash is a merge commit, and we have to tell git which parent we want to diff against. More cryptic:

$ git diff stash@{0}^! -- <filename>

should also work (see git rev-parse manpage for explanation of rev^! syntax, in "Specifying ranges" section).

Likewise, you can use git checkout to check a single file out of the stash:

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

or to save it under another filename:

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

or

$ git show stash@{0}:./<relative 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})).


You might need to protect stash@{0} from shell expansion, i.e. use "stash@{0}" or 'stash@{0}'.

这篇关于我将如何从git存储中提取单个文件(或更改为文件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆