Removing missing LFS objects from git repository

2020-06-17 06:19发布

问题:

I am missing a bunch of LFS objects in my git repository*, both on the clients and the server. I am aware that those objects are lost and that's okay. Unfortunately this means that git lfs fetch --all or even git lfs push --all origin will fail.

I would like to purge the "broken pointers" from the repository, either by replacing the binary with a dummy text file or by removing the pointer altogether. I am also aware that this involves rewriting history and that's also fine.

What would be the best way to proceed?

*To clarify, I am missing some LFS files on the server but not all of them and not for all revisions of those files.

For example, I have foo.png that has been modified in 3 commits:

  • foo.png (Version 1, LFS Sha: 03cfd743)
  • foo.png (Version 2, LFS Sha: 661f0797)
  • foo.png (Version 3, LFS Sha: 5fa2f122)

The LFS server no longer has foo.png version 2, so I would like to remove that commit from history. Unfortunately git lfs does not tell me which commit is broken, it simply tells me that 661f0797 is missing.

(For the record, I have found the missing files so I no longer have this issue but the solution should still be interesting!)

回答1:

Have you tried a simple (A backup is recommended before using the following command) :

rm -f .git/index
git reset


回答2:

You can untrack files, the manual is here.

Examples:

Just a single file:

git lfs untrack "/path/to/my-file.gif"

With wildcard you can catch several files by one command:

git lfs untrack "*.gif"


回答3:

Make git reindex and creat new entries to the files, please read the following

git reset [-q] [<tree-ish>] [--] <paths>...
       This form resets the index entries for all <paths> to their state at <tree-ish>. (It does not
       affect the working tree or the current branch.)

       This means that git reset <paths> is the opposite of git add <paths>.

       After running git reset <paths> to update the index entry, you can use git-checkout(1) to check
       the contents out of the index to the working tree. Alternatively, using git-checkout(1) and
       specifying a commit, you can copy the contents of a path out of a commit to the index and to the
       working tree in one go.

source : man git-reset