Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 670427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:15:41+00:00 2026-05-14T00:15:41+00:00

How do I checkout just one file from a git repo?

  • 0

How do I checkout just one file from a git repo?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:15:41+00:00Added an answer on May 14, 2026 at 12:15 am

    Originally, I mentioned in 2012 git archive (see Jared Forsyth‘s answer and Robert Knight‘s answer), since git1.7.9.5 (March 2012), Paul Brannan‘s answer:

    git archive --format=tar --remote=origin HEAD:path/to/directory -- filename | tar -O -xf -
    

    But: in 2013, that was no longer possible for remote https://github.com URLs.
    See the old page "Can I archive a repository?"

    The current (2018) page "About archiving content and data on GitHub" recommends using third-party services like GHTorrent or GH Archive.


    So you can also deal with local copies/clone:

    You could alternatively do the following if you have a local copy of the bare repository as mentioned in this answer,

    git --no-pager --git-dir /path/to/bar/repo.git show branch:path/to/file >file
    

    Or you must clone first the repo, meaning you get the full history:

    • in the .git repo

    • in the working tree.

    • But then you can do a sparse checkout (if you are using Git1.7+),:

      • enable the sparse checkout option (git config core.sparsecheckout true)
      • adding what you want to see in the .git/info/sparse-checkout file
      • re-reading the working tree to only display what you need

    To re-read the working tree:

    $ git read-tree -m -u HEAD
    

    That way, you end up with a working tree including precisely what you want (even if it is only one file)


    Richard Gomes points (in the comments) to "How do I clone, fetch or sparse checkout a single directory or a list of directories from git repository?"

    A bash function which avoids downloading the history, which retrieves a single branch and which retrieves a list of files or directories you need.


    With Git 2.40 (Q1 2023), the logic to see if we are using the "cone" mode by checking the sparsity patterns has been tightened to avoid mistaking a pattern that names a single file as specifying a cone.

    See commit 5842710 (03 Jan 2023) by William Sprent (williams-unity).
    (Merged by Junio C Hamano — gitster — in commit ab85a7d, 16 Jan 2023)

    dir: check for single file cone patterns

    Signed-off-by: William Sprent
    Acked-by: Victoria Dye

    The sparse checkout documentation states that the cone mode pattern set is limited to patterns that either recursively include directories or patterns that match all files in a directory.
    In the sparse checkout file, the former manifest in the form:

    /A/B/C/
    

    while the latter become a pair of patterns either in the form:

    /A/B/
    !/A/B/*/
    

    or in the special case of matching the toplevel files:

    /*
    !/*/
    

    The ‘add_pattern_to_hashsets()‘ function contains checks which serve to disable cone-mode when non-cone patterns are encountered.
    However, these do not catch when the pattern list attempts to match a single file or directory, e.g. a pattern in the form:

    /A/B/C
    

    This causes sparse-checkout to exhibit unexpected behaviour when such a pattern is in the sparse-checkout file and cone mode is enabled.

    Concretely, with the pattern like the above, sparse-checkout, in non-cone mode, will only include the directory or file located at ‘/A/B/C‘.
    However, with cone mode enabled, sparse-checkout will instead just manifest the toplevel files but not any file located at ‘/A/B/C‘.

    Relatedly, issues occur when supplying the same kind of filter when partial cloning with ‘--filter=sparse:oid=<oid>‘.
    ‘upload-pack‘ will correctly just include the objects that match the non-cone pattern matching.
    Which means that checking out the newly cloned repo with the same filter, but with cone mode enabled, fails due to missing objects.

    To fix these issues, add a cone mode pattern check that asserts that every pattern is either a directory match or the pattern ‘/*‘.
    Add a test to verify the new pattern check and modify another to reflect that non-directory patterns are caught earlier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 497k
  • Answers 497k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I solved my own problem! I am using JDK 6,… May 16, 2026 at 12:05 pm
  • Editorial Team
    Editorial Team added an answer So if you are able to get the file name… May 16, 2026 at 12:05 pm
  • Editorial Team
    Editorial Team added an answer Declare a namespace scope operator*, so that you can also… May 16, 2026 at 12:05 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

So I just started using Git and GitHub. It's still a steep climb, but
I was trying to merge a dev branch into master. git checkout master git
In my project I need to use third party code, stored in several Git
master branch has these files and folders (simplified): C:\Local\TickZoom\Project>ls file.txt name.txt public public branch
I'd read that when renaming files in Git , you should commit any changes,
I have been studding GIT for the last couple of weeks in an attempt
Often when I do a checkout of a different branch, or a reset, I
Using the SVN task from tigris I can't seem to find a way to
I work from several different locations on the same project and I use SVN
I recently just installed Eclipse onto a Ubuntu 10.04 machine. Now, I'm reading Getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.