Featured image of post git clone リポジトリの中だけをcloneしたい

git clone リポジトリの中だけをcloneしたい

こんにちは。

gitでcloneする時に既にあるディレクトリ内にリモートリポジトリの中だけをcloneしたかったのですが、

1
$ git clone [リモートリポジトリ名]

だけだとリポジトリ名のディレクトリも生成されてしまうんですよね。何かやり方があるのか調べました。

対応方法1

1
$ git clone [リモートリポジトリ名] .

リポジトリ名の後にピリオドをつけるという方法です。

git cloneするディレクトリ内は空である必要があるそうで、隠しファイルを含むファイルがある場合は下記エラーが出ます。

1
fatal: destination path '.' already exists and is not an empty directory.

ディレクトリ内のファイルを消したくない場合は次のようにします。

対応方法2

①フォルダをGITリポジトリとして初期化

1
$ git init

②リモートリポジトリを追加

1
$ git remote add origin https://github.com/XXXXX/XXXXX.git

③リモートリポジトリのコンテンツを取得

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ git fetch --all --prune
Fetching origin
remote: Enumerating objects: 465, done.
remote: Counting objects: 100% (465/465), done.
remote: Compressing objects: 100% (386/386), done.
remote: Total 465 (delta 70), reused 465 (delta 70), pack-reused 0
Receiving objects: 100% (465/465), 26.42 MiB | 11.06 MiB/s, done.
Resolving deltas: 100% (70/70), done.
From https://github.com/XXXXX/XXXXX
 * [new branch]      main       -> origin/main

④ローカルブランチを最新化

1
$ git pull origin main
Licensed under CC BY-NC-SA 4.0
Hugo で構築されています。
テーマ StackJimmy によって設計されています。