1

Suppose we have a git repository that contains two images: alice.png and bob.png. We commited this in Initial commit.

After that, we do cp bob.png alice.png, so alice.png will have the same content with bob.png.

Doing git diff shows that.

Now if I open alice.png I see Bob. How can I open/read/access programatically the old alice.png (which appears if I do git checkout alice.png)?


The best (which is not a good one) solution I found is to:

  • copy the repository into a temp directory
  • do git checkout .
  • get modified files from original directory
  • read the non-modified files from temp directory

But I suppose there should be a better solution.


For non-binary files git show HEAD:path/to/file does this, but for me, when doing git show HEAD:alice.png nothing is output.

Community
  • 1
  • 1
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • possible duplicate of [git-checkout older revision of a file under a new name](http://stackoverflow.com/questions/888414/git-checkout-older-revision-of-a-file-under-a-new-name) – Carl Norum Dec 10 '14 at 18:29
  • @CarlNorum Seems the correct solution, but I get this error: `fatal: Path '1.png' exists on disk, but not in 'HEAD^'.` – Ionică Bizău Dec 10 '14 at 18:34
  • You don't want `HEAD^` in your case, just `HEAD`. You could also use the hash of your `Initial commit`. The point is to specify a revision that includes the file you want to check out. If you have only one commit, you don't even *have* a `HEAD^`. – Carl Norum Dec 10 '14 at 18:35
  • @CarlNorum `git show HEAD:alice.png > old_alice.png` -- `old_alice.png` and `git show HEAD:alice.png` doesn't output anything (probably because it's a binary file?). – Ionică Bizău Dec 10 '14 at 18:36
  • @CarlNorum I updated my question reflecting this thing. – Ionică Bizău Dec 10 '14 at 18:44
  • I can't replicate this. `git show HEAD:alice.png > foo.png` works for me... – twalberg Dec 10 '14 at 19:51

1 Answers1

1

You can access a file at a given commit with git show. Since your changes are still uncommitted, the old file is still at HEAD.

# This opens a file in the pager
git show HEAD:path/to/file

# Save the file to disk
git show HEAD:alice.png > temp.png

If you have an image viewer that supports piping on stdin such as ImageMagick's display, you could view the image directly.

git show HEAD:alice.png | display
Justin Howard
  • 5,504
  • 1
  • 21
  • 48
  • When I tried it, my pager showed a bunch of binary garbage (the encoded image). Your default pager for git is probably ignoring the binary file content. Did you try saving the file and opening that in your image viewer? – Justin Howard Dec 10 '14 at 18:43
  • Yes, I tried. But since `>` just redirects the output, it created an empty file. – Ionică Bizău Dec 10 '14 at 18:45
  • That's strange. Do you get the same results with `bob.png`? – Justin Howard Dec 10 '14 at 18:50
  • According to the [git-show manual](https://www.kernel.org/pub/software/scm/git/docs/git-show.html), the contents of a blob shouldn't matter, so the image format difference is confusing. Just to eliminate any extra factors, try `git --no-pager show:alice.png`. This should output the binary contents to stdout. If that doesn't work, I would double check that the committed file is correct. – Justin Howard Dec 10 '14 at 19:04