101
139

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 使い方基本

Last updated at Posted at 2025-09-02

Git

準備

Git install

まずは既に git が入っているか確認する。

git --version

入っていたら version が表示されるので install する必要はない。
なければ ここ から入手する。

install 出来たか確認する。

git --version

バージョンが表示されたはず。

念のため path が通っているか確認してもいい

参考: Git のインストール 〜Git をMacにインストールしよう〜

Git に自分の情報を登録する

git config --global user.name YOUR_USER_NAME
git config --global user.email YOUR_EMAIL@example.com

SSH 接続のための 公開鍵/秘密鍵 を作成する

ssh-keygen -t ed25519 -C "YOUR_EMAIL@example.com"

うろ覚え: 途中でどこに保存するか聞かれて ~/.ssh を指定する気がする

~/.ssh 以下に id_ed25519 (秘密鍵) と id_ed25519.pub (公開鍵) が出来ている。

確認

ls ~/.ssh

公開鍵を GitHub に登録する

cat ~/.ssh/id_ed25519.pub

で terminal に表示されるので copy しておく。
VSCode で開いてコピっても可。

ブラウザで GitHub に行って以下をおこなう

  1. click your icon and select Settings
  2. click the SSH&GPG keys icon on the left side bar
  3. click on the NEW SSH key button
  4. copy and paste the contents of your public key on the field named Key
  5. click on the Add key button

(必要であれば改行コード設定)

git config --global core.autocrlf input

How to use git commands

まずは repository をローカルに取ってくる (clone する)

tips: 扱う repository が増えると messy になるので repo folder を作っておいて、その中に置くといいかも。

  • Repository の page の右上にある Clone というボタンを押して、Clone with SSH を copy する
  • terminal で repo directory に移動して clone する
git clone git@......
  • clone した repository の folder ができているので移動する

よくある実際の作業手順

branch の確認

git branch

master または main って表示されるはず

branch を切って自分の作業をする

git chechout -b my_branch

git branch すると、新しい branch が出来ていて、そこに移動しているのが分かる (先頭に * が付いていて緑になっているのが今いる branch)

~ 作業後 ~

git status

で、どのファイルが modify されたか分かる。

staging する

git add .    # 全て add
git add FILE_NAME    # 上げたいのだけ上げる

ちゃんと staging 出来たか git status して確認する余分なものが入っていたら git reset <filename> で取り消せる
(余計なファイルを add しちゃったとき: git add を取り消す)

commit を作成する

git commit -m "どういう変更をしたとかのメッセージを書く"

remote に上げる (push する)

git push -u origin my_branch

これで GitHub 上に反映される

他人が作った branch を取り込む

リモート追跡ブランチを最新の状態にする

git fetch

ローカルブランチとリモートブランチすべてを表示する

git branch -a

他人が作った branch を local に持ってくる

git checkout -b other_member_branch origin/other_member_branch

参考: 他メンバーが開発中のリモートブランチをローカルに持ってくる方法

その他のよく使う command

branch を移動する
git checkout 行き先の branch 名

移動後は念のため git branch して移動できてるか確認する

branch の更新を取り込む

目的の branch に移動後、

git pull

tips: conflict する可能性を減らすため、作業前には git pull しておきたい

他の人の Pull Request が main に merge された際に自分の branch にも反映する

通常の流れ

# main を最新にして
git checkout main
git pull origin main

# 作業ブランチに移動して
git checkout my_branch

# main をマージ
git merge main

作業ブランチでまだコミットしていない変更があるけど main を取り込みたい

方法1: 一旦 commit してしまってから上記 通常の流れ を行う

方法2: stash で変更を一時退避する

# まだコミットしていない変更を stash に退避
git stash

# main を更新して merge
git checkout main
git pull origin main
git checkout my_branch
git merge main

# 退避していた変更を戻す
git stash pop

git stash pop について補足

  • デフォルトだと stash@{0} (最新) が戻る
  • 適用に成功したら stash@{0} はリストから消える
  • もし競合で失敗しても stash は残る (安全設計)

注意: stash pop したときに、merge 後のコードと自分の変更がぶつかるとコンフリクトする

stash にコメントをつけることも出来る

git stash push -m "stash comment"
stash についてもう少し

git stash は複数回使えるのでインデックスで管理出来る

stash の確認

git stash list

出力例: stash@{0} が最新の stash で、番号が大きいほど古い

stash@{0}: WIP on my_branch: 123abc4 add login form
stash@{1}: WIP on my_branch: 456def7 fix typo
stash@{2}: On main: 789ghij quick debug

インデックスを指定して取り出す

# 特定の stash を適用する (残す)
git stash apply stash@{2}

# 特定の stash を適用して消す
git stash pop stash@{1}

stash を消す

# 特定の stash を消す
git stash drop stash@{0}

# 全ての stash を消す
git stash clear

たまに使うもの

サブモジュールをダウンロードする

git submodule init
git submodule update
101
139
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
101
139

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?