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
Bryan Turner <bturner@atlassian.com>
Date
Dec 7, 2018, 06:55 UTC
Message-ID
<CAGyf7-FUHMEq_FfPNrH6uT2b-nCd_wi=Aww+OUuoDem11DROGA@mail.gmail.com>
In-Reply-To
<CADHAf1Y_d=-9By4jC2xd+BmWJgfGmBNUr=uSuQtfuHDrarN4kw@mail.gmail.com>

On Thu, Dec 6, 2018 at 10:49 PM biswaranjan panda <biswaranjan.nitrkl@gmail.com> wrote:

Show 10 quoted lines
>
> I have the following scenario:
>
> On a branch A, I deleted a file foo.txt and committed the change. Then
> I did a bunch of other changes.
> Now I want to undelete foo.txt.
>
> One way is to checkout a separate branch B where the file is present.
> Then checkout A. Then do
> git checkout B -- path_to_file

It doesn't change anything, but note that you don't need to checkout B first, to restore the file. If you know a commit SHA where the file is present, "git checkout SHA -- path_to_file" will pull back the file as it existed at that commit.

Show 6 quoted lines
>
> While this does gets the file back, the file shows up as a new file to
> be committed. Once I commit it, git blame doesn't show the old history
> for the file.
>
> I would appreciate if anyone knows how to preserve git blame history

It's not possible, as far as I'm aware. While the new file has the same name as the old file, to Git they are two unrelated entries that happen to reside at the same path. Even things like "git log --follow" won't consider the file to be related to its previous history.

Bryan
Previous: biswaranjan pandaNext: biswaranjan panda
Message 2 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.