threads / discuss / 9033

Restoring files from old commits

Subject: Restoring files from old commits

## tl;dr

8 messages between Jul 14, 2007 and Aug 15, 2007.

replies: 7people: 6as markdown or json

Nikodemus Siivola· Jul 14, 2007, 12:09 UTC · lore

What is the recommended way to restore individual files (or directories) in the working tree to the content they had in a given commit?

That is to say, now that Cogito is deprecated, what should I use instead of cg-restore?

Cheers,
  -- Nikodemus
martin f krafft· Jul 14, 2007, 12:37 UTC · re: Nikodemus Siivola · lore

Re: Restoring files from old commits

also sprach Nikodemus Siivola <nikodemus@random-state.net> [2007.07.14.1409 +0200]:
> What is the recommended way to restore individual files
> (or directories) in the working tree to the content they
> had in a given commit?
As per my comment on IRC, I think
  git checkout <tree-ish> <path>
is the way.
-- 
martin;              (greetings from the heart of the sun.)
  \____ echo mailto: !#^."<*>"|tr "<*> mailto:" net@madduck
 
spamtraps: madduck.bogus@madduck.net
 
"the difference between genius and stupidity
 is that genius has it's limits."
                                                    -- albert einstein
Alex Riesen· Jul 14, 2007, 14:32 UTC · re: martin f krafft · lore

Re: Restoring files from old commits

martin f krafft, Sat, Jul 14, 2007 14:37:54 +0200:
Show 10 quoted lines
> also sprach Nikodemus Siivola <nikodemus@random-state.net> [2007.07.14.1409 +0200]:
> > What is the recommended way to restore individual files
> > (or directories) in the working tree to the content they
> > had in a given commit?
> 
> As per my comment on IRC, I think
> 
>   git checkout <tree-ish> <path>
> 
> is the way.
It does not work for directories, though. On can use
    $ git ls-tree -r -z <tree-ish> -- <path> | git update-index -z --index-info
    $ git ls-tree -r --name-only -z <tree-ish> -- <path> | git checkout-index -f -z --stdin
for a more generic way to do this.

P.S. Martin, would you please stop removing people from address lists and stop using that Mail-Followup-To? People miss their mails when they are not addressed to them anymore and you doing exactly that.

Junio C Hamano· Jul 14, 2007, 18:50 UTC · re: Alex Riesen · lore

Re: Restoring files from old commits

Alex Riesen <raa.lkml@gmail.com> writes:
Show 13 quoted lines
> martin f krafft, Sat, Jul 14, 2007 14:37:54 +0200:
>> also sprach Nikodemus Siivola <nikodemus@random-state.net> [2007.07.14.1409 +0200]:
>> > What is the recommended way to restore individual files
>> > (or directories) in the working tree to the content they
>> > had in a given commit?
>> 
>> As per my comment on IRC, I think
>> 
>>   git checkout <tree-ish> <path>
>> 
>> is the way.
>
> It does not work for directories, though.
Are you sure about that?  I just did this in my git.git workspace:
	$ mv Documentation Documentation-
        $ git checkout HEAD Documentation/
        $ diff -r Documentation- Documentation
Alex Riesen· Jul 15, 2007, 08:41 UTC · re: Junio C Hamano · lore

Re: Restoring files from old commits

Junio C Hamano, Sat, Jul 14, 2007 20:50:49 +0200:
Show 10 quoted lines
> Alex Riesen <raa.lkml@gmail.com> writes:
> >
> > It does not work for directories, though.
> 
> Are you sure about that?  I just did this in my git.git workspace:
> 
> 	$ mv Documentation Documentation-
>         $ git checkout HEAD Documentation/
>         $ diff -r Documentation- Documentation
> 
Missed it. Happy now :)
Nikodemus Siivola· Aug 15, 2007, 17:22 UTC · re: Alex Riesen · lore

Re: Restoring files from old commits

Tangentially related to my previous question:

Every once and a while I want to examine an older version of a file in an editor.

(The typical case is first looking for the source of a change with git-blame, and then wanting to look at the original code and the diff side by side.)

My current (suboptimal, I hope!) way of doing getting the file is git-cat-file: first look at the commit, then look at the tree, ... till I find the SHA1 for the file I want.

git-checkout <sha1-of-commit> <path>, but that leaves the old version in both the index and the tree, which feels icky especially if I'm giving this treatment to multiple files at the same time.

So what I'd like is something like
 git cat-path <commit> <path-in-the-tree-referenced-by-that-commit>
Does this exist, or is this scripting time?
Cheers,
 -- Nikodemus
Salikh Zakirov· Aug 15, 2007, 17:28 UTC · re: Nikodemus Siivola · lore

Re: Restoring files from old commits

Nikodemus Siivola пишет:
Show 5 quoted lines
> So what I'd like is something like
> 
>  git cat-path <commit> <path-in-the-tree-referenced-by-that-commit>
> 
> Does this exist, or is this scripting time?
Have you tried
   git show <commit>:<path>
?
Uwe Kleine-König· Aug 15, 2007, 18:27 UTC · re: Nikodemus Siivola · lore

Re: Restoring files from old commits

Hello Nikodemus,
Nikodemus Siivola wrote:
> So what I'd like is something like
> 
>  git cat-path <commit> <path-in-the-tree-referenced-by-that-commit>
Try:
	git show  <commit>:<path-in-the-tree-referenced-by-that-commit>

Best regards Uwe

-- 
Uwe Kleine-König

http://www.google.com/search?q=half+a+cup+in+teaspoons

← back to recent threads