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?

GitHubでブランチを切ってリポジトリをクローンする

0
Posted at

はじめに

チーム開発でmasterブランチを直接触らないために、作業用ブランチを切る手順を残します。

前提条件

  • GitHubアカウントを持っている
  • 対象のリポジトリへのアクセス権限がある
  • ローカル環境にGitがインストールされている

ブランチを切る方法

方法1: GitHub Web UIでブランチを作成してからクローン

1. GitHubのリポジトリページにアクセス

リポジトリのトップページで「main」または「master」と書かれたブランチ切り替えボタンをクリックします。

Switch branches/tags
* master
* develop
* feature/xxx
...

2. 新しいブランチ名を入力

テキストボックスに新しいブランチ名を入力

ブランチ命名規則の例:

  • feature/機能名 - 新機能開発用
  • fix/修正内容 - バグ修正用
  • setup/セットアップ内容 - 環境構築用

例:

feature/dev-setup
setup/local-environment
feature/new-feature

3. ブランチを作成

「Create branch: feature/dev-setup from master」という選択肢が表示されるので、クリック

4. 作成したブランチをクローン

ターミナルで以下のコマンドを実行します。

# 特定のブランチを指定してクローン
git clone -b ブランチ名 リポジトリのURL

例:

# HTTPSの場合
git clone -b feature/dev-setup https://github.com/ユーザー名/リポジトリ名.git

# SSHの場合
git clone -b feature/dev-setup git@github.com:ユーザー名/リポジトリ名.git

方法2: ローカルでクローン後にブランチを作成

1. リポジトリをクローン

# HTTPSの場合
git clone https://github.com/ユーザー名/リポジトリ名.git

# SSHの場合
git clone git@github.com:ユーザー名/リポジトリ名.git

2. リポジトリディレクトリに移動

cd リポジトリ名

3. 現在のブランチを確認

git branch
# * master  ← 現在masterブランチにいることを確認

4. 新しいブランチを作成して切り替え

git checkout -b feature/dev-setup

5. ブランチが切り替わったことを確認

git branch
# * feature/dev-setup  ← アスタリスクが現在のブランチを示す
#   master

6. リモートにプッシュ(初回のみ)

git push -u origin feature/dev-setup

SSH接続ができない場合

GitHubでSSHキーが設定されていない場合、以下のメッセージが表示されることがある

You don't have any public SSH keys in your GitHub account.

対処法1: HTTPSでクローンする(簡単)

git clone https://github.com/ユーザー名/リポジトリ名.git

Personal Access Token(PAT)による認証が必要になります。

対処法2: SSHキーを設定する(推奨)

SSHキーの生成

# SSHキーを生成
ssh-keygen -t ed25519 -C "your_email@example.com"

# SSHエージェントを起動
eval "$(ssh-agent -s)"

# SSHキーをエージェントに追加
ssh-add ~/.ssh/id_ed25519

# 公開鍵を表示(これをGitHubに登録)
cat ~/.ssh/id_ed25519.pub

GitHubへの登録

  1. GitHub の Settings → SSH and GPG keys にアクセス
  2. "New SSH key" をクリック
  3. 表示された公開鍵を貼り付けて保存

よく使うGitコマンド

# 現在のブランチを確認
git branch

# すべてのブランチ(リモート含む)を確認
git branch -a

# ブランチを切り替え
git checkout ブランチ名

# 新しいブランチを作成して切り替え
git checkout -b 新しいブランチ名

# 現在の状態を確認
git status

# リモートブランチの最新情報を取得
git fetch

# 変更をコミット
git add .
git commit -m "コミットメッセージ"

# リモートにプッシュ
git push origin ブランチ名

ベストプラクティス

1. masterブランチは直接触らない

作業は必ず作業用ブランチで行い、masterブランチへは Pull Request (PR) を通じてマージ

2. わかりやすいブランチ名をつける

# 良い例
feature/user-authentication
fix/login-bug
setup/docker-environment

# 悪い例
test
new
branch1

3. こまめにコミット・プッシュする

作業内容が失われないよう、定期的にコミットとプッシュを行いましょう。

git add .
git commit -m "〇〇機能を実装"
git push origin feature/dev-setup

トラブルシューティング

ブランチが切り替わらない

# 変更をコミットまたは退避してからブランチを切り替える
git stash  # 変更を一時退避
git checkout 別のブランチ名
git stash pop  # 変更を復元

リモートブランチが見つからない

# リモートの最新情報を取得
git fetch origin

# リモートブランチ一覧を確認
git branch -r

プッシュ時にエラーが出る

# リモートの変更を先に取り込む
git pull origin feature/dev-setup

# コンフリクトを解決後、再度プッシュ
git push origin feature/dev-setup

まとめ

  • ブランチを切ることで、masterブランチを保護しつつ安全に開発できる
  • GitHub Web UIでブランチを作成する方法が最も簡単
  • ローカルでブランチを切る場合は git checkout -b を使う
  • わかりやすい命名規則でブランチ名をつけることが重要
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?