2. Gitコマンドを覚えよう! - ncxx-sl-lab/git-tutorial GitHub Wiki

最低限のGitコマンド

  1. リポジトリをクローン: git clone
  2. 他人の変更を取得: git pull
  3. ファイルを変更/追加: git add
  4. 変更をコミット: git commit
  5. コミットを送信: git push
  6. 2 へ

以下は適宜

  • リポジトリの状態を確認する: git status
  • ブランチを切り替える: git checkout

git clone (最初の 1 回だけ)

(必要なら、いつもの作業場所のひとつ上に移動する)

$ cd ..

中央のリポジトリをクローンしてきます

$ git clone --recursive https://github.com/ncxx-sl-lab/members.git

リポジトリとサブモジュールをコピーするため、多少時間がかかります。

git pull (最新の変更を取得する)

他の人が行った変更を手元にも反映します。 以下の 1 コマンドで OK です。

$ git pull
$ git submodule update --init

手元のファイルがブランチの最新の状態になります。

pull 時のトラブル

error: Your local changes to 'templates/index.html' would be overwritten by merge.  Aborting.
Please, commit your changes or stash them before you can merge.

上記のメッセージが出た場合は、pull で更新されるファイルが手元で修正されていて、まだコミットされていません。
変更をコミットしてからもう一度 git pull しなおして下さい。

Auto-merging templates/index.html
CONFLICT (content): Merge conflict in templates/index.html
Automatic merge failed; fix conflicts and then commit the result.

上記のメッセージが出た場合は、他の人が行った変更と手元でコミットした内容がコンフリクトしています。
量が多すぎる、訳がわからない、修正できるか自信がない場合は、以下のコマンドを実行して中止し、開発者に尋ねてください。

$ git reset --hard

git checkout (ブランチを切り替える/作成する)

同じリポジトリで複数のブランチを並行して開発できるのが git の便利な点です。
常に master ブランチが本番に反映されます。
開発は master 以外のブランチで行い、適宜 master にマージして反映、というのが開発の流れです。
開発中は複数のブランチを渡り歩くことになるかもしれません。

作業ブランチを切り替えたい場合は

$ git checkout branch-name

ブランチ名を指定して git checkout コマンドを呼びます。

新しい機能の開発などで、誰かが push したブランチが手元にまだ出来ていない場合は

$ git checkout -t origin/branch-name

とします。

checkout 直後は submodule の更新が必要な場合があります。

M       modules/Hatena
M       modules/WorkerManager

のように表示されたら、以下のコマンドで submodule を更新してください。

git submodule update --init

git add (コミットするファイルを追加する)

ファイルの変更をコミットするには、一度 git add してコミット可能な状態にする必要があります。

$ git add templates/index.html

ディレクトリ名を指定することで、ディレクトリ以下の全ての変更を Git に通知します。

$ git add static/images

git add は通常何の出力も返さないので、次の項でふれる git status で状態を確認してください。

git status (作業状況を確認する)

現在の作業状況を確認するには、git status というコマンドを使います。
英語のメッセージですが、試しに読んでみてください。一行一行に意味があります。

$ git status

まっさらな状態では、以下のような出力になります。

# On branch master
nothing to commit (working directory clean)

1 行目はいま自分が "master" ブランチにいることを、2 行目はファイルやディレクトリに何の変更も加わっていないことを表しています。

master ブランチで作業中に新しいファイル static/images/hoge.gif をつくったときは、以下のような出力になります。

# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       static/images/hoge.gif

リポジトリに入っておらずバージョン管理されないファイルとして、static/images/hoge.gif がリストされています。
3行目の指示を見れば、これをコミットに含めるには git add すればよいということが分かるようになっています。

すでにリポジトリに含まれているファイルを変更した場合は、以下のような出力になります。

# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   static/images/spacer.gif
#
no changes added to commit (use "git add" and/or "git commit -a")

編集したけれどその変更が Git に伝わっていないファイルとして、static/images/spacer.gif がリストされています。
先ほどと同じ 3 行目の指示に加え 4 行目にも指示がありますがまあ試してみてください(変更が元に戻ります)。
最後の行は、今のままではコミットするものがないので git add してくださいといっています。
(git commit -a は非推奨です。見なかったことにしてください)

git add static/images/spacer.gif などで変更を Git に通知してやると、git status の結果は以下のようになります。

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   static/images/spacer.gif
#

コミットされる予定のファイルがリストされています。

これらの出力は組み合わさって表示されることもあります。

$ git st
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   static/images/spacer.gif
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   static/images/logo_header.gif
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       i18n/a

git commit (コミットする)

git add された変更をまとめてコミットします。
git add されていないファイルはコミットに含まれないので、新しい画像を追加したときなどは注意してください。

$ git commit -m "ここに変更の説明を書く!!"

(もし、-m オプションを付けない場合は)

$ git commit

エディタ(vim)が立ち上がります。適当にコミットメッセージを書いて保存し、エディタを終了させてください。
vimとはキーボードのみで編集できる、高性能なエディタです。
慣れるまでは操作が難しいので、出来るだけ -m をつけてコミットしてください。
vimのコマンドはこちら

[master 7196d3f] 文言修正
 1 files changed, 1 insertions(+), 0 deletions(-)

こんなメッセージが出たらコミット完了。

git push (変更をサーバに送る)

変更を中央のリポジトリに送信します。
push したつもりがエラーが出て push できていないこともあるので、コマンドの結果に注意してください。

$ git push

Counting objects: 24, done.
Compressing objects: 100% (13/13), done.
Writing objects: 100% (13/13), 1.58 KiB, done.
Total 13 (delta 10), reused 0 (delta 0)
To git@repository01:/var/git/projects/Hatena-Project
   1ede18e..c6fa4c4  HEAD -> master

以下のように出たときは push に失敗しています!
他の人が同じブランチに先に push していた場合、この表示になります。
一度 pull してから push しなおしてみて下さい。

$ git push
To git@repository01:/var/git/projects/Hatena-Project
 ! [rejected]        HEAD -> master (non-fast-forward)
error: failed to push some refs to 'git@repository01:/var/git/projects/Hatena-Project'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'Note about
fast-forwards' section of 'git push --help' for details.
⚠️ **GitHub.com Fallback** ⚠️