GitHubでForkしてPull Request〜Mergeするまでの手順
GitHubを使う事が無かったが、チーム開発する環境への移動に伴い基本知識を学んだ時の学習メモ。
GitHubを利用したことのない人が、Fork〜Pull Request〜Merge までの入門記事です。
■ GitHubへ接続するSSH鍵を生成
# ssh-keygen -t rsa -f ~/.ssh/id_rsa_git -N ''
#chmod 600 ~/.ssh/id_rsa_git
#vi ~/.ssh/config Host github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa_git TCPKeepAlive yes IdentitiesOnly yes
■ GitHubへSSH鍵登録
GitHubへログインし、鍵登録を行う
- アカウントメニュー→Settlersを選択
- プロファイル→SSH and GPG keys→New SSH keyを選択
- Titleに任意の値を設定
- Keyに生成した公開鍵(id_rsa_git.pub)の値をそのまま貼り付け
- 入力完了後、「Add SSH key」をクリックし登録
■ Fork〜Commit〜Pushまでの手順
Gitには①作業ディレクトリ、②ステージングエリア、③Gitディレクトリがあり、編集したファイルをGitディレクトリまで移動する事で、GitHubにプッシュできるようになる。
対象リポジトリをFork(clone)する
$ git clone git@github.com:ogugu/testrepo.git
ユーザー情報設定
$ git config --global user.name "【ユーザー名】" $ git config --global user.email "【メールアドレス】"
ファイル新規作成
$ cd testrepo/ $ vi hello.txt $ git add hello.txt ※ ディレクトリ単位で纏めて $ git add ./ $ git commit -m "first commit" ※addとcommit を纏めて $ git commit -am "first commit" [master (root-commit) ef06ff7] first commit 1 file changed, 1 insertion(+) create mode 100644 hello.txt $ git push Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Writing objects: 100% (3/3), 212 bytes | 212.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To github.com:ogugu/testrepo.git * [new branch] master -> master
ignoreファイルの作成
git管理外のファイルを作成する場合、ignoreを使用
管理外のファイルを指定(ワイルドカード(*)指定可能)
$ vi .gitignore *.log ignore-test.txt
適用範囲について
ディレクトリ単位→作成したディレクトリ以下で有効
GItHub全体→~/.gitignore_global
除外ファイルを作成
$ vi ignore-test.txt
管理外設定をレポジトリへ反映
$ git add .gitignore $ git add ignore-test.txt The following paths are ignored by one of your .gitignore files: ignore-test.txt Use -f if you really want to add them. fatal: no files added $ git commit -m "ignore test" [master 6f78cbf] ignore test 1 file changed, 2 insertions(+) create mode 100644 .gitignore $ git push
Branch戦略に関して
作成された段階のリポジトリには、次のように「master」ブランチのみが存在
$ git branch * master
Gitで開発する際には、直接masterブランチを修正するのではなく、作業用のブランチを作成して、そちらを修正していきます。新たにブランチを作成することを「ブランチを切る」と呼びます。
Gitのブランチの切り方には、git-flow(
https://github.com/nvie/gitflow)
やGitHub Flow(
https://gist.github.com/Gab-km/3705015)
といったワークフローがある。それぞれメリット/デメリットを考慮し選択。
今回はGitHub Flowを採用し、masterブランチと作業ブランチを使うフローを採用。
branch作成
$ git branch develop $ git branch develop * master
branchへ移動
$ git checkout develop Switched to branch 'develop' $ git branch * develop master
新規ファイル作成/Commitまでを実行
$ vi test-pr.txt $ git add ./ $ git commit -a # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # Committer: コミッター名 [develop 39c5671] Committer: pr-user 1 file changed, 1 insertion(+) create mode 100644 test-pr.txt
GItHubへ反映
$ git push origin develop Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 4 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 278 bytes | 278.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) remote: remote: Create a pull request for 'develop' on GitHub by visiting: remote: https://github.com/ogugu/testrepo/pull/new/develop remote: To github.com:ogugu/testrepo.git * [new branch] develop -> develop
■ Pull Reques〜Mergeまでの手順
GitHubサイトから作成したブランチを切り替えることにより、それぞれの状態をブラウザ上で確認可能
Compare & pull request」もしくは「New pull request」をクリックし、Pull Requestを作成
新たにコミットしたdevelopブランチから、masterブランチへ向けてプルリクエストを送るため、「base」(リクエスト受信側)が「master」ブランチ、「compare」(リクエスト送信側)が「develop」ブランチとなります。
プルリクエストのタイトルと内容を記載し、「Create pull request」をクリックし、プルリクエストを作成。
本来、プルリクエストを送る開発者と確認する開発者は別の人ですが、今回は説明の都合上、自分自身でプルリクエストを確認
作成したプルリクエストを開いて各タブで、それぞれ次の内容を確認
- 「Conversation」:プルリクエストの説明
- 「Commits」:コミット内容
- 「Files changed」:ファイルの差分(変更内容)
プルリクエストの終了とマージ
「Merge pull request」をクリックし、プルリクエストを終了させる
developブランチがmasterブランチにマージされた状態になる
作業していたブランチが不要であれば「Delete branch」をクリックし、ブランチを削除
以上で、作業していたdevelopブランチも削除され、修正内容がmasterブランチに反映された状態になる
ローカル環境を最新にする
masterブランチに移動
$ git branch * develop master $ git checkout master Switched to branch 'master' $ git branch develop * master
masterブランチをリモート上の最新状態と同期
$ git pull remote: Enumerating objects: 1, done. remote: Counting objects: 100% (1/1), done. remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (1/1), done. From github.com:ogugu/testrepo 3763d90..2f06402 master -> origin/master Updating 3763d90..2f06402 Fast-forward test-pr.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 test-pr.txt
Pull Requesがのマージによって、作業内容がmasterブランチに反映された状態でローカル環境も同期される
ローカルの作業ブランチも不要のため削除
$ git branch -d develop Deleted branch develop (was 39c5671).
ブランチ削除時のエラーに関して
マージ反映前の状態で削除した場合、-dオプションでは以下のエラーが発生する。ローカルのmasterブランチがマージ前の状態であっても同様。
$ git branch -d develop error: The branch 'develop' is not fully merged. If you are sure you want to delete it, run 'git branch -D develop'.
マージ前のローカルブランチを強制的に削除したい場合は -dオプションではなく、-Dオプションを使います。
$ git branch -D develop
まとめ
Git/GitHubはソースコードだけではなくシステムドキュメント等でも用いる事が可能な為、必須スキルとして学んでおく事は重要だと考えてます。
今回は基礎知識を覚えるまでに留まりましたが、使い倒す事でスキルを向上して行こうと思う。
ref)
https://employment.en-japan.com/engineerhub/entry/2017/01/31/110000#%E3%83%AA%E3%83%9D%E3%82%B8%E3%83%88%E3%83%AA%E3%81%AE%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6
https://keiilog.com/git-pull/
https://blog.qnyp.com/2013/05/28/pull-request-for-github-beginners/