git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Retrieving a file in git that was deleted and committed

From
Elijah Newren <newren@gmail.com>
Date
Dec 10, 2018, 21:33 UTC
Message-ID
<CABPp-BHjR7Wq-D9tFMyPHZE1ogL5udOt8ri1rN3E1CasfD-2PQ@mail.gmail.com>
In-Reply-To
<20181207072004.GA32603@sigill.intra.peff.net>
On Thu, Dec 6, 2018 at 11:48 PM Jeff King <peff@peff.net> wrote:
Show 27 quoted lines
>
> On Thu, Dec 06, 2018 at 11:07:00PM -0800, biswaranjan panda wrote:
>
> > Thanks! Strangely git log --follow does work.
>
> I suspect it would work even without --follow. When you limit a log
> traversal with a pathspec, like:
>
>   git log foo
>
> that is not about following some continuous stream of content, but
> rather just applying that pathspec to the diff of each commit, and
> pruning ones where it did not change. So even if there are gaps where
> the file did not exist, we continue to apply the pathspec to the older
> commits.
>
> Tools like git-blame will _not_ work, though, as they really are trying
> to track the content as they walk back through history. And Once all of
> the content seems to appear from nowhere in your new commit, that seems
> like a dead end.
>
> In theory there could be some machine-readable annotation in the commit
> object (or in a note created after the fact) to say "even though 'foo'
> is a new file here, it came from $commit:foo".  And then git-blame could
> keep following the content there. But such a feature does not yet exist.
>
> -Peff

Hmm...sure, if the file is deleted on the only relevant branch through history...but what if there were another branch where it weren't deleted? What does git blame do then?

In other words, do NOT restore the file as biswaranjan suggested, but instead restore it this way[1]:

git checkout -b keep-foo $REVISION_BEFORE_FOO_DELETED git commit --allow-empty -m "We want to keep foo" git checkout A git merge --no-commit keep-foo git checkout keep-foo -- foo.txt git commit

Now, when you run
  git blame foo.txt

blame should notice that foo.txt didn't exist in the first parent history on A, so it won't bother walking it to find that at some point foo.txt did exist there. Instead, it'll walk down the second parent and follow its history, where it should keep walking back and show all the old changes...right? Or did I mess up my testcase and misunderstand something somehow?

Elijah
[1] Sidenote: it seems like those commands I gave should have simplified down to

git merge --no-commit --no-ff $REVISION_BEFORE_FOO_DELETED git checkout $REVISION_BEFORE_FOO_DELETED -- foo.txt git commit

But that seems to error out with "Already up-to-date"...even when testing with older versions of git, so it wasn't any of my changes. Not sure why the --no-ff flag doesn't work.

Previous: Junio C HamanoNext: Jeff King
Message 10 of 12 in “Retrieving a file in git that was deleted and committed”
  1. biswaranjan pandaDec 7, 2018
  2. Bryan TurnerDec 7, 2018
  3. biswaranjan pandaDec 7, 2018
  4. Jeff KingDec 7, 2018
  5. Bryan TurnerDec 7, 2018
  6. biswaranjan pandaDec 7, 2018
  7. Jeff KingDec 8, 2018
  8. biswaranjan pandaDec 9, 2018
  9. Junio C HamanoDec 11, 2018
  10. Elijah NewrenDec 10, 2018
  11. Jeff KingDec 11, 2018
  12. Sergey OrganovDec 10, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.