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

11 messages from 2011-02-17 to 2011-02-18. Participants: Chunlin Zhang, Michael J Gruber, Jonathan Nieder, Andreas Schwab, Junio C Hamano.
Thread: https://gitlist.dev/t/26530

## Chunlin Zhang, 2011-02-17 07:00

Subject: How to git checkout a orgin(unstage) version file in another directoy?
Message-ID: <loom.20110217T074533-358@post.gmane.org>
URL: https://gitlist.dev/e/loom.20110217T074533-358%40post.gmane.org

```
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, 2011-02-17 08:26

Subject: Re: How to git checkout a orgin(unstage) version file in another directoy?
Message-ID: <4D5CDBAD.4050700@drmicha.warpmail.net>
URL: https://gitlist.dev/e/4D5CDBAD.4050700%40drmicha.warpmail.net
In-Reply-To: <loom.20110217T074533-358@post.gmane.org>

```
Chunlin Zhang venit, vidit, dixit 17.02.2011 08:00:
> 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, 2011-02-17 10:03

Subject: Re: How to git checkout a orgin(unstage) version file in another directoy?
Message-ID: <loom.20110217T110216-810@post.gmane.org>
URL: https://gitlist.dev/e/loom.20110217T110216-810%40post.gmane.org
In-Reply-To: <4D5CDBAD.4050700@drmicha.warpmail.net>

```
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, 2011-02-17 10:17

Subject: Re: How to git checkout a orgin(unstage) version file in another directoy?
Message-ID: <loom.20110217T110820-459@post.gmane.org>
URL: https://gitlist.dev/e/loom.20110217T110820-459%40post.gmane.org
In-Reply-To: <4D5CDBAD.4050700@drmicha.warpmail.net>

```
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.

```

## Jonathan Nieder, 2011-02-17 10:34

Subject: Re: How to git checkout a orgin(unstage) version file in another directoy?
Message-ID: <20110217103431.GA8308@elie>
URL: https://gitlist.dev/e/20110217103431.GA8308%40elie
In-Reply-To: <loom.20110217T074533-358@post.gmane.org>

```
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, 2011-02-17 12:06

Subject: Re: How to git checkout a orgin(unstage) version file in another directoy?
Message-ID: <loom.20110217T130405-970@post.gmane.org>
URL: https://gitlist.dev/e/loom.20110217T130405-970%40post.gmane.org
In-Reply-To: <20110217103431.GA8308@elie>

```
Jonathan Nieder <jrnieder <at> gmail.com> writes:

> 
> 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 !

> 
> 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?
> 

```

## Michael J Gruber, 2011-02-17 12:35

Subject: Re: How to git checkout a orgin(unstage) version file in another directoy?
Message-ID: <4D5D1603.6040208@drmicha.warpmail.net>
URL: https://gitlist.dev/e/4D5D1603.6040208%40drmicha.warpmail.net
In-Reply-To: <loom.20110217T110820-459@post.gmane.org>

```
Chunlin Zhang venit, vidit, dixit 17.02.2011 11:17:
> 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, 2011-02-17 13:30

Subject: Re: How to git checkout a orgin(unstage) version file in another directoy?
Message-ID: <loom.20110217T142701-926@post.gmane.org>
URL: https://gitlist.dev/e/loom.20110217T142701-926%40post.gmane.org
In-Reply-To: <4D5D1603.6040208@drmicha.warpmail.net>

```
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
> 

```

## Andreas Schwab, 2011-02-17 17:17

Subject: Re: How to git checkout a orgin(unstage) version file in another directoy?
Message-ID: <m2r5b6y40v.fsf@igel.home>
URL: https://gitlist.dev/e/m2r5b6y40v.fsf%40igel.home
In-Reply-To: <loom.20110217T130405-970@post.gmane.org>

```
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."

```

## Junio C Hamano, 2011-02-17 18:33

Subject: Re: How to git checkout a orgin(unstage) version file in another directoy?
Message-ID: <7vpqqqcxzf.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vpqqqcxzf.fsf%40alter.siamese.dyndns.org
In-Reply-To: <4D5CDBAD.4050700@drmicha.warpmail.net>

```
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?

```

## Michael J Gruber, 2011-02-18 07:41

Subject: Re: How to git checkout a orgin(unstage) version file in another directoy?
Message-ID: <4D5E22BE.301@drmicha.warpmail.net>
URL: https://gitlist.dev/e/4D5E22BE.301%40drmicha.warpmail.net
In-Reply-To: <7vpqqqcxzf.fsf@alter.siamese.dyndns.org>

```
Junio C Hamano venit, vidit, dixit 17.02.2011 19:33:
> 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

```
