Marcus Leon
Marcus Leon

Reputation: 56699

git show to view prior version of a renamed file?

We renamed a file where git log --follow my/new-file correctly shows changes before and after the rename.

But this command does not work to show a prior version: git show <rev>:my/new-file.

We have to run git show <rev>:my/old-file.

Is there a way you can run this command with my/new-file instead of my/old-file?

Upvotes: 6

Views: 185

Answers (1)

torek
torek

Reputation: 489748

The short answer is easy, if displeasing: no.

The problem here is that git show first resolves what to show (via git rev-parse, more or less), then shows it, and git rev-parse does not walk through history trying to follow a file and do a diff from each commit to its parent(s) to see if it can find what looks like a rename.

Using git log --follow, however, does precisely that: look at each commit compared to parent commit(s), generate a diff, and see if the resulting diff suggests that perhaps my/new-file was renamed from my/old-file at that time. If so, it adjusts its next-older check to work with my/old-file.

Git could perhaps use a separate program (or mode in git log) that looks to see if some file(s) were renamed across some commit-pair(s), and if so, provide the previous path to other git utilities like git show. Then you could say: git show $(git whatwas HEAD <rev> my/new-file), or something like that (depending on the details of this hypothetical git whatwas command did). You could write a script that essentially performs this whatwas using the technique I outlined in this answer. I'm sure various annoying details would crop up in the process, but the theory seems reasonable.

Upvotes: 5

Related Questions