Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git lfs pull a single file #1351

Closed
mmahalwy opened this issue Jul 5, 2016 · 12 comments
Closed

git lfs pull a single file #1351

mmahalwy opened this issue Jul 5, 2016 · 12 comments
Labels

Comments

@mmahalwy
Copy link

mmahalwy commented Jul 5, 2016

Is there a way to only pull a single file?

@ttaylorr
Copy link
Contributor

ttaylorr commented Jul 5, 2016

Hi @mmahalwy! You can specify the --include or -I flag (they are aliases of each other) to only include a specific filename in your pull.

For example, if you only wanted to pull the file called "a.dat", try:

$ git lfs pull --include "a.dat"

Or, if you only wanted to pull files matching the ".dat" extension, try:

$ git lfs pull --include "*.dat"

When given either the --include or --exclude, LFS will only pull files that are explicitly included and not excluded. For more information on these filters, you can check out our documentation here. 😄

Let me know if you have any questions in the future!

@mmahalwy
Copy link
Author

mmahalwy commented Jul 5, 2016

@ttaylorr thank you for that awesome answer!

One last thing, does that include directories? For example, I have name a.dat that is inside directory first/ and second/. I want to only pull first/a.dat.

Thoughts?

@ttaylorr
Copy link
Contributor

ttaylorr commented Jul 5, 2016

Yep, just pass -I first/a.dat. a.dat alone will matcher neither first/a.dat or second/a.dat, since it pattern matches the whole path, not just the base 😄

@mmahalwy
Copy link
Author

mmahalwy commented Jul 6, 2016

sweet! TY!

@ttaylorr
Copy link
Contributor

ttaylorr commented Jul 6, 2016

You're welcome! Feel free to comment here again if you have any more questions 😄

@wutiejun
Copy link

wutiejun commented Mar 9, 2017

I can use git lfs pull -I "*a.dat", but git lfs pull -I "a.dat" is useless. Why?

@ttaylorr
Copy link
Contributor

ttaylorr commented Mar 9, 2017

@wutiejun we have a test-case that is passing that illustrates this behavior working correctly here.

If you're having trouble, feel free to open a new issue with further details.

@zezba9000
Copy link

How do you do this without a remote? Local repo only?

This doesn't work:

git lfs checkout --include "image.bmp" > image.bmp.orig

@apm963
Copy link

apm963 commented May 3, 2018

@zezba9000 I could not find a native command (ala show) that returns the contents of an LFS object. Instead, I was able to get the file by building a script that performed the following steps:

  • Get output of git show branch:path/to/file. Ex:
    version https://git-lfs.github.com/spec/v1
    oid sha256:7de96d152019316b4b1d6c89979292fa5c4034c9f28bb3e3304717314ddea594
    size 279
    
  • Parse the output to get the hash after sha256. To do this I split the output into lines, then performed a regex match on the second line for something like /^oid \w+:([a-fA-F0-9]+)$/
  • Parse the hash for the following pieces
    • Chars in position 0, 1 (eg. 7d)
    • Chars in position 2, 3 (eg. e9)
    • The full hash
  • Using those facets, I attempt to get the file from the following path (relative to the repo root): .git/lfs/objects/${firstHexPair}/${secondHexPair}/${fullHash}

That worked well for me. You'll, of course, need to make sure that the LFS objects exist in your local repository.

@WilliamTambellini
Copy link

@apm963 thank you.
Still, is it really the only way to extract the real file from a git lfs pointer file : should nt git lfs CLI provide a simple way to extract the file to a specified location ?

@bk2204
Copy link
Member

bk2204 commented Sep 10, 2019

You don't actually need a Git LFS-specific way to extract a file. You can use Git to do it yourself by using git cat-file --filters, like so:

$ git cat-file --filters HEAD:test1.bin

@LeungMaggie
Copy link

git fetch
git checkout origin/branchxx path/to/file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants