量産メモ帳

忘れっぽいのでメモを残しています。浅く適当に書きます。

Git でファイル・ディレクトリをバージョン管理する。

スポンサーリンク

前提条件として、以下の様な状態にあるとする。

  • C:\workspace ディレクトリ配下にバージョン管理したいファイル・ディレクトリ群がある。
  • C:\workspace ディレクトリ直下に Git のローカルリポジトリ(.git)がある。
  • GitHub アカウントにリモートリポジトリを作成済みである。
  • ローカルリポジトリはリモートリポジトリからクローンしたものである。
  • エミュレーターとして MSysGit を利用する。

なお、文章を短くするため、ファイル・ディレクトリをリソースと呼ぶことにする。


まず、MSysGit で C:\workspace ディレクトリに移動して、"git status"コマンドを実行する。
すると、以下のようなメッセージが表示されると思う。


$ git status
# On branch master
# Untracked files:
# (use "git add ..." to include in what will be committed)
# (リソースの名前)
# :

git のローカルリポジトリで新規のリソースをバージョン管理する場合、以下のように"git add"と"git commit"という2つのコマンドを実行しなければならない。


$ git add (リソースの名前)
$
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
#
# new file: (ファイルの名前)
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
# (リソースの名前)
$
$ git commit -m "(コミットコメント)"
[master (中略)] (コミットコメント)
warning: LF will be replaced by CRLF in (ファイルの名前).
The file will have its original line endings in your working directory.
(中略) files changed, (中略) insertions(+)
create mode (中略) (リソースの名前)
$
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
# (use "git push" to publish your local commits)
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
# (リソースの名前)
"git add"や"git commit"コマンドの前後に"git status"コマンドを実行すれば、状態がどのように遷移したのかが分かる。


"git add"コマンドの引数にはコミット予定のリソースを指定する。
ワイルドカード(*)を使った場合、一致するリソースが全てコミット予定の状態になる。
ディレクトリを指定した場合、その配下にあるリソース全てが同様にコミット予定の状態になる。
ただし、いずれも .gitignore に記された名前に一致するものがある場合、それらはコミットされない。(そもそも "git status"コマンドの結果に表示されない。)


一通り"git add"コマンドを実行し終わった後、"git commit"コマンドを実行すると、コミット予定状態のリソースが全てローカルリポジトリにコミットされる。
コミットコメントの指定の仕方は以下の様に二通りある。

  • git commit -m "(コミットコメント)"
  • git commit

前者は -m オプションに指定した文字列がそのままコミットコメントになる。
一方、後者を実行すると、エディタが表示される。
エディタはおそらく vim なので、vim を操作するように編集・保存すれば、それがコミットコメントになる。


なお、"git commit -a"という"git add"と"git commit"をまとめて実行してしまうようなコマンドもあるが、これは新規ファイルには使えないようだ。


ローカルリポジトリへのコミットが終わったら、後は"git push"コマンドを実行すれば、ローカルリポジトリ→リモートリポジトリへリソースが登録される。


$ git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Enter passphrase for key '(中略)':
Counting objects: (中略), done.
Delta compression using up to (中略) threads.
Compressing objects: 100% (中略), done.
Writing objects: 100% (中略), (中略) bytes, done.
Total (中略), reused (中略)
To ssh://git@ssh.github.com:443/(中略).git
(中略) master -> master

"Enter passphrase for key"というメッセージが表示されたら、SSH 鍵のパスフレーズを入力する。


プッシュが正常終了すれば、以下の Git リモートリポジトリのページにも登録したリソース名が表示されるはず。



参考資料: