Nick Sergeant
Nick Sergeant

Reputation: 38113

How do I clone a subdirectory only of a Git repository?

I have my Git repository which, at the root, has two subdirectories:

/finisht
/static

When this was in SVN, /finisht was checked out in one place, while /static was checked out elsewhere, like so:

svn co svn+ssh://[email protected]/home/admin/repos/finisht/static static

Is there a way to do this with Git?

Upvotes: 2120

Views: 1615284

Answers (30)

git clone --filter + git sparse-checkout downloads only the required files

E.g., to clone only files in subdirectory small/ in this test repository: https://github.com/cirosantilli/test-git-partial-clone-big-small-no-bigtree

git clone -n --depth=1 --filter=tree:0 \
  https://github.com/cirosantilli/test-git-partial-clone-big-small-no-bigtree
cd test-git-partial-clone-big-small-no-bigtree
git sparse-checkout set --no-cone /small
git checkout

You could also select multiple directories for download with:

git sparse-checkout set --no-cone /small /small2

The slash in /small is required, if you do just small then git also downloads any other directory with basename small, we have a subdir/small/ directory in the repository to test that.[1]

Note: This method doesn't work for individual files however, but here is another method that does: How to sparsely checkout only one single file from a git repository?

Confirming that we actually didn't download uneeded files

The above test repository contains:

  • a big/ subdirectory with 10x 10MB files
  • 10x 10MB files 0, 1, ... 9 on toplevel (this is because certain previous attempts would download toplevel files)
  • a small/ and small2/ subdirectories with 1000 files of size one byte each

All contents are pseudo-random and therefore incompressible, so we can easily notice if any of the big files were downloaded, e.g. with ncdu.

So if you download anything you didn't want, you would get 100 MB extra, and it would be very noticeable.

All git commands ran were basically instantaneous, and we can confirm that the cloned repository is very small as desired:

du --apparent-size -hs * .* | sort -hs

giving:

2.0K    small
226K    .git

Running git clone downloads a single object, presumably the commit:

Cloning into 'test-git-partial-clone-big-small'...
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 1 (delta 0), pack-reused 0
Receiving objects: 100% (1/1), done.

and then the final checkout downloads the files we requested:

remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), 10.19 KiB | 2.04 MiB/s, done.
remote: Enumerating objects: 253, done.
remote: Counting objects: 100% (253/253), done.
Receiving objects: 100% (253/253), 2.50 KiB | 2.50 MiB/s, done.
remote: Total 253 (delta 0), reused 253 (delta 0), pack-reused 0
Your branch is up to date with 'origin/master'.

Tested on git 2.37.2, Ubuntu 22.10, on January 2023.

TODO Also prevent download of unneeded tree objects

The above method downloads all Git tree objects (i.e. directory listings, but not actual file contents). We can confirm that by running:

git ls-files

and seeing that it contains the directories large files such as:

big/0

In most projects this won't be an issue, as these should be small compared to the actual file contents, but the perfectionist in me would like to avoid them.

I've also created a very extreme repository with some very large tree objects (100 MB) under the directory big_tree: https://github.com/cirosantilli/test-git-partial-clone-big-small

Let me know if anyone finds a way to clone just the small/ directory from it!

About the Commands

The --filter option was added together with an update to the remote protocol, and it truly prevents objects from being downloaded from the server.

The sparse-checkout part is also needed unfortunately. You can also only download certain files with the much more understandable:

git clone --depth 1  --filter=blob:none  --no-checkout \
  https://github.com/cirosantilli/test-git-partial-clone-big-small
cd test-git-partial-clone-big-small
git checkout master -- d1

but that method for some reason downloads files one by one very slowly, making it unusable unless you have very few files in the directory.

Another less verbose but failed attempt was:

git clone --depth 1 --filter=blob:none --sparse \
  https://github.com/cirosantilli/test-git-partial-clone-big-small
cd test-git-partial-clone-big-small
git sparse-checkout set /small

but that downloads all files in the toplevel directory: How to prevent git clone --filter=blob:none --sparse from downloading files on the root directory?

The Dream: Any Directory can have Web Interface Metadata

This feature could revolutionize Git.

Imagine having all the code base of your enterprise in a single monorepo without ugly third-party tools like repo.

Imagine storing huge blobs directly in the repo without any ugly third party extensions.

Imagine if GitHub would allow per file / directory metadata like stars and permissions, so you can store all your personal stuff under a single repo.

Imagine if submodules were treated exactly like regular directories: just request a tree SHA, and a DNS-like mechanism resolves your request, first looking on your local ~/.git, then first to closer servers (your enterprise's mirror / cache) and ending up on GitHub.

I have a dream.

The test cone monorepo philosophy

This is a possible philosophy for monorepo maintenance without submodules.

We want to avoid submodules because it is annoying to have to commit to two separate repositories every time you make a change that has a submodule and non-submodule component.

Every directory with a Makefile or analogous should build and test itself.

Such directories can depend on either:

  • every file and subdirectory under it directly at their latest versions
  • external directories can be relied upon only at specified versions

Until git starts supporting this natively (i.e. submodules that can track only subdirectories), we can support this with some metadata in a git tracked file:

monorepo.json

{
    "path": "some/useful/lib",
    "sha": 12341234123412341234,
}

where sha refers to the usual SHA of the entire repository. Then we need scripts that will checkout such directories e.g. under a gitignored monorepo folder:

monorepo/som/useful/lib

Whenever you change a file, you have to go up the tree and test all directories that have Makefile. This is because directories can depend on subdirectories at their latest versions, so you could always break something above you.

Related:

Upvotes: 1372

adamz
adamz

Reputation: 353

#!/usr/bin/env bash

set -e

URL="$*"
START=$(echo "$URL" | sed 's%/tree/main/%\n%g' | head -n1)
END=$(echo "$URL" | sed 's%/tree/main/%\n%g' | tail -n1)
FOLDER=$(echo "$END" | rev | cut -d'/' -f1 | rev)
REPO=$(echo "$START" | cut -d'/' -f5)
CHECKOUT="${REPO}_$(uuidgen)"

function get_folder {
    git clone -n --depth=1 --filter=tree:0 "$START" "$CHECKOUT" &>/dev/null
    cd "$CHECKOUT"
    git sparse-checkout set --no-cone "$END" &>/dev/null
    git checkout &>/dev/null
    mv "$END" ../"${FOLDER}@$(date '+%Y-%m-%dT%H:%M:%S')"
    cd ..
    rm -rf "$CHECKOUT"
    return $?
}

function await_last {
    pid=$!
    while ps -a | awk '{print $1}' | grep -q "${pid}"; do
        for s in / - \\ \|; do
            printf "\r%s" "$s"
            sleep .1
        done
    done
    wait ${pid}
    ret=$?
    exit ${ret}
}

get_folder &
await_last

setup:

assuming:

  • "${HOME}/bin" is in your $PATH. if not substitute a folder that is.
  • you're on a mac and have pbpaste. if not paste the script however you
  1. copy the script
  2. pbpaste > ~/bin/folderdl
  3. chmod +x ~/bin/folderdl

usage:

  1. get the URL of your browsers view into the folder you want
    e.g. https://github.com/microsoft/vscode/tree/main/scripts

  2. call script

    folderdl https://github.com/microsoft/vscode/tree/main/scripts
    
  3. wait a second

You now have your (timestamped) folder.

> tre

[0] .
└── [1] scripts@2024-09-04T14:43
    ├── [2] debugger-scripts-api.d.ts
    ├── [3] code-cli.bat
    ├── [4] code-server.js
    ├── [5] test.bat
    ├── [6] test-web-integration.bat
    ├── [7] node-electron.sh
    ├── [8] hot-reload-injected-script.js
    ├── [9] node-electron.bat
    ├── [10] test-remote-integration.bat
    ├── [11] test-integration.bat
    ├── [12] test-amd.bat
    ├── [13] test-integration-amd.sh
    ├── [14] playground-server.ts
    ├── [15] code-web.sh
    ├── [16] test-amd.sh
    ├── [17] test-documentation.bat
    ├── [18] code-perf.js
    ├── [19] xterm-update.ps1
    ├── [20] xterm-symlink.ps1
    ├── [21] code.bat
    ├── [22] code-web.js
    ├── [23] generate-definitelytyped.sh
    ├── [24] test-web-integration.sh
    ├── [25] test-integration.sh
    ├── [26] xterm-update.js
    ├── [27] test-remote-integration.sh
    ├── [28] test.sh
    ├── [29] code-server.sh
    ├── [30] npm.sh
    ├── [31] code-cli.sh
    ├── [32] test-documentation.sh
    ├── [33] code.sh
    ├── [34] code-server.bat
    ├── [35] code-web.bat
    ├── [36] test-integration-amd.bat
    └── [37] npm.bat

Upvotes: 0

udondan
udondan

Reputation: 59979

You can combine the sparse checkout and the shallow clone features. The shallow clone cuts off the history and the sparse checkout only pulls the files matching your patterns.

git init <repo>
cd <repo>
git remote add origin <url>
git config core.sparsecheckout true
echo "finisht/*" >> .git/info/sparse-checkout
git pull --depth=1 origin master

You'll need minimum git 1.9 for this to work. Tested it myself only with 2.2.0 and 2.2.2.

This way you'll be still able to push, which is not possible with git archive.

Upvotes: 454

Carson
Carson

Reputation: 7948

here is what I do

git init
git sparse-checkout init
git sparse-checkout set "YOUR_DIR_PATH"
git remote add origin https://github.com/AUTH/REPO.git
git pull --depth 1 origin <SHA1_or_BRANCH_NAME>

Simple note

  • sparse-checkout

  • git sparse-checkout init many articles will tell you to set git sparse-checkout init --cone If I add --cone will get some files that I don't want.

  • git sparse-checkout set "..." will set the .git\info\sparse-checkout file contents as ...

    Suppose you don't want to use this command. Instead, you can open the git\info\sparse-checkout and then edit.


Example

Suppose I want to get 2 folderfull repo size>10GB↑ (include git), as below total size < 2MB

  1. chrome/common/extensions/api
  2. chrome/common/extensions/permissions
git init
git sparse-checkout init
// git sparse-checkout set "chrome/common/extensions/api/"
start .git\info\sparse-checkout   👈 open the "sparse-checkut" file

/* .git\info\sparse-checkout  for example you can input the contents as below 👇
chrome/common/extensions/api/
!chrome/common/extensions/api/commands/     👈 ! unwanted : https://www.git-scm.com/docs/git-sparse-checkout#_full_pattern_set
!chrome/common/extensions/api/devtools/
chrome/common/extensions/permissions/
*/

git remote add origin https://github.com/chromium/chromium.git
start .git\config

/* .git\config
[core]
    repositoryformatversion = 1
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[extensions]
    worktreeConfig = true
[remote "origin"]
    url = https://github.com/chromium/chromium.git
    fetch = +refs/heads/*:refs/remotes/Github/*
    partialclonefilter = blob:none  // 👈 Add this line, This is important. Otherwise, your ".git" folder is still large (about 1GB)
*/
git pull --depth 1 origin 2d4a97f1ed2dd875557849b4281c599a7ffaba03
// or
// git pull --depth 1 origin master

  • partialclonefilter = blob:none

    I know to add this line because I know from: git clone --filter=blob:none it will write this line. so I imitate it.

git version: git version 2.29.2.windows.3

Upvotes: 13

Patrick Simard
Patrick Simard

Reputation: 2375

So i tried everything in this tread and nothing worked for me ... Turns out that on version 2.24 of Git (the one that comes with cpanel at the time of this answer), you don't need to do this

echo "wpm/*" >> .git/info/sparse-checkout

all you need is the folder name

wpm/*

So in short you do this

git config core.sparsecheckout true

you then edit the .git/info/sparse-checkout and add the folder names (one per line) with /* at the end to get subfolders and files

wpm/*

Save and run the checkout command

git checkout master

The result was the expected folder from my repo and nothing else Upvote if this worked for you

Upvotes: -1

YenForYang
YenForYang

Reputation: 3284

I wrote a .gitconfig [alias] for performing a "sparse checkout". Check it out (no pun intended):

On Windows run in cmd.exe

git config --global alias.sparse-checkout "!f(){ [ $# -eq 2 ] && L=${1##*/} L=${L%.git} || L=$2; mkdir -p \"$L/.git/info\" && cd \"$L\" && git init --template= && git remote add origin \"$1\" && git config core.sparseCheckout 1; [ $# -eq 2 ] && echo \"$2\" >> .git/info/sparse-checkout || { shift 2; for i; do echo $i >> .git/info/sparse-checkout; done }; git pull --depth 1 origin master;};f"

Otherwise:

git config --global alias.sparse-checkout '!f(){ [ $# -eq 2 ] && L=${1##*/} L=${L%.git} || L=$2; mkdir -p "$L/.git/info" && cd "$L" && git init --template= && git remote add origin "$1" && git config core.sparseCheckout 1; [ $# -eq 2 ] && echo "$2" >> .git/info/sparse-checkout || { shift 2; for i; do echo $i >> .git/info/sparse-checkout; done }; git pull --depth 1 origin master;};f'

Usage:

# Makes a directory ForStackExchange with Plug checked out
git sparse-checkout https://github.com/YenForYang/ForStackExchange Plug

# To do more than 1 directory, you have to specify the local directory:
git sparse-checkout https://github.com/YenForYang/ForStackExchange ForStackExchange Plug Folder

The git config commands are 'minified' for convenience and storage, but here is the alias expanded:

# Note the --template= is for disabling templates.
# Feel free to remove it if you don't have issues with them (like I did)
# `mkdir` makes the .git/info directory ahead of time, as I've found it missing sometimes for some reason
f(){
    [ "$#" -eq 2 ] && L="${1##*/}" L=${L%.git} || L=$2;
    mkdir -p "$L/.git/info"
        && cd "$L"
        && git init --template=
        && git remote add origin "$1"
        && git config core.sparseCheckout 1;
    [ "$#" -eq 2 ]
        && echo "$2" >> .git/info/sparse-checkout
        || {
            shift 2;
            for i; do
                echo $i >> .git/info/sparse-checkout;
            done
        };
    git pull --depth 1 origin master;
};
f

Upvotes: 3

jxramos
jxramos

Reputation: 8256

Here's a shell script I wrote for the use case of a single subdirectory sparse checkout

coSubDir.sh

localRepo=$1
remoteRepo=$2
subDir=$3


# Create local repository for subdirectory checkout, make it hidden to avoid having to drill down to the subfolder
mkdir ./.$localRepo
cd ./.$localRepo
git init
git remote add -f origin $remoteRepo
git config core.sparseCheckout true

# Add the subdirectory of interest to the sparse checkout.
echo $subDir >> .git/info/sparse-checkout

git pull origin master

# Create convenience symlink to the subdirectory of interest
cd ..
ln -s ./.$localRepo/$subDir $localRepo

Upvotes: 6

Jaswinder
Jaswinder

Reputation: 2289

git init <repo>
cd <repo>
git remote add origin <url>
git config core.sparsecheckout true
echo "<path you want to clone>/*" >> .git/info/sparse-checkout
git pull --depth=1 origin <branch you want to fetch>

Example for cloning only Jetsurvey Folder from this repo

git init MyFolder
cd MyFolder 
git remote add origin [email protected]:android/compose-samples.git
git config core.sparsecheckout true
echo "Jetsurvey/*" >> .git/info/sparse-checkout
git pull --depth=1 origin main

Upvotes: 5

Chronial
Chronial

Reputation: 70653

What you are trying to do is called a sparse checkout, and that feature was added in Git 1.7.0 (Feb. 2012). The steps to do a sparse clone are as follows:

mkdir <repo>
cd <repo>
git init
git remote add -f origin <url>

This creates an empty repository with your remote, and fetches all objects but doesn't check them out. Then do:

git config core.sparseCheckout true

Now you need to define which files/folders you want to actually check out. This is done by listing them in .git/info/sparse-checkout, eg:

echo "some/dir/" >> .git/info/sparse-checkout
echo "another/sub/tree" >> .git/info/sparse-checkout

Last but not least, update your empty repo with the state from the remote:

git pull origin master

You will now have files "checked out" for some/dir and another/sub/tree on your file system (with those paths still), and no other paths present.

You might want to have a look at the extended tutorial and you should probably read the official documentation for sparse checkout and read-tree.

As a function:

function git_sparse_clone() (
  rurl="$1" localdir="$2" && shift 2

  mkdir -p "$localdir"
  cd "$localdir"

  git init
  git remote add -f origin "$rurl"

  git config core.sparseCheckout true

  # Loops over remaining args
  for i; do
    echo "$i" >> .git/info/sparse-checkout
  done

  git pull origin master
)

Usage:

git_sparse_clone "http://github.com/tj/n" "./local/location" "/bin"

Note that this will still download the whole repository from the server – only the checkout is reduced in size. At the moment it is not possible to clone only a single directory. But if you don't need the history of the repository, you can at least save on bandwidth by creating a shallow clone. See udondan's answer below for information on how to combine shallow clone and sparse checkout.


As of Git 2.25.0 (Jan 2020) an experimental sparse-checkout command is added in Git:

git sparse-checkout init
# same as:
# git config core.sparseCheckout true

git sparse-checkout set "A/B"
# same as:
# echo "A/B" >> .git/info/sparse-checkout

git sparse-checkout list
# same as:
# cat .git/info/sparse-checkout

Upvotes: 1905

david_adler
david_adler

Reputation: 10902

I wrote a Python 2 script for downloading a subdirectory from GitHub.

Usage:

python get_git_sub_dir.py path/to/sub/dir <RECURSIVE>

Python 3 fork https://gitlab.com/vitaly-zdanevich/github-download-dir

Upvotes: 10

le_top
le_top

Reputation: 493

@Chronial 's answer is no longer applicable to recent versions, but it was a useful answer as it proposed a script.

To checkout only one or more subdirectories of a branch, I created the following shell function. It gets a shallow copy of only the most recent version in the branch for the provided directories.

function git_sparse_clone_branch() (
  local rurl="$1" localdir="$2" branch="$3" && shift 3

  git clone "$rurl" --branch "$branch" --no-checkout "$localdir" --depth 1  # limit history
  cd "$localdir" || return
  
  # git sparse-checkout init --cone  # fetch only root file

  # Loops over remaining args
  for i; do
    git sparse-checkout add "$i"
    # git sparse-checkout set "$i"
  done

  git checkout --ignore-other-worktrees "$branch"
)

So example use:

git_sparse_clone_branch [email protected]:user/repo.git localpath branch-to-clone path1_to_fetch path2_to_fetch

In my case the clone was "only" 23MB versus 385MB for the full clone.

Tested with git version 2.36.1 .

Upvotes: 5

J&#246;rg W Mittag
J&#246;rg W Mittag

Reputation: 369430

As of Git 2.19, this is finally possible, as can be seen in this answer.

Consider upvoting that answer.

Note: in Git 2.19, only client-side support is implemented, server-side support is still missing, so it only works when cloning local repositories. Also note that large Git hosters, e.g. GitHub, don't actually use the Git server, they use their own implementation, so even if support shows up in the Git server, it does not automatically mean that it works on Git hosters. (On the other hand, since they don't use the Git server, they could implement it faster in their own implementations before it shows up in Git server.)


No, that's not possible in Git.

Implementing something like this in Git would be a substantial effort and it would mean that the integrity of the client-side repository could no longer be guaranteed. If you are interested, search for discussions on "sparse clone" and "sparse fetch" on the Git mailing list.

In general, the consensus in the Git community is that if you have several directories that are always checked out independently, then these are really two different projects and should live in two different repositories. You can glue them back together using Git Submodules.

Upvotes: 781

Evan MJ
Evan MJ

Reputation: 2813

2022 Answer

I'm not sure why there are so many complicated answers to this question. It can be done easily by doing a sparse cloning of the repo, to the folder that you want.

  1. Navigate to the folder where you'd like to clone the subdirectory.
  2. Open cmd and run the following commands.
  3. git clone --filter=blob:none --sparse %your-git-repo-url%
  4. cd %the repository directory%
  5. git sparse-checkout add %subdirectory-to-be-cloned%
  6. cd %your-subdirectory%

Voila! Now you have cloned only the subdirectory that you want!

Explanation - What are these commands doing really?

git clone --filter=blob:none --sparse %your-git-repo-url%

In the above command,

  • --filter=blob:none => You tell git that you only want to clone the metadata files. This way git collects the basic branch details and other meta from remote, which will ensure that your future checkouts from origin are smooth.
  • --sparse => Tell git that this is a sparse clone. Git will checkout only the root directory in this case.

Now git is informed with the metadata and ready to checkout any subdirectories/files that you want to work with.

git sparse-checkout add gui-workspace ==> Checkout folder

git sparse-checkout add gui-workspace/assets/logo.png ==> Checkout a file

Sparse clone is particularly useful when there is a large repo with several subdirectories and you're not always working on them all. Saves a lot of time and bandwidth when you do a sparse clone on a large repo.

Additionally, now in this partially cloned repo you can continue to checkout and work like you normally would. All these commands work perfectly.

git switch -c  %new-branch-name% origin/%parent-branch-name% (or) git checkout -b %new-branch-name% origin/%parent-branch-name% 
git commit -m "Initial changes in sparse clone branch"
git push origin %new-branch-name%

Upvotes: 170

Marinos An
Marinos An

Reputation: 10818

(extending this answer )

Cloning subdirectory in specific tag

If you want to clone a specific subdirectory of a specific tag you can follow the steps below.

I clone the distribution/src/main/release/samples/ subdirectory of cxf github repo in cxf-3.5.4 tag.

Note: if you attempt to just clone the above repo you will see that it is very big. The commands below clones only what is needed.

git clone --depth 1 --filter=blob:none --sparse https://github.com/apache/cxf
cd cxf/
git sparse-checkout set distribution/src/main/release/samples/
git fetch --depth 1 origin cxf-3.5.4
# This is the hash on which the tag points, however using the tag does not work.
git switch --detach 3ef4fde

Cloning subdirectory in specific branch

I clone the distribution/src/main/release/samples/ subdirectory of cxf github repo in 2.6.x-fixes branch.

git clone --depth 1 --filter=blob:none --sparse https://github.com/apache/cxf --branch 2.6.x-fixes
cd cxf/
git sparse-checkout set distribution/src/main/release/samples/

Upvotes: 2

You can still use svn:

svn export https://[email protected]/home/admin/repos/finisht/static static --force

to "git clone" a subdirectory and then to "git pull" this subdirectory.

(It is not intended to commit & push.)

Upvotes: 1

NeoZoom.lua
NeoZoom.lua

Reputation: 2891

For macOS users

For zsh users (macOS users, specifically) cloning Repos with ssh, I just create a zsh command based on the answer by @Ciro Santilli:

requirement: The version of git matters. It doesn't work on 2.25.1 because of the --sparse option. Try upgrade your git to the latest version. (e.g. tested 2.36.1)

example usage:

git clone [email protected]:google-research/google-research.git etcmodel

code:

function gitclone {
    readonly repo_root=${1?Usage: gitclone repo.git sub_dir}
    readonly repo_sub=${2?Usage: gitclone repo.git sub_dir}
    echo "-- Cloning $repo_root/$repo_sub"
    git clone \
      --depth 1 \
      --filter=tree:0 \
      --sparse \
      $repo_root \
    ;
    repo_folder=${repo_root#*/}
    repo_folder=${repo_folder%.*}
    cd $repo_folder
    git sparse-checkout set $repo_sub
    cd -
}


gitclone "$@"

Upvotes: 0

Abdul97j
Abdul97j

Reputation: 145

It worked for me- (git version 2.35.1)

git init
git remote add origin <YourRepoUrl>
git config core.sparseCheckout true
git sparse-checkout set <YourSubfolderName>
git pull origin <YourBranchName>

Upvotes: 11

Ilir Liburn
Ilir Liburn

Reputation: 222

I don't know if anyone succeeded pulling specific directory, here is my experience: git clone --filter=blob:none --single-branch <repo>, cancel immediately while downloading objects, enter repo, then git checkout origin/master <dir>, ignore errors (sha1), enter dir, repeat checkout (using new dir) for every sub-directory. I managed to quickly get source files in this way

Upvotes: 0

  • If you want to clone

    git clone --no-checkout <REPOSITORY_URL>
    cd <REPOSITORY_NAME>
    
    1. Now set the specific file / directory you wish to pull into the working-directory:
      git sparse-checkout set <PATH_TO_A_SPECIFIC_DIRECTORY_OR_FILE_TO_PULL>
      
    2. Afterwards, you should reset hard your working-directory to the commit you wish to pull.

      For example, we will reset it to the default origin/master's HEAD commit.

      git reset --hard HEAD
      
  • If you want to git init and then remote add

    git init
    git remote add origin <REPOSITORY_URL>
    
    1. Now set the specific file / directory you wish to pull into the working-directory:
      git sparse-checkout set <PATH_TO_A_SPECIFIC_DIRECTORY_OR_FILE_TO_PULL>
      
    2. Pull the last commit:
      git pull origin master
      

NOTE:

If you want to add another directory/file to your working-directory, you may do it like so:

git sparse-checkout add <PATH_TO_ANOTHER_SPECIFIC_DIRECTORY_OR_FILE_TO_PULL>

If you want to add all the repository to the working-directory, do it like so:

git sparse-checkout add *

If you want to empty the working-directory, do it like so:

git sparse-checkout set empty

If you want, you can view the status of the tracked files you have specified, by running:

git status

If you want to exit the sparse mode and clone all the repository, you should run:

git sparse-checkout set *
git sparse-checkout set init
git sparse-checkout set disable

Upvotes: 0

Parables Boltnoel
Parables Boltnoel

Reputation: 31

degit makes copies of git repositories. When you run degit some-user/some-repo, it will find the latest commit on https://github.com/some-user/some-repo and download the associated tar file to ~/.degit/some-user/some-repo/commithash.tar.gz if it doesn't already exist locally. (This is much quicker than using git clone, because you're not downloading the entire git history.)

degit <https://github.com/user/repo/subdirectory> <output folder>

Find out more https://www.npmjs.com/package/degit

Upvotes: 1

hillu
hillu

Reputation: 9611

If you never plan to interact with the repository from which you cloned, you can do a full git clone and rewrite your repository using

git filter-branch --subdirectory-filter <subdirectory>

This way, at least the history will be preserved.

Upvotes: 82

Eric Stricklin
Eric Stricklin

Reputation: 31

Lots of great responses here, but I wanted to add that using the quotations around the directory names was failing for me on Windows Sever 2016. The files simply were not being downloaded.

Instead of

"mydir/myfolder"

I had to use

mydir/myfolder

Also, if you want to simply download all sub directories just use

git sparse-checkout set *

Upvotes: 3

Mike Slinn
Mike Slinn

Reputation: 8405

Lots of good ideas and scripts above. I could not help myself and combined them into a bash script with help and error checking:

#!/bin/bash

function help {
  printf "$1
Clones a specific directory from the master branch of a git repository.

Syntax:
  $(basename $0) [--delrepo] repoUrl sourceDirectory [targetDirectory]

If targetDirectory is not specified it will be set to sourceDirectory.
Downloads a sourceDirectory from a Git repository into targetdirectory.
If targetDirectory is not specified, a directory named after `basename sourceDirectory`
will be created under the current directory.

If --delrepo is specified then the .git subdirectory in the clone will be removed after cloning.


Example 1:
Clone the tree/master/django/conf/app_template directory from the master branch of
[email protected]:django/django.git into ./app_template:

\$ $(basename $0) [email protected]:django/django.git django/conf/app_template

\$ ls app_template/django/conf/app_template/
__init__.py-tpl  admin.py-tpl  apps.py-tpl  migrations  models.py-tpl  tests.py-tpl  views.py-tpl


Example 2:
Clone the django/conf/app_template directory from the master branch of
https://github.com/django/django/tree/master/django/conf/app_template into ~/test:

\$ $(basename $0) [email protected]:django/django.git django/conf/app_template ~/test

\$ ls test/django/conf/app_template/
__init__.py-tpl  admin.py-tpl  apps.py-tpl  migrations  models.py-tpl  tests.py-tpl  views.py-tpl

"
  exit 1
}

if [ -z "$1" ]; then help "Error: repoUrl was not specified.\n"; fi
if [ -z "$2" ]; then help "Error: sourceDirectory was not specified."; fi

if [ "$1" == --delrepo ]; then
  DEL_REPO=true
  shift
fi

REPO_URL="$1"
SOURCE_DIRECTORY="$2"
if [ "$3" ]; then
  TARGET_DIRECTORY="$3"
else
  TARGET_DIRECTORY="$(basename $2)"
fi

echo "Cloning into $TARGET_DIRECTORY"
mkdir -p "$TARGET_DIRECTORY"
cd "$TARGET_DIRECTORY"
git init
git remote add origin -f "$REPO_URL"
git config core.sparseCheckout true

echo "$SOURCE_DIRECTORY" > .git/info/sparse-checkout
git pull --depth=1 origin master

if [ "$DEL_REPO" ]; then rm -rf .git; fi

Upvotes: 1

Everett
Everett

Reputation: 9558

Just to clarify some of the great answers here, the steps outlined in many of the answers assume that you already have a remote repository somewhere.

Given: an existing git repository, e.g. [email protected]:some-user/full-repo.git, with one or more directories that you wish to pull independently of the rest of the repo, e.g. directories named app1 and app2

Assuming you have a git repository as the above...

Then: you can run steps like the following to pull only specific directories from that larger repo:

mkdir app1
cd app1
git init
git remote add origin [email protected]:some-user/full-repo.git
git config core.sparsecheckout true
echo "app1/" >> .git/info/sparse-checkout
git pull origin master

I had mistakenly thought that the sparse-checkout options had to be set on the original repository, but this is not the case: you define which directories you want locally, prior to pulling from the remote. The remote repo doesn't know or care about your only wanting to track a part of the repo.

Hope this clarification helps someone else.

Upvotes: 7

weberjn
weberjn

Reputation: 1985

If you're actually ony interested in the latest revision files of a directory, Github lets you download a repository as Zip file, which does not contain history. So downloading is very much faster.

Upvotes: 2

expelledboy
expelledboy

Reputation: 2373

While I hate actually having to use svn when dealing with git repos :/ I use this all the time;

function git-scp() (
  URL="$1" && shift 1
  svn export ${URL/blob\/master/trunk}
)

This allows you to copy out from the github url without modification. Usage;

--- /tmp » git-scp https://github.com/dgraph-io/dgraph/blob/master/contrib/config/kubernetes/helm                                                                                                                  1 ↵
A    helm
A    helm/Chart.yaml
A    helm/README.md
A    helm/values.yaml
Exported revision 6367.

--- /tmp » ls | grep helm
Permissions Size User    Date Modified    Name
drwxr-xr-x     - anthony 2020-01-07 15:53 helm/

Upvotes: 1

Nasir Iqbal
Nasir Iqbal

Reputation: 872

Using Linux? And only want easy to access and clean working tree ? without bothering rest of code on your machine. try symlinks!

git clone https://github.com:{user}/{repo}.git ~/my-project
ln -s ~/my-project/my-subfolder ~/Desktop/my-subfolder

Test

cd ~/Desktop/my-subfolder
git status

Upvotes: 7

BARJ
BARJ

Reputation: 2002

This will clone a specific folder and remove all history not related to it.

git clone --single-branch -b {branch} [email protected]:{user}/{repo}.git
git filter-branch --subdirectory-filter {path/to/folder} HEAD
git remote remove origin
git remote add origin [email protected]:{user}/{new-repo}.git
git push -u origin master

Upvotes: 11

Anona112
Anona112

Reputation: 3792

For other users who just want to download a file/folder from github, simply use:

svn export <repo>/trunk/<folder>

e.g.

svn export https://github.com/lodash/lodash.com/trunk/docs

(yes, that's svn here. apparently in 2016 you still need svn to simply download some github files)

Courtesy: Download a single folder or directory from a GitHub repo

Important - Make sure you update the github URL and replace /tree/master/ with '/trunk/'.

As bash script:

git-download(){
    folder=${@/tree\/master/trunk}
    folder=${folder/blob\/master/trunk}
    svn export $folder
}

Note This method downloads a folder, does not clone/checkout it. You can't push changes back to the repository. On the other hand - this results in smaller download compared to sparse checkout or shallow checkout.

Upvotes: 204

ErichBSchulz
ErichBSchulz

Reputation: 15659

This looks far simpler:

git archive --remote=<repo_url> <branch> <path> | tar xvf -

Upvotes: 72

Related Questions