IT井戸端会議

IT井戸端会議

インフラ、ネットワーク、アプリケーション開発、IT界隈の話等々を東京都千代田区界隈から発信します。

Git チュートリアル

GitHub の準備

GitHub にアクセスし、ログインします。 (ユーザーをお持ちでない方は、ユーザー登録してください。) github.com

■ 右上の「+」をクリックし、「New repository」をクリックし、テスト用のリポジトリ作成画面へ遷移します。

f:id:candapc:20160118015811p:plain

■ 以下のとおり入力後、「Create repository」ボタンをクリックしてリポジトリ作成を完了します。 - Repository name: demo - Public/Private: Public - Initialize this repository with a README: TRUE - Add .gitnore: Ruby - Add lisence: None

f:id:candapc:20160118015825p:plain

■ .gitnore と README.md ファイルがあることを確認します。

■ 右ペイントの [Graphs]-[Network] を選択し、ブランチの状態を確認できます。 (初期では、「master」ブランチが点で表示されています。) f:id:candapc:20160118015857p:plain

ローカル環境の準備

■ 使用するOSに合わせて、git パッケージをインストールします。

■ 以下のコマンドを実行し、git のバージョンが表示されることを確認します。

$ git --version
git version 2.3.2 (Apple Git-55)

■ ローカル環境に git のユーザ情報をセット後、登録情報を確認します。

$ git config --global user.name <GitHubのユーザ名>
$ git config --global user.email <GitHubのメールアドレス>

$ git config --list
user.name=<GitHubのユーザ名>
user.email=<GitHubのメールアドレス>

SSHGitHub に接続するために、公開鍵を作成します。 (今回は、すべて空Enterで完了させます。)

$ ssh-keygen

■ 作成した公開鍵の内容をコピーします。

$ cat ~/.ssh/id_rsa.pub 
<公開鍵の内容>

GitHub 右上のユーザメニューより、[Settings]を選択後、左メニューの [SSH keys] を選択し、「Add SSH key」ボタンより、先ほどコピーした公開鍵の内容を貼り付けます。 - Title: デモ用公開鍵 - Key: <公開鍵の内容>

f:id:candapc:20160118020007p:plain

git の基本操作

■ 再び GitHub の demo リポジトリのトップページへ移動し、右ペインの Clone URLで「SSH」のリンクを押して、表示される「git@github.com:<ユーザー名>/demo.git」をコピーします。

■ ローカル環境にて、任意のフォルダへ移動し、demo リポジトリのクローンを作成します。 (初めてアクセスする場合、SSH接続確認が行われますので、「yes」を入力してください。)

$ git clone <Clone URL>
Cloning into 'demo'...
The authenticity of host 'github.com (192.30.252.129)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.252.129' (RSA) to the list of known hosts.
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (4/4), done.
Checking connectivity... done.

■ demo フォルダが作成されており、その中に以下のファイルがあることを確認します。 - .git #ディレクトリ - .gitignore #ファイル - README.md #ファイル

$ ls -ld demo
drwxr-xr-x  5 otsuka  staff  170  6 26 03:43 demo

$ cd demo

$ ls -al
total 16
drwxr-xr-x   5 otsuka  staff   170  6 26 03:43 .
drwxr-xr-x+ 39 otsuka  staff  1326  6 26 03:42 ..
drwxr-xr-x  13 otsuka  staff   442  6 26 03:43 .git
-rw-r--r--   1 otsuka  staff   588  6 26 03:43 .gitignore
-rw-r--r--   1 otsuka  staff     7  6 26 03:43 README.md

■ origin に正しい Clone URL が設定されていることを確認します。

$ git remote -v
origin  git@github.com:<GitHubユーザー名>/demo.git (fetch)
origin  git@github.com:<GitHubユーザー名>/demo.git (push)

ブランチの状態を確認し、「 master」のみ表示されていることを確認します。 (アスタリスク()が現在選択しているブランチとなります。)

$ git branch
* master

■ branch-demo ブランチを作成し、作成したブランチへ移動します。

$ git bracnh branch-demo    #ブランチ作成
$ git checkout branch-demo     #ブランチ移動
Switched to branch 'branch-demo'

■ branch-demo ブランチが作成され、アスタリスク(*)が付いていることを確認します。

$ git branch
* branch-demo
  master

■ master ブランチへ移動後、branch-demo ブランチを削除します。

$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

$ git branch
  branch-demo
* master

$ git branch -D branch-demo
Deleted branch branch-demo (was 92882ea).

$ git branch
* master

■ branch-demo2 ブランチを作成し、作成したブランチへ移動します。

$ git checkout -b branch-demo2    #ブランチ作成&移動
Switched to a new branch 'branch-demo2'

$ git branch
* branch-demo2
  master

■ hello.rb を以下のとおり、作成します。

$ vi hello.rb 
puts 'Hello World!'
$ ls -l
total 16
-rw-r--r--  1 otsuka  staff   7  6 26 03:43 README.md
-rw-r--r--  1 otsuka  staff  20  6 26 04:08 hello.rb

■ 変更状況を確認し、「hello.rb」」のみ赤字で表示されていることを確認します。

$ git status
On branch branch-demo2
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    hello.rb    #赤字で表示

nothing added to commit but untracked files present (use "git add" to track)

■ 「hello.rb」をコミットの対象として指定し、赤字の表示がなくなったことを確認します。

$ git add hello.rb

$ git status
On branch branch-demo2
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   hello.rb

■ 変更内容をコミットします。

$ git commit -m"はじめてのコミット"
[branch-demo2 c0183cf] はじめてのコミット
 1 file changed, 1 insertion(+)
 create mode 100644 hello.rb

■ git のログで先ほどのコミットが適用されていることを確認します。

$ git log
commit c0183cff9ca931d3c0c4780780756cbdf2110c12
Author: falcon39 <jundo414@gmail.com>
Date:   Fri Jun 26 04:18:27 2015 +0900

    はじめてのコミット

commit 92882ea563d5e732d6b81feb3fe3558521514d56
Author: falcon39 <otsuka@ap-com.co.jp>
Date:   Fri Jun 26 03:06:16 2015 +0900

    Initial commit

■ 先ほどコミットした内容を origin の branch-demo2 ブランチへ反映させます。

$ git push origin branch-demo2
Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 356 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:falcon39/demo.git
 * [new branch]      branch-demo2 -> branch-demo2

GitHub の demo リポジトリへアクセスし、「Compare & pull request」ボタンが表示されていることを確認し、クリックします。

■ コメント欄へ任意の文字列を入力後、「Create pull request」ボタンをクリックします。 スクリーンショット 2015-06-26 4.31.15

■ [Commits] や [Files changed] タブをクリックして、コミットの状況やソースコードの差分を確認してみましょう。

■ [Conversaition] タブへ戻り、レビューアがソースコードレビューを完了した想定で、[Merge pull request]-[Confirm merge] をクリックします。

■ 「Pull request successfully merged and closed」と表示されたことを確認します。

■ [Graphs]-[Network] よりブランチの状態を表示し、以下の通り、branch-demo2 ブランチが master ブランチへマージされたことを確認します。 f:id:candapc:20160118020319p:plain

GitHub の demo ブランチトップページへ移動し、「hello.rb」が表示されていることを確認します。

■ 以上で終了となります。 その他コマンド類は、以下の記事をご参照ください。 asobo.hatenablog.jp