■ 基本コマンド
git init
* リポジトリを新規作成するときに使うコマンド構文
git init ([ディレクトリ] [オプション])参考文献
https://eng-entrance.com/git-init
git add
* ステージング・エリアに変更済みファイルを追加構文
git add [ファイル]例
git add hello.txt # 現在のディレクトリ配下のもの全て追加 git add .
git commit
* コミット構文
git commit ([オプション])例
git commit # コメント文「This is a Comment...」を追加 git commit -m "This is a Comment..." # 直前のコミットを変更する git commit --amend
git log
* コミット履歴の閲覧構文
git log ([オプション])例
# 一行表示 git log --oneline # 差分表示 - 変更箇所 git log -p # 差分表示 - どのファイルに何か所変更があったか git log --stat参考文献
https://git-scm.com/book/ja/v1/Git-%E3%81%AE%E5%9F%BA%E6%9C%AC-%E3%82%B3%E3%83%9F%E3%83%83%E3%83%88%E5%B1%A5%E6%AD%B4%E3%81%AE%E9%96%B2%E8%A6%A7
git status
* 変更の状態を確認する構文
git status [オプション]
git diff
* ステージング・エリアにないファイルの変更の状態を確認する例
git diff # 次の commit で反映される変更を表示 git diff --cached参考文献
http://www-creators.com/archives/755
git checkout
* 変更を戻す構文
# 変更を戻す git checkout -- [ファイル]参考文献
https://gist.github.com/satoshin2071/7cc2a8e2135d02f36649
git reset
* 過去の状態に戻す構文
git reset ([オプション]) git reset --hard [ID/HEAD/HEAD^]例
git reset # 直前のコミットに戻す git reset --hard HEAD # 2つ前のコミットに戻す git reset --hard HEAD^ # git resetしたあとにそれを取り消す git reset --hard ORIG_HEAD # ID「9402b408ad7f5315a9757241fec5c7ebb2afb7c4」に戻す(git logで調べられる) git reset --hard 9402b408ad7f5315a9757241fec5c7ebb2afb7c4
■ サンプル
* Hello World的なこととして、ファイルをgitでバージョン管理してみる
準備
mkdir my-web cd my-web vi index.html ~~~~~~~ Hello World ~~~~~~~
コマンド例
# 現在のディレクトリ(今回は「my-web」)をgitで使うためのコマンド git init => 「Initialized empty Git repository in /home/【ユーザ名】/my-web/.git/」 # 「作業ディレクトリ」→「ステージング・エリア(インデックス)」に遷移するためのコマンド git add index.html # 「ステージング・エリア(インデックス)」→「レポジトリ(Gitディレクトリ)」に遷移するためのコマンド git commit # 確認 git log vi index.html ~~~~~~~ Hello World!! ~~~~~~~ # 変更確認 git status On branch master Changes not staged for commit: << ステージにもなくコミットもしてない状態 (use "git add <file>..." to update what will be committed) << 次に行うコマンドを教えてくれる (use "git checkout -- <file>..." to discard changes in working directory) modified: index.html no changes added to commit (use "git add" and/or "git commit -a") # 元に戻す git checkout -- index.html # 再度、変更確認 git status On branch master nothing to commit, working tree clean vi index.html ~~~~~~~ Hello World!! ~~~~~~~ # 変更確認 git diff diff --git a/index.html b/index.html index 557db03..9369771 100644 --- a/index.html +++ b/index.html @@ -1 +1 @@ -Hello World +Hello World!! # ステージング・エリアに遷移 git add index.html # 変更確認 (既にステージング・エリアに遷移しているので、出力結果は何も表示されない) git diff # 次の commit で反映される変更を表示 git diff --cached diff --git a/index.html b/index.html index 557db03..9369771 100644 --- a/index.html +++ b/index.html @@ -1 +1 @@ -Hello World +Hello World!!
git reset
* 過去の状態に戻す
■ 使用上の注意
ファイルの削除/移動について
* ファイルの削除、移動する際には、gitコマンドを使う例
git mv index.html ./hello/index.html git rm index.html
参考文献
https://qiita.com/hshimo/items/ab91b99cd61724127aa7https://qiita.com/usizou/items/9e8ca22511b6d4e5a574