Git 超基本コマンド


git init
Gitリポジトリの作成

ディレクトリの作成

$ mkdir git-tutorial
$ cd git-tutorial

リポジトリの作成

$ git init
Initialized empty Git repository in /home/user/work/git-tutorial/.git/

これで git-tutorialディレクトリがGitリポジトリとして管理されるようになる。

git status
Git リポジトリの状態の確認

ファイルの作成

$ echo 'Hello,git!!' > README.md
$ cat README.md
Hello,git!!

Gitステータスの確認コマンド

$git status

ステータスの確認

On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        README.md

nothing added to commit but untracked files present (use "git add" to track)

・Untracked files(未追跡)状態とは
ステージングエリアにもリポジトリにも登録されていない状態。

Gitの管理下から外すとき

リポジトリの削除を行う

$ rm -rf .git 

git add
ステージングエリアへファイルを追加しコミット対象にする

ステージングエリアへのファイルの追加コマンド

$ git add README.md

ステータスの確認

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   README.md



複数ファイルの指定

$ git add <file> <file> ... 



全てのファイルを登録

$ git add .

・ステージングエリアとは
ファイルをコミットする前に変更内容を一時的に登録しておくバッファのようなもの。

ワーキングディレクトリとステージングエリアの差分の確認

# すでにステージングされたREADME.md の編集を行う
$ echo '# Hi!! Git!!' > README.md
$ cat README.md
# Hi!! Git!!
# Gitの状態の確認
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   README.md

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:   README.md

git diff
Gitでファイルの変更差分を確認する

ステージングエリアとの比較

$ git diff
diff --git a/README.md b/README.md
index e69de29..5264c8b 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1 @@
+# Hi!! Git!!

単語単位の比較

$ git diff --word-diff
diff --git a/README.md b/README.md
index e69de29..5264c8b 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1 @@{+# Hi!! Git!!+}

git commit
リポジトリの変更を記録する

ステージングエリアにされている登録されているファイル・ディレクトリをつの束としてリポジトリの歴史として記録します。

$ git commit -m "add ファイルを新規作成"
[master (root-commit) 1ca686b] add ファイルを新規作成
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md

git log
リポジトリのコミット履歴を閲覧する

リポジトリのコミット履歴を一覧で表示します。

$ git log
commit 1ca686bab5cbbaf29d411c5de449baf2131cd2ba (HEAD -> master)
Author: user.name <user.email>
Date:   Sun Oct 18 00:54:23 2020 +0900

    add ファイルを新規作成

git reset
コミットを取り消す

git reset はコミット履歴を過去にさかのぼってそれ以降のコミットを全てなかったことにします。

$ git reset <file>

ワーキングディレクトリの状態を直前の状態まで戻す

--hard オプションを付けると直前のコミット時と完全に一致した状態に戻される。

$ git reset --hard 

指定したコミットの状態まで戻す

コミットIDの指定
```
$ git reset