threads / discuss / 26530

How to git checkout a orgin(unstage) version file in another directoy?

Subject: How to git checkout a orgin(unstage) version file in another directoy?

## tl;dr

11 messages between Feb 17, 2011 and Feb 18, 2011.

replies: 10people: 5as markdown or json

Chunlin Zhang· Feb 17, 2011, 07:00 UTC · lore

I am writing a script for git-gui's "Tools" menu to diff a file. I want to checkout an origin file in a tmp directory and use the diff tool. Now it is ok to those unstage file,but to those staged file, I found that I do not know how to checkout the orgin(unstage) version.

In detail,I change current directory to a tmp directory,
and use for example 
'git --work-tree=. --git-dir="/media/linux/t/kernel/.git" checkout README'
 to checkout origin version.

Does anyone can help with this? I look the git help and can not find way till now.

Thanks!
Michael J Gruber· Feb 17, 2011, 08:26 UTC · re: Chunlin Zhang · lore

Re: How to git checkout a orgin(unstage) version file in another directoy?

Chunlin Zhang venit, vidit, dixit 17.02.2011 08:00:
Show 15 quoted lines
> I am writing a script for git-gui's "Tools" menu to diff a file.
> I want to checkout an origin file in a tmp directory and use the diff tool.
> Now it is ok to those unstage file,but to those staged file,
> I found that I do not know how to checkout the orgin(unstage) version.
> 
> In detail,I change current directory to a tmp directory,
> and use for example 
> 'git --work-tree=. --git-dir="/media/linux/t/kernel/.git" checkout README'
>  to checkout origin version.
> 
> Does anyone can help with this? 
> I look the git help and can not find way till now.
> 
> Thanks!
> 

I'm not sure what you mean by "origin" version, but if you mean the version from the current revision (before making and staging changes), you would check out from HEAD instead of from the index:

git --work-tree=. --git-dir="/media/linux/t/kernel/.git" checkout HEAD -- README

Michael
Chunlin Zhang· Feb 17, 2011, 10:03 UTC · re: Michael J Gruber · lore

Re: How to git checkout a orgin(unstage) version file in another directoy?

Michael J Gruber <git <at> drmicha.warpmail.net> writes:
> 
> I'm not sure what you mean by "origin" version, but if you mean the
> version from the current revision (before making and staging changes),
> you would check out from HEAD instead of from the index:
Yes,I mean HEAD version.
> 
> git --work-tree=. --git-dir="/media/linux/t/kernel/.git" checkout HEAD
It works! Thank you very much!
Chunlin Zhang· Feb 17, 2011, 10:17 UTC · re: Michael J Gruber · lore

Re: How to git checkout a orgin(unstage) version file in another directoy?

Michael J Gruber <git <at> drmicha.warpmail.net> writes:
> git --work-tree=. --git-dir="/media/linux/t/kernel/.git" checkout HEAD

But after I test,I found that after run this command,the file I do checkout action become unstaged. It is weard,because I have set the "--work-tree" to another directory. my command line for example: git --work-tree=. --git-dir="/media/linux/t/kernel/.git" checkout HEAD -- Android.mk

You can test with a git repository.I have no idea how to work around now.
Michael J Gruber· Feb 17, 2011, 12:35 UTC · re: Chunlin Zhang · lore

Re: How to git checkout a orgin(unstage) version file in another directoy?

Chunlin Zhang venit, vidit, dixit 17.02.2011 11:17:
Show 12 quoted lines
> Michael J Gruber <git <at> drmicha.warpmail.net> writes:
> 
>> git --work-tree=. --git-dir="/media/linux/t/kernel/.git" checkout HEAD
> But after I test,I found that after run this command,the file I do 
> checkout action become unstaged.
> It is weard,because I have set the "--work-tree" to another directory.
> my command line for example:
> git --work-tree=. --git-dir="/media/linux/t/kernel/.git" checkout 
> HEAD -- Android.mk
> 
> You can test with a git repository.I have no idea how to work around now.
> 

So, you don't really want to "checkout" the file. You only want its contents without updating the index. (My brain is apparently too git-rotted already to even think of the possibility that you didn't mean "our checkout" by "checkout").

Jonathan gave you the perfect solution for what you want.
Michael
Chunlin Zhang· Feb 17, 2011, 13:30 UTC · re: Michael J Gruber · lore

Re: How to git checkout a orgin(unstage) version file in another directoy?

Michael J Gruber <git <at> drmicha.warpmail.net> writes:
> So, you don't really want to "checkout" the file. You only want its
> contents without updating the index. (My brain is apparently too
> git-rotted already to even think of the possibility that you didn't mean
> "our checkout" by "checkout").
Yes,I am a git newbie,a little bit misunderstanding about the git-checkout.
> 
> Jonathan gave you the perfect solution for what you want.
Anyway,your answer make me more familiar about git cli usage.
> 
> Michael
> 
Junio C Hamano· Feb 17, 2011, 18:33 UTC · re: Michael J Gruber · lore

Re: How to git checkout a orgin(unstage) version file in another directoy?

Michael J Gruber <git@drmicha.warpmail.net> writes:
Show 6 quoted lines
> I'm not sure what you mean by "origin" version, but if you mean the
> version from the current revision (before making and staging changes),
> you would check out from HEAD instead of from the index:
>
> git --work-tree=. --git-dir="/media/linux/t/kernel/.git" checkout HEAD
> -- README

Wouldn't that still affect the index in /media/linux/t/kernel/.git/index, making /media/linux/t/kernel/README and the index entry out-of-sync?

Michael J Gruber· Feb 18, 2011, 07:41 UTC · re: Junio C Hamano · lore

Re: How to git checkout a orgin(unstage) version file in another directoy?

Junio C Hamano venit, vidit, dixit 17.02.2011 19:33:
Show 11 quoted lines
> Michael J Gruber <git@drmicha.warpmail.net> writes:
> 
>> I'm not sure what you mean by "origin" version, but if you mean the
>> version from the current revision (before making and staging changes),
>> you would check out from HEAD instead of from the index:
>>
>> git --work-tree=. --git-dir="/media/linux/t/kernel/.git" checkout HEAD
>> -- README
> 
> Wouldn't that still affect the index in /media/linux/t/kernel/.git/index,
> making /media/linux/t/kernel/README and the index entry out-of-sync?

Yes. I guess you overlooked the two posts where Chunlin and I realised that I took his request for "checkout" too giterally.

Michael
Jonathan Nieder· Feb 17, 2011, 10:34 UTC · re: Chunlin Zhang · lore

Re: How to git checkout a orgin(unstage) version file in another directoy?

Chunlin Zhang wrote:
> I am writing a script for git-gui's "Tools" menu to diff a file.
> I want to checkout an origin file in a tmp directory and use the diff tool.

Because you are writing a script, I'd suggest using one of the "low-level commands (plumbing)" listed in git(1). Because you are retrieving a file, it would be one of the "interrogation commands". And because it is a single file, I suppose this should be git cat-file, git unpack-file, or git archive.

For example,
	git cat-file blob HEAD:README
will print the contents of README in the HEAD commit.

Because I am curious: what exactly would your script be used to do? Can the git diff-tree or git diff-files command help?

Chunlin Zhang· Feb 17, 2011, 12:06 UTC · re: Jonathan Nieder · lore

Re: How to git checkout a orgin(unstage) version file in another directoy?

Jonathan Nieder <jrnieder <at> gmail.com> writes:
Show 11 quoted lines
> 
> Chunlin Zhang wrote:
> Because you are writing a script, I'd suggest using one of the
> "low-level commands (plumbing)" listed in git(1).  Because you are
> retrieving a file, it would be one of the "interrogation commands".
> And because it is a single file, I suppose this should be git
> cat-file, git unpack-file, or git archive.
> 
> For example,
> 
> 	git cat-file blob HEAD:README

Wow,this solved my problem perfectly. use this command for example: git --work-tree=. --git-dir="/media/linux/t/kernel/.git" cat-file blob HEAD:Android.mk>Android.mk

Thanks !
Show 6 quoted lines
> 
> will print the contents of README in the HEAD commit.
> 
> Because I am curious: what exactly would your script be used to do?
> Can the git diff-tree or git diff-files command help?
> 
Andreas Schwab· Feb 17, 2011, 17:17 UTC · re: Chunlin Zhang · lore

Re: How to git checkout a orgin(unstage) version file in another directoy?

Chunlin Zhang <zhangchunlin@gmail.com> writes:
> use this command for example:
> git --work-tree=. --git-dir="/media/linux/t/kernel/.git" cat-file blob 
> HEAD:Android.mk>Android.mk

You won't need --work-tree any more, since cat-file only looks at the object database.

Andreas.
-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

← back to recent threads