Gitのコマンドを使ってみる(その1)

Gitクライアントが重くて仕方がないので、そろそろちゃんと?コマンドを使ってGitの操作をしたいと思う。
まずはローカルのみのリポジトリで練習。

リポジトリの作成(init)

リポジトリを作りたいフォルダでコマンドを実行する。

$ git init
Initialized empty Git repository in xxx

すると .git フォルダが出来てリポジトリの初期化が行われる。

インデックスに追加(add)

ファイルを追加したり修正したりした場合、それをインデックスに追加する。
何かファイル(今回は空のREADME.mdファイル)を作成し、以下のコマンドを実行する。

$ git add .

ブランチにコミット(commit)

インデックスにあるファイルをブランチにコミットする。

$ git commit -m "first commit."
[master (root-commit) 70821ef] first commit.
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md

リポジトリの確認(status)

何をするにも確認がだいじ。

$ git status
On branch master
nothing to commit, working tree clean

ブランチの作成(branch)

masterブランチだけで運用するのもアレなので、git-flowを想定して「master」「release」「develop」を作る。
initでリポジトリを作成すると「master」が出来るので、残りの「release」と「develop」を作成する。

branchコマンドは現在のブランチを基に分岐するので、いまどのブランチにいるかを把握する必要がある。
どのブランチにいるかは、先のstatusコマンドで調べられる。
On branch masterと言っていたので、現在は「master」ブランチにいることが分かる。

$ git branch release
$ git branch develop

ローカルブランチの確認(branch -l)

ローカルにあるブランチの一覧を表示する。

$ git branch -l
  develop
* master
  release

現在「master」にいて、先ほど作った「develop」と「release」ブランチがあることが分かる。

リモートも含むブランチの確認(branch -a)

リモートも含むブランチの一覧を表示する。
今回作ったものではない、リモートブランチがある別のリポジトリで試すと、以下のようになる。

$ git branch -a
  develop
  feature/xxx
* master
  release
  remotes/origin/HEAD -> origin/master
  remotes/origin/develop
  remotes/origin/feature/xxx
  remotes/origin/master
  remotes/origin/release

「remotes」となっているもの以下がリモートブランチ。

ブランチの移動(checkout)

他のブランチに移動する。

$ git checkout develop
Switched to branch 'develop'

$ git status
On branch develop
nothing to commit, working tree clean

ブランチを作って同時に移動する場合は -b オプションを付けて実行する。

$ git checkout -b feature/some_develop
Switched to a new branch 'feature/some_develop'

$ git status
On branch feature/some_develop
nothing to commit, working tree clean

次回予告

リモートも絡めないと使い物にならないので、そのあたりを実施しようそうしよう。