LoginSignup
0
0

More than 3 years have passed since last update.

gitのコマンドメモ

Last updated at Posted at 2019-11-01

gitをローカルで使う場合、よく使うコマンドについてのメモ。
push,pullなどのリモートリポジトリとの連携は後日書こうと思います。
gitのインストールは完了している想定です。

※筆者はgitにあまり慣れていないため、誤った認識、記載があるかもしれません。ご注意ください。

1.リポジトリ作成

作業ディレクトリ下に移動してgit initを実行。
これでgitリポジトリが作られる。ls -aで確認してやると、.gitができている。

$ git init
$ ls -a
.  ..  .git

2.ディレクトリ更新

適当な新しいファイルa.txtを作成してみた後にgit statusで状態を確認してみる。
現在のリポジトリでは追跡できていないファイルが表示される。

$ git status
On branch master

Initial commit

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

        a.txt

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

git addで、更新情報を保存してやる。--allオプションは、変更されたファイル一式が更新される。

$ git add --all

3.コミット

リポジトリにコミット。-m以下はメッセージ。

$ git commit -m "first commit"
[master (root-commit) d80db16] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt

git statusで作業ディレクトリの更新を確認して、nothing to commitになっていれば、無事完了

$ git status
On branch master
nothing to commit, working directory clean

4.ブランチの作成

各自開発をしていく時には、masterからブランチを切ってやり、作業完了後にmasterに統合してやる。
今はブランチがmasterのみなので、作業用のブランチを作成してやる。

$ git branch mywork
$ git branch
* master
  mywork

新しいブランチmyworkへ移動して、切り替わっていることを確認。

$ git checkout mywork
Switched to branch 'mywork'
$ git branch
  master
* mywork

もう一度新しいファイルb.txtを作って、コミットしてやる。

$ git add --all
$ git commit -m "b.txtを追加"
[mywork 0f05c45] b.txtを追加
 1 file changed, 1 insertion(+)
 create mode 100644 b.txt

myworkブランチでのgitのログを見てみると、2回コミットした履歴が確認できる。

$ git log -p
commit 0f05c45536fc6120da0a2c6c05d5e5e17946e817
Author: user
Date:   Fri Nov 1 06:39:51 2019 +0000

    b.txtを追加

diff --git a/a.txt b/b.txt
new file mode 100644
index 0000000..3e75765
--- /dev/null
+++ b/b.txt
@@ -0,0 +1 @@
+bbbbb

commit d80db16e3263fc9b22978bafdfe790425ef17306
Author: user
Date:   Fri Nov 1 06:34:58 2019 +0000

    first commit

diff --git a/a.txt b/a.txt
new file mode 100644
index 0000000..2d832d9
--- /dev/null
+++ b/a.txt
@@ -0,0 +1 @@
+aaaaa

5.ブランチのマージ

git mergeで、さっき新しく更新したmyworkをmasterに統合する。

$ git merge mywork
Updating d80db16..0f05c45
Fast-forward
 b.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 b.txt

6.過去の状態でブランチを作成する

今のディレクトリ内ではa.txt,b.txtの2ファイルが存在する。

$ ls -a
.  ..  .git  a.txt  b.txt

a.txtを作る前のfirst commitのタイミングに戻って作業したい時は、
git logで記載されているcommit以降のidを入力してgit checkout -bを行うと、過去の状態でのブランチを作成することができる。

$ git checkout mywork
Switched to branch 'mywork'
$ git log 
commit 0f05c45536fc6120da0a2c6c05d5e5e17946e817
Author: user
Date:   Fri Nov 1 06:39:51 2019 +0000

    b.txtを追加

commit d80db16e3263fc9b22978bafdfe790425ef17306
Author: user
Date:   Fri Nov 1 06:34:58 2019 +0000

    first commit
$ git checkout -b mywork_first  d80db16e3263fc9b22978bafdfe790425ef17306
Switched to a new branch 'mywork_first'

新しいブランチができていて、git logfirst commit時に戻っている。
ディレクトリ内もb.txtができる前の状態に戻った。

$ git branch
  master
  mywork
* mywork_first
$ git log
commit d80db16e3263fc9b22978bafdfe790425ef17306
Author: user
Date:   Fri Nov 1 06:34:58 2019 +0000

    first commit
$ ls -a
.  ..  .git  a.txt
0
0
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
0
0