Gitコマンド
Git初期設定 [git config]
// グローバル設定
$ git config --global user.name "<ユーザー名>"
$ git config --global user.email "<メールアドレス>"
// 個別リポジトリ設定
$ cd <対象リポジトリパス>
$ git config user.name "<ユーザー名>"
$ git config user.email "<メールアドレス>"
Gitリポジトリを作成する [git init]
// 対象のディレクトリに移動
$ cd <対象ディレクトリパス>
// .gitリポジトリを作成する
$ git init
GitHubプロジェクトをクローンする [git clone]
// クローン(コピー)を作成する
$ git clone <リポジトリ名>
// 例
$ git clone https://github.com/atom/atom.git
変更をステージに追加する [git add]
// ステージに追加する
$ git add <ファイル名>
$ git add <ディレクトリ名>
# 変更・新規ファイルをステージに追加(削除ファイルは反映されない)
$ git add .
# 変更・削除・新規すべてのファイルをステージに追加
$ git add -A
# 変更・削除されたファイルをステージに追加(新規ファイルは対象外)
$ git add -u
変更をコミットする [git commit]
// コミットする(メッセージを入力するエディタが開く)
$ git commit
// メッセージ付きでコミットする
$ git commit -m "<メッセージ>"
// 変更内容を確認してコミットする
$ git commit -v
// 例
$ git commit
// エディタが開いたら1行目に「initial commit」と入力後、
// 保存してエディタを閉じる
// ※コミットしない場合は、変更せずにエディタを閉じる。
現在の変更状況を確認する [git status]
// 変更されたファイルを確認する
// ワークツリー⇔ステージ間、ステージ⇔リポジトリ間
$ git status
// 例
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
Git利用具体例
GitHubからクローンして開発する
// クローン(コピー)を作成する
$ git clone <リポジトリURL>
$ cd <リポジトリ名>
// 最新の状態を取得(他の人の変更を取り込む)
$ git pull
// ソースコードを編集する
# 変更・削除・新規すべてのファイルをステージに追加
$ git add -A
// コミットする(メッセージを入力するエディタが開く)
$ git commit
// ※コミットしない場合は、変更せずにエディタを閉じる。
// 変更をリモートリポジトリに反映(アップロード)する
$ git push