Quantcast
Channel: プログラム の個人的なメモ
Viewing all articles
Browse latest Browse all 860

【分散リポジトリ】 Git ~ 入門編 ~

$
0
0

■ 基本コマンド

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

■ その他使い方

管理に含めない場合

[1] 「.gitignore」を作成する (vi .gitignore)
[2] 除外するファイルを「.gitignore」に記述する (例「*.log」)


関連記事

Git ~ 初期設定編 ~

https://blogs.yahoo.co.jp/dk521123/37613741.html

Viewing all articles
Browse latest Browse all 860


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>