0
1

More than 1 year has passed since last update.

既存ファイルを新しいGithubにあげる方法

Posted at

目次

1. 背景
2. Githubでのレポジトリ(リモートレポジトリ)の作成
3. ローカルレポジトリの操作
4. 結果

1. 背景

筆者はgitに慣れておらず、既存のローカルファイルを新しくgithubにアップロードし管理しようとした際に苦戦したので、解決方法、ロードマップをまとめる。

2. Githubでのレポジトリの作成

まず、Githubにて今回push(アップロード)するためのレポジトリを作成する。
以下の画面からレポジトリを作成する。
Screen Shot 2022-06-01 at 16.37.27.png

  • Repository name: レポジトリ名を入力
  • Description: レポジトリの概要説明(option)
  • PUblic/Private: このレポジトリを公開するかどうか
  • Add a README file: README.mdをあらかじめレポジトリに含めるかどうか(チェックしない)
  • Add .gitignore: .gitignoreをあらかじめレポジトリに含めるかどうか(None)
  • Choose a license: licenseの設定(None)

ここで注意が必要なのがAdd a README fileAdd .gitignoreと**Choose a licenseの三つ。
この三つが選択され、あらかじめリポジトリ内に含まれていると、ローカルからpushした際(後述)に以下のようになってしまい、Github(リモートレポジトリ)に反映できない。
image.png
筆者はここに引っかかって苦労した。
ここまで間違いなく選択しレポジトリを作成できれば以下の通りの画面に遷移する。
Screen Shot 2022-06-01 at 16.52.21.png

! README, gitignore, licenseに関しては後からでも追加できるため問題ない。

3. ローカルレポジトリの操作

ローカルでの操作は序盤のgitのローカルレポジトリ化以降は基本的に上の画像での
...or push an existing repository from the command line の通りである。
今回筆者は以下のローカルファイルをGithubにあげるものとする。

~/ $ cd test && ls

# output
hello.py  hello.txt

~/test/ $ git init

# outout
Initialized empty Git repository in /Users/<Username>/test/.git/

~/test/[master] $ git add .
~/test/[master+] $ git commit -m "init commit"
~/test/[master] $ git remote add origin https://github.com/ryuichi-hirano/test.git
~/test/[master] $ git branch -M main
~/test/[main] $ git push -u origin main

# output
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 290 bytes | 290.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/ryuichi-hirano/test.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

4. 結果

Screen Shot 2022-06-01 at 17.06.53.png

以上で、上の画像のようにGithub(リモートレポジトリ)に上げ、つなげることができる。
README、gitignore、licenseなどが含まれてしまうとローカルリポジトリとGithub(リモートレポジトリ)の初期段階での構成が違うということで先述の通り There isn't anything to compareと表示されてしまうようだった。

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