0
0

More than 3 years have passed since last update.

既に存在するリポジトリのファイルをチェックアウトしないでディレクトリを追加する方法

Last updated at Posted at 2020-12-30

やりたいこと

GitHubには前にJava用に作ったリポジトリがあります。
そんな時、EclipseのワークスペースでJavaのプロジェクトを新規に作りました。
そこで思った、「GitHubにあるリポジトリにこのディレクトリを追加しよっと」と。
なぜなら、別にリポジトリを作ってもすぐ忘れちゃうからまとめておきたい。
でもEclipseのワークスペース内にあるリポジトリに既にあるディレクトリをローカルにチェックアウトしたくない!
そんな時のこと・・・。

やりたいこと
((ローカルにあるEclipseのワークスペース))
Eclipse_2020-12.app/Contents/workspace
└ tryJSF -------┐
                |
((既存のリポジトリ)) |
tryJava        追加
├ tryJava       |
└ ★★tryJSF★★ <--┘

既存リポジトリのファイルをチェックアウトしないでディレクトリを追加する方法

  • 環境
    • macOS Big Sur バージョン11.1
    • git version 2.28.0

1. GitHub上で空ディレクトリを追加する

参考 : GitHub 上でサクッと空のディレクトリを作成する方法 - Qiita

  1. ブラウザでリポジトリを表示
  2. [Add file] > [Create new file]
  3. 「tryJSF(追加したいディレクトリの名前)」 > 「/」 > 「.gitkeep」
  4. コミットコメントを入力(ブランチを選びたかったら指定) > [Commit new file]

2. 追加したいディレクトリの1つ上位ディレクトリに移動する

# Eclipseのワークスペースにある
% cd /Applications/Eclipse_2020-12.app/Contents/workspace
# tryJSFディレクトリを追加したい
% ls -la
total 0
drwxr-xr-x   5 ponsuke  admin  160 12 29 14:37 .
drwxr-xr-x@  9 ponsuke  admin  288 11 16 23:23 ..
drwxr-xr-x  18 ponsuke  admin  576 12 28 22:49 .metadata
drwxr-xr-x  10 ponsuke  admin  320 12 29 10:15 tryJSF

3. リポジトリを設定する

# 空リポジトリを初期化する
% git init
Initialized empty Git repository in /Applications/Eclipse_2020-12.app/Contents/workspace/.git/

# リポジトリを設定する(2段階認証していなければ「{ユーザ名}:{パーソナルアクセストークン}@」は不要)
% git remote add origin https://{ユーザ名}:{パーソナルアクセストークン}@github.com/username/tryJava.git

# ユーザー名を設定する
% git config user.name "{ユーザ名}"

# メールアドレスを設定する
% git config user.email "{メールアドレス}"

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

# 追加したいディレクトリを設定する
% echo tryJSF > .git/info/sparse-checkout

4. 既存リポジトリをローカルで作ったリポジトリとマージする

既存リポジトリをプルして、ローカルで作ったリポジトリとマージします。
スパースチェックアウトを設定しているので、既存リポジトリのファイルやディレクトリはローカルに追加されません。

# 既存リポジトリをプルしてローカルで作ったリポジトリとマージする
% git pull origin master --allow-unrelated-histories
From https://github.com/username/tryJava
 * branch            master     -> FETCH_HEAD
hint: Waiting for your editor to close the file...

#- viでマージのメッセージを求められるので設定して保存(:wq)する ----------------------------
Merge branch 'master' of https://github.com/username/tryJava into master
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.

#-----------------------------------------------------------------------------------------------

Merge made by the 'recursive' strategy.
# ...省略...

# マージをプッシュする
% git push origin master
Enumerating objects: 40, done.
Counting objects: 100% (40/40), done.
Delta compression using up to 8 threads
Compressing objects: 100% (25/25), done.
Writing objects: 100% (38/38), 6.10 KiB | 2.03 MiB/s, done.
Total 38 (delta 1), reused 6 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/username/tryJava.git
   981a2ba..99d4561  master -> master

5. 追加する内容をプッシュする

# 変更を確認して
% git status
...省略...
Untracked files:
  (use "git add <file>..." to include in what will be committed)
    .metadata/
    tryJSF/

nothing added to commit but untracked files present (use "git add" to track)

# .gitignoreや.gitkeepで必要なものといらないものを設定する
% vi .gitignore

# 追加するものをaddして
% git add .

# コミットして
% git commit -m 'JSF実験用のプロジェクトディレクトリを追加した'
[master 9108a40] JSF実験用のプロジェクトディレクトリを追加した
 11 files changed, 82 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 tryJSF/WebContent/META-INF/MANIFEST.MF
#...省略...

# ブランチを指定してプッシュする
% git push origin master
Enumerating objects: 16, done.
Counting objects: 100% (16/16), done.
Delta compression using up to 8 threads
Compressing objects: 100% (12/12), done.
Writing objects: 100% (14/14), 2.54 KiB | 2.54 MiB/s, done.
Total 14 (delta 0), reused 3 (delta 0), pack-reused 0
To https://github.com/username/tryJava.git
   99d4561..9108a40  master -> master

ブラウザを使えばもっと簡単にできる?

全部終わって気がついた、こんな手間をかける必要なくない?

  1. ブラウザでGitHubのリポジトリを表示する
  2. 空ディレクトリを追加する
  3. 空ディレクトリ配下を表示 > [Add file] > [Upload files] > [choose your files]
  4. ファイルをアップロードする
  5. ローカルで対象のディレクトリだけセパレートチェックアウトする

こっちの方が早い気がする・・・やってないけど
スクリーンショット 2020-12-30 12.16.07.png

0
0
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
0
0