GitHubでNew repository
リポジトリ名は例として git-practice にします。
WHAT YOU WILL LEARN
練習用ファイルは、このリポジトリの practice/ フォルダに用意してあります。
この練習では、元のリポジトリにはpushしません。
元の接続先は upstream に名前を変え、自分で作った練習用リポジトリを origin に設定します。実務でよく使う構成です。
PREPARE
GitHubで、自分のアカウントに空のリポジトリを1つ作ります。
リポジトリ名は例として git-practice にします。
この練習ではREADME、.gitignore、Licenseを追加せずに作成します。
https://github.com/あなたのユーザー名/git-practice.git を後で使います。
「あなたのユーザー名」は自分のGitHubユーザー名に置き換えてください。
このページのコマンドにそのまま残したまま実行しないようにします。
GET THE PROJECT
まず元の教材リポジトリを自分のパソコンへ取得します。保存先フォルダ名は git-practice にします。
clone はGitHub上のリポジトリを履歴ごとパソコンへ取得します。
git clone https://github.com/designequal/how-to-use-github.git git-practice
cd git-practice
git status
On branch main と表示され、変更がない状態ならOKです。git statusだいたい次のように表示されます。
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean見るところ: mainにいる / origin/mainと同じ / 未保存の変更がない、の3点です。
git log --oneline -3コミットIDやメッセージは時期によって変わります。
a1b2c3d 最新の変更内容
e4f5g6h 1つ前の変更内容
i7j8k9l さらに前の変更内容見るところ: 上にあるものほど新しいコミットです。
practice/index.html:最初の変更用
practice/remote-note.txt:pull確認用
practice/conflict.txt:競合練習用
pwd
git status
git branch --show-currentCONNECT YOUR REPO
clone直後の origin は、この教材のリポジトリを指しています。元リポジトリは upstream に残し、自分の練習Repoを新しい origin にします。
git remote -v
2行目のURLだけ自分のURLに変更して実行します。
git remote rename origin upstream
git remote add origin https://github.com/あなたのユーザー名/git-practice.git
git remote -v
git remote -v
設定できていれば、表示は次のようになります。
origin https://github.com/あなたのユーザー名/git-practice.git (fetch)
origin https://github.com/あなたのユーザー名/git-practice.git (push)
upstream https://github.com/designequal/how-to-use-github.git (fetch)
upstream https://github.com/designequal/how-to-use-github.git (push)
ここを確認: origin が自分、upstream が元教材になっていればOKです。
自分が普段pushする先。今回なら、自分の git-practice リポジトリです。
元になったリポジトリ。教材の最新版を取り込みたいときに使えます。
すでにoriginがあり、URLだけ変えたい場合git remote set-url origin 新しいURL を使えます。後半で練習します。
git push -u origin main-u を付けると、次回以降は git push だけでも送り先を判断しやすくなります。
WORK ON A BRANCH
mainを直接変更せず、練習用のブランチを作って移動します。
git switch -c は、新しいブランチを作ってそのブランチへ移動します。
git switch -c practice/first-change
git branch --show-current
git status
practice/first-change と表示されたら、そのブランチ上で作業しています。git statusOn branch practice/first-change
nothing to commit, working tree cleanまだファイルを触っていないので、変更なしなら正常です。
git log --oneline --decorate -3a1b2c3d (HEAD -> practice/first-change, main, origin/main) 最新の変更
...HEAD -> practice/first-change が「今このbranchにいる」という意味です。
MAKE A CHANGE
practice/index.html をエディタで開き、次の1行を変更します。
<p id="message">ここを書き換えてください</p><p id="message">Gitの最初の変更ができました!</p>git status
git diff
On branch practice/first-change
Changes not staged for commit:
modified: practice/index.html
no changes added to commitmodified が出ていれば、Gitが変更を見つけています。ただし、まだcommit対象には入っていません。
- <p id="message">ここを書き換えてください</p>
+ <p id="message">Gitの最初の変更ができました!</p>- が変更前、+ が変更後です。意図した行だけ変わっているか確認します。
add → 確認 → commit の順です。
git add practice/index.html
git status
git diff --staged
git commit -m "練習ページのメッセージを変更"
git status
git log --oneline --decorate -3
Changes to be committed:
modified: practice/index.htmlChanges to be committed に移動していれば、次のcommitに入る状態です。
On branch practice/first-change
nothing to commit, working tree clean7abc123 (HEAD -> practice/first-change) 練習ページのメッセージを変更
a1b2c3d (origin/main, main) 1つ前の変更新しいcommitが一番上に増え、作業中の変更が0になっていればOKです。
git add . でも追加できますが、練習ではファイル名を指定します。
何をcommitするのか意識しやすく、不要なファイルを誤って含めにくくなります。
SEND IT TO GITHUB
ローカルに作ったコミットを、自分のGitHubリポジトリへ送ります。
送り先は自分で設定した origin です。
git push -u origin practice/first-change
git-practice リポジトリに practice/first-change ブランチが増えていれば成功です。git statusOn branch practice/first-change
Your branch is up to date with 'origin/practice/first-change'.
nothing to commit, working tree cleanup to date ならローカルとGitHub側が同じ状態です。
git branch -vv* practice/first-change 7abc123 [origin/practice/first-change] 練習ページのメッセージを変更
main a1b2c3d [origin/main] ...作業branchが origin/practice/first-change を追跡していればOKです。
REVIEW & MERGE
GitHub上で「作業ブランチの変更をmainへ取り込む」流れを体験します。
GitHubで作業ブランチを選ぶ
base: main / compare: practice/first-change
Files changedで差分を見る
mainへ取り込む
PRは「pushの代わり」ではありません。
まずbranchをpushし、そのGitHub上のbranchからmainへ取り込む依頼を作るのがPRです。
git switch main
git pull origin main
git branch -d practice/first-change
git status
git log --oneline --decorate -5On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree cleanPRをmergeしてpullしたあと、mainとorigin/mainが一致していればOKです。
git log --oneline --decorate -59def456 (HEAD -> main, origin/main) Merge pull request ...
7abc123 練習ページのメッセージを変更
...Merge commitが作られる設定では、このように履歴が増えます。Squash mergeなどでは表示が異なります。
GET REMOTE CHANGES
次はGitHubの画面でファイルを変更し、その変更をパソコンへ取り込みます。
自分の git-practice のmainで practice/remote-note.txt を開き、鉛筆アイコンから文章を変更してcommitします。
ターミナルでmainにいることを確認します。
GitHubにだけ存在する新しいコミットを取得して反映します。
pull後、ファイルを開いてGitHubで変更した文章になっているか確認します。
git switch main
git status
git pull origin main
git status
git log -1 --oneline
GitHubだけを更新した直後は、ローカルはまだ古い状態です。git status だけではリモートの最新情報を取得していない場合があるので、pullして確認します。
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean3fed890 GitHub側でremote-noteを変更git log -1 の先頭がGitHubで作ったcommitになっていれば、pullできています。
CREATE A REAL CONFLICT
同じファイルの同じ行を「ローカル」と「GitHub」で別々に変更し、実際の競合を体験します。
この練習は必ず自分の git-practice リポジトリで行ってください。
会社や本番プロジェクトのmainで、わざと競合を作らないでください。
git switch main
git pull origin main
git statuspractice/conflict.txt の 回答: まだ編集していません を 回答: ローカルで編集しました に変えます。
git add practice/conflict.txt
git commit -m "ローカル側で回答を変更"まだローカルのcommitはpushしません。GitHubのmainで同じ practice/conflict.txt を開き、同じ行を 回答: GitHub側で編集しました に変更してcommitします。
git pull --no-rebase origin main
git status同じ行を別々に変更したため、自動では統合できずCONFLICTになります。
On branch main
You have unmerged paths.
Unmerged paths:
both modified: practice/conflict.txt
no changes added to commit
both modified が、ローカル側とGitHub側の両方で同じファイルを変更して競合している印です。
<<<<<<< HEAD
回答: ローカルで編集しました
=======
回答: GitHub側で編集しました
>>>>>>> origin/main3種類の記号を削除し、最終的に残したい文章だけにします。
回答: 両方の内容を確認して統合しましたadd することで「このファイルの競合は解消した」とGitへ伝えます。
git status
git add practice/conflict.txt
git status
git commit -m "コンフリクトを解消"
git push origin main
git status
git log --oneline --decorate -5
All conflicts fixed but you are still merging.
Changes to be committed:
modified: practice/conflict.txtこの表示になれば、競合箇所の編集は終わり、あとはmerge commitを作る段階です。
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean8abc999 (HEAD -> main, origin/main) コンフリクトを解消
...再びcleanになり、解消commitが履歴の先頭にあれば完了です。
コンフリクトはエラーではなく「Gitだけでは決められない」という状態です。
どちらを残すか、両方を統合するかを人間が判断し、記号を消してからadd・commitします。
REMOTE TOOLBOX
実務でよく使うremote操作も、この練習Repoで確認できます。
git remote -v接続先一覧を見るgit remote get-url originoriginのURLを見るgit remote rename origin upstreamremote名を変更git remote add origin URLremoteを追加git remote set-url origin URLoriginのURLを変更git fetch upstream元Repoの情報を取得URLを間違えた場合や、移行先を変える場合に使います。
git remote set-url origin https://github.com/あなたのユーザー名/新しいリポジトリ名.git
git remote get-url origin
git remote -v
remoteのURL変更は、ファイルやコミット自体を削除しません。
「次にfetch/pull/pushするとき、どのGitHubリポジトリと通信するか」を変える設定です。
git fetch upstream
git switch main
git merge upstream/main
git push origin mainupstream は元Repo、origin は自分のRepo、という使い分けです。
REAL WORKFLOW
ここまでの操作を、毎日の作業順にまとめると次の流れです。
git switch main
git pull origin main
git switch -c feature/sample-change
# ファイルを編集したあと
git status
git diff
git add practice/index.html
git diff --staged
git commit -m "変更内容を書く"
git push -u origin feature/sample-change
git switch main
git pull origin main
git branch -d feature/sample-change
git status
clone → remote → branch → diff → add → commit → push → PR → merge → pull → conflict解消 を実際に触った状態です。暗記よりも、迷ったときに git status と git remote -v を確認する習慣をつける方が大切です。