Skip to contentSkip to conversation

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

DISCUSSION Chunlin Zhang 5 participants 11 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

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!

#1 lore raw
Quoted 2 lines · Chunlin Zhang

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?

#2 lore raw
Quoted 11 lines · 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 !

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

#3 lore raw
Quoted 3 lines · Chunlin Zhang

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.

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#4 lore raw
In reply to Chunlin Zhang

Chunlin Zhang venit, vidit, dixit 17.02.2011 08:00:

Quoted 10 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.

Quoted 5 lines

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

#5 lore raw
Quoted 6 lines · Michael J Gruber

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?

#6 lore raw

Junio C Hamano venit, vidit, dixit 17.02.2011 19:33:

Quoted 11 lines
Quoted 6 lines · Michael J Gruber

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

#7 lore raw
In reply to Michael J Gruber
Quoted 4 lines · 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.

Quoted 2 lines

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

It works! Thank you very much!

#8 lore raw
In reply to Michael J Gruber
Quoted 1 line · 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.

#9 lore raw

Chunlin Zhang venit, vidit, dixit 17.02.2011 11:17:

Quoted 12 lines
Quoted 1 line · 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

#10 lore raw
Quoted 4 lines · 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.

Quoted 2 lines

Jonathan gave you the perfect solution for what you want.

Anyway,your answer make me more familiar about git cli usage.

Quoted 3 lines

Michael