LoginSignup
111
102

More than 3 years have passed since last update.

Gitの一部のディレクトリだけ取得する方法

Last updated at Posted at 2019-06-11

Gitのリポジトリの一部のディレクトリしか使わない時にを全部取得するとすごくたくさんファイルがあるから嫌な時の方法

1.空ディレクトリを作る

# 空ディレクトリを作る
$ mkdir sub-directory-name

# 作ったディレクトリに移動する
$ cd sub-directory-name

2.空リポジトリを作って初期化

# 空リポジトリを初期化する
$ git init
Initialized empty Git repository in C:/path/to/sub-directory-name/.git/

# 初期化した状態でのconfig
$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true

3.一部だけ取得できるようにsparsecheckoutを設定する

$ git config core.sparsecheckout true

# sparsecheckoutを設定した状態のconfig
$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
        sparsecheckout = true

4.取得元のリポジトリを設定する

# 取得元のリポジトリを設定する
$ git remote add origin https://github.com/username/all.git

# リポジトリを設定した状態のconfig
$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
        sparsecheckout = true
[remote "origin"]
        url = https://github.com/username/all.git
        fetch = +refs/heads/*:refs/remotes/origin/*

5.取得したいディレクトリをsparse-checkoutに設定する

# 取得したいディレクトリをsparse-checkoutに設定する
$ echo sub-directory-name > .git/info/sparse-checkout

6.pullする

# pullする
$ git pull origin master
remote: Enumerating objects: 13, done.
remote: Counting objects: 100% (13/13), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 7172 (delta 6), reused 7 (delta 4), pack-reused 7159
Receiving objects: 100% (7172/7172), 280.58 MiB | 5.91 MiB/s, done.
Resolving deltas: 100% (2559/2559), done.
From https://github.com/username/all
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
Checking out files: 100% (2021/2021), done.
使いたいブランチを指定したい場合
$ git pull origin {ブランチ名}

特定のファイルだけpullすることもできます

README.mdだけ取得する
# 1.空ディレクトリを作る
$ mkdir repo-name
$ cd repo-name

# 2.空リポジトリを作って初期化
$ git init
Initialized empty Git repository in C:/git/repo-name/.git/

# 3.一部だけ取得できるようにsparsecheckoutを設定する
$ git config core.sparsecheckout true

# 4.取得元のリポジトリを設定する
$ git remote add origin https://github.com/username/repo-name.git

# 5.README.mdをsparse-checkoutに設定する
echo path/to/README.md > .git/info/sparse-checkout

# 6.pullする
$ git pull origin master
From https://github.com/username/repo-name
 * branch            master     -> FETCH_HEAD

# 7. README.mdだけ取得できた
$ find path/
path/
path/to
path/to/README.md

一度設定した後に追加でほかのディレクトリを取得したい場合

sparse-checkoutに改行区切りで追加したいディレクトリを追加できる。

# 追加したいディレクトリを追記する
$ echo add-sub-directory >> .git/info/sparse-checkout

# ツリー情報を更新する
$ git read-tree -m -u HEAD
111
102
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
111
102