Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

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

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.

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

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

(任何人都可以提供一些关于此的建议/想法吗?)

  ask by Danny translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

(在git stash手册页上,您可以阅读(在“讨论”部分,在“选项”描述之后):)

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.

(存储表示为提交,其树记录工作目录的状态,其第一个父节点是创建存储时HEAD的提交。)

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

(因此,您可以将stash(例如stash@{0}是第一个/最顶层的存储)视为合并提交,并使用:)

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

Explanation: stash@{0}^1 means the first parent of the given stash, which as stated in the explanation above is the commit at which changes were stashed away.

(说明: stash@{0}^1表示给定存储的第一个父节点,如上面的解释中所述,是存储更改的提交。)

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.

(我们使用这种形式的“git diff”(有两次提交),因为stash@{0} / refs/stash是一个合并提交,我们必须告诉git我们要对哪个父进行区分。)

More cryptic:

(更神秘:)

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

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

(也应该工作(有关rev^!语法的解释,请参阅git rev-parse手册页,在“指定范围”部分)。)

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

(同样,您可以使用git checkout从存储中检查单个文件:)

$ 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} )).

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


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

(您可能需要保护stash@{0}免受shell扩展,即使用"stash@{0}"'stash@{0}' 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...