3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

git 基本

Posted at

リポジトリ作成

# git init
  1. 作業ディレクトリ作成
  2. 作業ディレクトリへ移動
  3. リポジトリ作成
コマンド例
# mkdir -p /home/git_test/
# cd /home/git_test/
# git init
	Initialized empty Git repository in /home/git_test/.git/

ステージング

# git add test.txt
  1. ファイル修正
  2. ステージング
コマンド例
# echo $(LANG=C;date) >> date.txt
# git add date.txt

コミット

# git commit -m '[メッセージ]' [ファイル名]
  1. ファイルの作成
  2. ステージング
  3. コミット
# echo $(LANG=C;date) > date.txt
# git add date.txt
# git commit -m 'message 1' date.txt
	[master (root-commit) 2d7fef7] message 1
	 1 files changed, 1 insertions(+), 0 deletions(-)
	 create mode 100644 date.txt

コミットメッセージ修正

# git commit --amend -m '[修正後のメッセージ]'

作業流れ

  1. ファイル修正
  2. ステージング
  3. コミット(あやまったメッセージを登録してしまった!)
  4. コミットメッセージ修正
コマンド例
# echo $(LANG=C;date) >> date.txt
# git add date.txt
# git commit -m 'message 999' date.txt
	[master 05c45ec] message 999
	 1 files changed, 1 insertions(+), 0 deletions(-)
# git commit --amend -m 'message 2'
	[master 572cd65] message 2
	 1 files changed, 1 insertions(+), 0 deletions(-)

ステージング前のファイル修正取消

# git checkout -- [ファイル名]
  1. ファイル作成
  2. 中身確認
  3. ステージング
  4. コミット ←ここの状態に戻す
  5. ファイル修正 ←ここの状態を
  6. 中身確認
  7. 修正取消
コマンド例
# echo $(LANG=C;date) >> date.txt
# cat date.txt
	Mon Nov 25 06:35:00 JST 2013
# git add date.txt
# git commit -m 'message 1' date.txt
	[master (root-commit) 1c7760a] message 1
	 1 files changed, 1 insertions(+), 0 deletions(-)
# echo $(LANG=C;date) >> date.txt
# cat date.txt
	Mon Nov 25 06:35:00 JST 2013
	Mon Nov 25 06:36:10 JST 2013
# git checkout -- date.txt
# cat date.txt
	Mon Nov 25 06:35:00 JST 2013

ステージング取消

# git reset HEAD [ファイル名]
  1. ファイル修正
  2. ステージング
  3. ステージング取消
コマンド例
# echo $(LANG=C;date) >> date.txt
# git add date.txt
# git reset HEAD date.txt
	Unstaged changes after reset:
	M       date.txt

コミット取消(ファイル修正はそのままの状態でステージングに戻す)

# git reset --soft HEAD^
  1. ファイル作成
  2. 中身確認
  3. ステージング
  4. コミット
  5. ファイル修正 ←ここの状態に戻す
  6. 中身確認
  7. ステージング
  8. コミット
  9. コミット取消
  10. 中身確認
コマンド例
# echo $(LANG=C;date) >> date.txt
# cat date.txt
	Mon Nov 25 06:35:00 JST 2013
# git add date.txt
# git commit -m 'message 1' date.txt
	[master (root-commit) 1c7760a] message 1
	 1 files changed, 1 insertions(+), 0 deletions(-)
# echo $(LANG=C;date) >> date.txt
# cat date.txt
	Mon Nov 25 06:35:00 JST 2013
	Mon Nov 25 06:36:10 JST 2013
# git add date.txt
# git commit -m 'message 2'
	[master 02da2c6] message 2
	 1 files changed, 1 insertions(+), 0 deletions(-)
# git reset --soft HEAD^
# cat date.txt
	Mon Nov 25 06:35:00 JST 2013
	Mon Nov 25 06:36:10 JST 2013

コミット取消(ファイル修正も取消)

※同時にコミットした複数のファイルがある場合、すべての修正内容が削除されるので注意

# git reset --hard HEAD^
  1. ファイル作成
  2. 中身確認
  3. ステージング
  4. コミット ←ここの状態に戻す
  5. ファイル修正
  6. 中身確認
  7. ステージング
  8. コミット
  9. コミット取消
  10. 中身確認
コマンド例
# echo $(LANG=C;date) >> date.txt
# cat date.txt
	Mon Nov 25 06:35:00 JST 2013
# git add date.txt
# git commit -m 'message 1' date.txt
	[master (root-commit) 1c7760a] message 1
	 1 files changed, 1 insertions(+), 0 deletions(-)
# echo $(LANG=C;date) >> date.txt
# cat date.txt
	Mon Nov 25 06:35:00 JST 2013
	Mon Nov 25 06:36:10 JST 2013
# git add date.txt
# git commit -m 'message 2'
	[master 02da2c6] message 2
	 1 files changed, 1 insertions(+), 0 deletions(-)
#  git reset --hard HEAD^
	HEAD is now at 1c7760a message

ワーキングツリーとステージングの差分確認

# git diff [ファイル名]
  1. ファイル作成
  2. ステージング
  3. コミット
  4. ファイル修正 ←ここと
  5. ステージング
  6. ファイル修正 ←ここの差分
  7. ワーキングツリーとステージングの差分確認
コマンド例
# echo $(LANG=C;date) >> date.txt
# git add date.txt
# git commit -m 'message 1'
# echo $(LANG=C;date) >> date.txt
# git add date.txt
# echo $(LANG=C;date) >> date.txt
# git diff date.txt
	diff --git a/date.txt b/date.txt
	index ef4ecec..b621436 100644
	--- a/date.txt
	+++ b/date.txt
	@@ -1,2 +1,3 @@
	 Mon Nov 25 06:18:32 JST 2013
	 Mon Nov 25 06:18:58 JST 2013
	+Mon Nov 25 06:19:09 JST 2013

ワーキングツリーとコミットの差分確認

# git diff HEAD [ファイル名]
  1. ファイル作成 ←ここと
  2. ステージング
  3. コミット
  4. ファイル修正
  5. ステージング
  6. ファイル修正 ←ここの差分
  7. ワーキングツリーとコミットの差分確認
コマンド例
# echo $(LANG=C;date) >> date.txt
# git add date.txt
# git commit -m 'message 1'
# echo $(LANG=C;date) >> date.txt
# git add date.txt
# echo $(LANG=C;date) >> date.txt
# git diff HEAD date.txt
	diff --git a/date.txt b/date.txt
	index 4a6fe57..b621436 100644
	--- a/date.txt
	+++ b/date.txt
	@@ -1 +1,3 @@
	 Mon Nov 25 06:18:32 JST 2013
	+Mon Nov 25 06:18:58 JST 2013
	+Mon Nov 25 06:19:09 JST 2013

ステージングとコミットの差分確認

# git diff --cached [ファイル名]
  1. ファイル作成 ←ここと
  2. ステージング
  3. コミット
  4. ファイル修正 ←ここの差分
  5. ステージング
  6. ファイル修正 ←ここの差分は表示されない
  7. ステージングとコミットの差分確認
コマンド例
# echo $(LANG=C;date) >> date.txt
# git add date.txt
# git commit -m 'message 1'
# echo $(LANG=C;date) >> date.txt
# git add date.txt
# echo $(LANG=C;date) >> date.txt
# git diff --cached date.txt
	diff --git a/date.txt b/date.txt
	index 4a6fe57..ef4ecec 100644
	--- a/date.txt
	+++ b/date.txt
	@@ -1 +1,2 @@
	 Mon Nov 25 06:18:32 JST 2013
	+Mon Nov 25 06:18:58 JST 2013
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?