0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

私のPython環境構築〜GitHub連携編

Last updated at Posted at 2025-08-05

GitHub連携編では、GitHubリポジトリとの同期について説明します。

前提

  • Apple Silicon Macを使用
  • Homebrewによるパッケージ管理
  • ホームディレクトリ(/Users/xxxxx)から開始
  • GitHubにアカウント作成済み・環境設定済み

GitHub連携の準備

GitHub CLIのインストール

$ brew install gh

GitHub Desktopのインストール

$ brew install --cask github

または、Installing GitHub Desktopよりダウンロードしてインストール。

GitHubでのプロジェクト管理

GitHubでプロジェクトを管理する方法として、以下の3つが考えられる。

  1. GitHubでリポジトリを作成してからローカルにクローンする(おすすめ)
  2. ローカルでリポジトリを作成してからGitHubと同期する
  3. ローカルでプロジェクトを作成してからリポジトリとしてGitHubと同期する

方法1: GitHubでリポジトリを作成してからローカルにクローンする

GitHubでリポジトリtest-projectを作成しておく。

GitHubからリポジトリのクローンをローカルに作成する

GitHub CLIを使う方法

$ gh repo clone xxxxx/test-project
出力
gh repo clone xxxxx/test-project
Cloning into 'test-project'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Receiving objects: 100% (3/3), done.

または、GitHub Desktopでクローンをローカルに作成する(リポジトリが空の場合)。

リポジトリ内で環境構築する

$ cd test-project/
$ uv init
出力
uv init
Initialized project `test-project`
$ uv venv --python 3.13.5
出力
uv venv --python 3.13.5
Using CPython 3.13.5
Creating virtual environment at: .venv
Activate with: source .venv/bin/activate
$ source .venv/bin/activate
$ uv add requests
出力
uv add requests
Resolved 6 packages in 203ms
Prepared 1 package in 82ms
Installed 5 packages in 21ms
 + certifi==2025.8.3
 + charset-normalizer==3.4.2
 + idna==3.10
 + requests==2.32.4
 + urllib3==2.5.0

GitHubと同期する

必要ならば、touchでタイムスタンプを更新する。 既存のファイルの場合、最終アクセス日時と最終更新日時が現在時刻に更新される。ファイルの内容は変更されない。ファイルが存在しない場合は、指定したファイル名で内容が空のファイルが作成される。

$ touch test-file

ファイルをステージングエリアに追加。

$ git add . # すべてのファイル
または
$ git add test-file # 特定のファイルのみ
$ git commit -m "message"
$ git push
出力
git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 284 bytes | 284.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/xxxxx/test-project.git
   0023431..1bac9b8  main -> main

方法2:ローカルでリポジトリを作成してからGitHubと同期する

GitHub CLIでローカルにリポジトリを作成する(例はpublicの場合)。同時にリモートにもリポジトリが作成される。

$ gh repo create test-project --public --clone
出力
gh repo create test-project --public --clone
✓ Created repository xxxxx/test-project on github.com
  https://github.com/xxxxx/test-project

または、GitHub Desktopでリポジトリをローカルに作成し、GitHubと同期する。

以下、方法1の「リポジトリ内でプロジェクトを作成する〜GitHubと同期する」と同様。ただし、最初にgit pushする際、ブランチを指定すること。

$ git push --set-upstream origin master
出力
git push --set-upstream origin master
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 12 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (7/7), 3.37 KiB | 1.69 MiB/s, done.
Total 7 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To https://github.com/xxxxx/new-project.git
 * [new branch]      master -> master
branch 'master' set up to track 'origin/master'.

方法3:ローカルでプロジェクトを作成してからGitHubと同期する

uv initでプロジェクトのリポジトリを作成する。

$ uv init new-project
出力
uv init new-project
Initialized project `new-project` at `/Users/xxxxx/new-project`

リポジトリに移動後、GitHub CLIでリポジトリ名を指定してリモートにリポジトリを作成する(例はprivateの場合)。

$ gh repo create new-project --private --source=. --remote=upstream
出力
gh repo create my-project --private --source=. --remote=upstream
✓ Created repository xxxxx/new-project on github.com
  https://github.com/xxxxx/new-project
✓ Added remote https://github.com/xxxxxx/new-project.git

または、GitHub DesktopでリポジトリをGitHubにアップロードする。

プロジェクトのリポジトリに移動後、方法1の「リポジトリ内で環境構築する(uv venvによる仮想環境の設定以降)〜GitHubと同期する」と同様。ただし、最初にgit pushする際、ブランチを指定すること。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?