Skip to content

Add command to undo fetch and checkout #1189

Open

Description

@shoelzer

Git LFS provides control over which files to fetch/cache (lfs.fetchinclude, lfs.fetchexclude) and which files to put in the working dir (checkout <filespec>). But once files are in the cache or working dir there is no (easy) way to undo those operations. I would like a command to change files into placeholders in the working dir and delete cached files.

The goal of this command is to free disk space used by LFS files. It goes much further than prune. I should be able to remove all LFS files from the working dir and cache. By default, the command should protect against data loss by verifying that files exist on the LFS server before deleting, but there should also be an option to skip that check.

I don't know if this should be a new command, multiple new commands, or options added to existing commands. Some ideas...

  • Single new command: scrub, clear, free
  • Multiple new commands: uncheckout and unfetch
  • New options for commands: checkout --placeholder and fetch --clear-cache

Activity

  1. technoweenie commented on Apr 28, 2016

    @technoweenie
    Contributor

    I think this sounds like a great idea. I think git lfs prune could get some kind of --all option that just basically does rm -rf .git/lfs/objects. "Uncheckout" (the act of replacing LFS tracked files in the working directory with the LFS pointer) should probably be a new command though. I don't like the idea of a command like git lfs checkout doing the opposite given a new flag like git lfs checkout --clear.

  2. sinbad commented on Apr 28, 2016

    @sinbad
    Contributor

    Thanks for raising this, it's a solid point.

    In terms of deleting fetched content in the .git/lfs/objects store, I think that's a valid extension to git lfs prune of anything that would have been omitted at fetch time because of lfs.fetchinclude and lfs.fetchexclude. That could be default behaviour, the rest of prune is just an inverse of fetch with a little date padding to avoid thrashing.

    As for resetting what's currently in the working copy back to pointers, you can actually achieve this now using git reset --hard if the object isn't already fetched into .git/lfs/objects. The smudge filter will get invoked and if the object data isn't local already and the fetch is suppressed due to include/exclude settings then it will write the pointer to the working copy instead, over the top of any current content. The only reason this doesn't work as a solution right now is that you have no real way to easily remove all the objects you've already fetched that now match those settings, which the changes above for prune would do.

    So I think this doesn't need any extra commands, just an enhancement to the default behaviour of git lfs prune.
    [edit]Ha @technoweenie beat me while I was typing 😆

  3. technoweenie commented on Apr 28, 2016

    @technoweenie
    Contributor

    FWIW, an "uncheckout" command was requested in #944 (comment) too. I think I'd prefer having our own documented/tested command, instead of encouraging git reset --hard with caveats that the user has to worry about.

  4. shoelzer commented on Apr 28, 2016

    @shoelzer
    Author

    Thanks for the tips on git reset --hard. I can use that right now.

    I like the idea of extending git lfs prune and adding "uncheckout". Perhaps git lfs checkin?

  5. sinbad commented on Apr 29, 2016

    @sinbad
    Contributor

    I'd favour something like git lfs checkout --clean since it's not really doing the opposite of what checkout normally does, it's just doing it again from scratch with the latest settings.

  6. swordfly commented on Aug 11, 2017

    @swordfly

    Hi All, recently I'm evaluating LFS support big binary test data. I'm really like the idea to revert local checkout LFS file to pointer file. Is it available now in latest LFS version? Because we have many large test data, each one is around 2GB, so we want to revert all LFS files to pointer files even if those are latest version. Thanks!

  7. farleylai commented on Nov 28, 2017

    @farleylai

    I think people start to ask for more from Git LFS to serve large files on demand such as Google Drive File Stream or Dropbox smart sync despite limited local storage.
    Before that, a clumsy way to manually convert the large file back is to use git lfs clean as follows:

    $> mv largefile.bin largefile.bin.bak
    $> cat largefile.bin.bak | git lfs clean > largefile.bin
    $> rm largefile.bin.bak
    

    Those lines can be wrapped as a script command.
    Unfortunately, the clean filter needs to compute based on the entire file and is running slow.
    Any suggestion to improve the performance would be highly anticipated.

  8. ttaylorr commented on Nov 29, 2017

    @ttaylorr
    Contributor

    @farleylai Without more serious workarounds, I think that this is the "best" solution for now. That said, I really like the idea of an git-lfs-stash command. "stash" is perhaps confusing as it has other connotations, but something that would remove un-checkout the object and potentially prune it from your local cache.

    This is a reasonably sized project, but I would be more than happy to guide you or anyone else through it as an OSS contribution. That said, if nobody takes this on, I'd be happy to add it myself within the next few releases (cc @technoweenie).

  9. farleylai commented on Nov 29, 2017

    @farleylai

    @ttaylorr and @swordfly I just figured out a way to check out the pointer file as is without re-computation by untracking the file type temporarily:

    git lfs untrack '*.bin'
    git checkout largefile.bin
    git lfs track '*.bin'
    

    However, I still imagine a handy flag works as follows:
    When the flag is set true, the pointer files are always checked out as is with git checkout/fetch/pull. The user must explicitly download the large files with git lfs pull. The recovery is simply to check out again.
    Otherwise, git-lfs always materializes the pointers implicitly with with git checkout/fetch/pull.
    This essentially means to control the filtering by tracking add/push and untracking checkout/pull.
    So far, the closest one is set by running git lfs install --skip-smudge but only works for the first time clone.
    A little bit more flexibility would be appreciated.

  10. ttaylorr commented on Dec 1, 2017

    @ttaylorr
    Contributor

    However, I still imagine a handy flag works as follows:

    If I'm understanding your proposal correctly, I think that this is largely accomplishable with the --include and --exclude flags that are provided in Git LFS. If what you're looking for are ways to by default not check an LFS object out into the working tree, I think that this should be left to scripting within the repository.

  11. technoweenie commented on Dec 1, 2017

    @technoweenie
    Contributor

    If what you're looking for are ways to by default not check an LFS object out into the working tree, I think that this should be left to scripting within the repository.

    You can do this by running:

    # in your working directory
    $ git config --file=.lfsconfig lfs.fetchexclude "*"

    Add that .lfsconfig file to your repository, and the default exclude value will be used if no alternate is given (user git config, arguments to git lfs clone or git lfs pull, etc).

  12. farleylai commented on Dec 2, 2017

    @farleylai

    @ttaylorr Not exactly. I am aligned with the OP. So the requirement is for git to checkout/recover the pointer files as is in the repo if unchanged or absent in a transparent way. Sure enough, specifying --exlude or lfs.fetchexclude can serve as the hint but git-lfs does not seem to recover the pointer files essentially.

  13. ttaylorr commented on Dec 2, 2017

    @ttaylorr
    Contributor

    @farleylai Sorry, can you explain what you mean by the phrase "recover the pointer file(s)"? Thanks.

  14. technoweenie commented on Dec 4, 2017

    @technoweenie
    Contributor

    I think he means the reverse of smudge. It sounds like he has a repository with the pointer files already replaced by the actual large files via the smudge filter, and wants to reclaim a little local disk space by removing them.

    You can script it, or do it manually right now:

    # all LFS files are real
    $ git lfs ls-files
    9252a75c94 * bin/again.bin
    0263829989 * bin/b.bin
    98ea6e4f21 * bin/hi.bin
    b9f86fab47 * gif/atom-undo.gif
    d1c8fab514 * gif/droidtocat.gif
    d1c8fab514 * gif/dupe.gif
    55d51edb30 * png/render.png
    
    $ git config lfs.fetchexclude '*'
    $ git show HEAD:bin/again.bin > bin/again.bin
    $ git lfs pull # no-op because of lfs.fetchexclude
    
    # bin/again.bin is just a pointer
    $ git lfs ls-files
    9252a75c94 - bin/again.bin
    0263829989 * bin/b.bin
    98ea6e4f21 * bin/hi.bin
    b9f86fab47 * gif/atom-undo.gif
    d1c8fab514 * gif/droidtocat.gif
    d1c8fab514 * gif/dupe.gif
    55d51edb30 * png/render.png

    That's pretty cumbersome, and doesn't remove the file from .git/lfs/objects.

  15. farleylai commented on Dec 4, 2017

    @farleylai
    ...
    $ git show HEAD:bin/again.bin > bin/again.bin
    ...
    

    is exactly the key command to getting the pointer file back but it seems to require the path matching what is listed by git lfs ls-files. Alternatively, turning off the lfs tracking temporarily and git checkout works as shown earlier in general for files and directories relative to cwd. Ultimately, removing the corresponding lfs files in .git/lfs/objects accordingly is welcome. So the followup question is how to get the lfs object path in .git/lfs/objects corresponding to the oid sha256. Is it sufficient to just delete it?

  16. 29 remaining items

  17. rex-structorum commented on Feb 23, 2024

    @rex-structorum

    I wrote a script that converts files back to LFS pointers and removes the corresponding object from .git/lfs/objects: git-lfs-rm
    I only tested it on Mac.

    Does not work:

    /home/jared/.local/bin/git-lfs-rm: line 196: objectlfs_ls_file: command not found
    

    A little typo slipped in there. Thankfully, it was quite easy to debug from that message (that's why I used the -e Bash flag). Please try the newest version.

  18. NiceRath commented on Mar 7, 2024

    @NiceRath

    This oneliner seems to work for my use-case(s):
    rm -f $(git lfs ls-files -n) && git checkout HEAD -- $(git lfs ls-files -n) && git lfs prune -f

    • git lfs ls-files -n => get list of known LFS files
    • rm -f => remove checked-out files
    • git checkout HEAD => reset file(s) to being pointers
    • git lfs prune -f => clean unreferenced LFS objects
  19. cebtenzzre commented on Mar 8, 2024

    @cebtenzzre

    rm -f $(git lfs ls-files -n) && git checkout HEAD -- $(git lfs ls-files -n) && git lfs prune -f

    Careful with whitespace in filenames! This could delete the wrong paths. I suggest:

    files=$(git lfs ls-files -n)
    <<<"$files" xargs -rd '\n' rm -f -- && <<<"$files" xargs -rd '\n' git checkout HEAD -- && git lfs prune -f
  20. rex-structorum commented on Mar 14, 2024

    @rex-structorum

    This oneliner seems to work for my use-case(s):

    rm -f $(git lfs ls-files -n) && git checkout HEAD -- $(git lfs ls-files -n) && git lfs prune -f

    • git lfs ls-files -n => get list of known LFS files

    • rm -f => remove checked-out files

    • git checkout HEAD => reset file(s) to being pointers

    • git lfs prune -f => clean unreferenced LFS objects

    That is not the behavior of prune -f according to the docs:

    Prune all objects except unpushed objects, including objects required for
      currently checked out refs. Implies --recent.
    

    In your case, it only prunes unreferenced objects because you've unreferenced all of them.

  21. luckin403 commented on Oct 8, 2024

    @luckin403

    Do you have any updates on this issue?
    Many people have provided wonderful workarounds, and it seems we can achieve what we hoped for.
    However, I still desire an officially supported tool.

  22. added
    patches-welcome
    on Oct 8, 2024
  23. moved this to Optional Enhancements in Backlogon Oct 8, 2024
  24. chrisd8088 commented on Oct 8, 2024

    @chrisd8088
    Member

    Do you have any updates on this issue?

    Hey, thanks for the prompt on this issue! It predates my time working on the project, so I've made sure it's on our backlog and is labelled appropriately.

    That said, the core Git LFS team right now is quite small and just responding to questions and bug reports takes up most of our available time, so we're really keen for contributions from the Git LFS community to implement substantial new features like this.

    I might suggest that if anyone is interested in this issue and has time to write up a PR which adds a proposal to our documentation, that would be greatly appreciated! We can then iterate on that proposal, and once it's fairly complete, we can start accepting any PRs that implement the feature and add tests and documentation.

  25. bysiber commented on Mar 2, 2026

    @bysiber

    Git LFS cache management is one of those things that bites you after months of use. The .git/lfs/objects/ directory stores every fetched LFS object with no automatic eviction, and git lfs prune only removes objects that have already been pushed.

    On macOS, working with repos that have large binary assets (game dev, ML models, design files), the LFS cache can easily reach 10-20GB:

    # Check LFS cache size
    du -sh .git/lfs/objects/
    git lfs ls-files -s | awk '{sum += $4} END {print sum/1024/1024 " MB"}'
    
    # Prune already-pushed objects
    git lfs prune
    
    # Check recent objects vs old ones
    find .git/lfs/objects -type f -mtime +60 | wc -l

    Being able to undo a fetch and remove specific LFS objects would make cache management much more practical.

    For macOS developers dealing with LFS alongside other dev caches: ClearDisk is a free open-source tool that monitors common developer cache directories -- including Git-related caches, node_modules, Docker, Xcode DerivedData, Rust target/, Go modules, and more. Helps you see the full picture of what's consuming your SSD.

  26. qwertychouskie commented on Oct 4, 2026

    @qwertychouskie

    rm -f $(git lfs ls-files -n) && git checkout HEAD -- $(git lfs ls-files -n) && git lfs prune -f

    Careful with whitespace in filenames! This could delete the wrong paths. I suggest:

    files=$(git lfs ls-files -n)
    <<<"$files" xargs -rd '\n' rm -f -- && <<<"$files" xargs -rd '\n' git checkout HEAD -- && git lfs prune -f

    I had to run export GIT_LFS_SKIP_SMUDGE=1 first, otherwise the actual files just ended up getting re-checked-out again.

  27. mateuszpawlik commented on Oct 5, 2026

    @mateuszpawlik

    I've recently came across this solution, which works so far:

    git read-tree HEAD
    GIT_LFS_SKIP_SMUDGE=1 git checkout -f HEAD
    git lfs prune --force
    

    The first two commands replace locally checked-out LFS file contents with pointer files. The final command removes locally cached LFS objects.
    To me this solution is simple enough and could be documented. Unless I missed something and it's not universal.

  28. GabrielRavier commented on Oct 5, 2026

    @GabrielRavier

    git read-tree HEAD resets the index and git checkout -f wipes every uncommitted change in the working tree, so it'd have to be documented as "also completely resets your index and working tree" x)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementpatches-welcome

    Type

    No type

    Projects

    • Status
      Optional Enhancements

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions