LoginSignup
7
7

More than 5 years have passed since last update.

Gitことはじめ

Last updated at Posted at 2016-03-07

Linuxのコマンドライン操作に基づいて解説する。

まずは個人で活用できるようになることを目指す。
Gitを用いることで、プログラミングの際にファイルのチェックポイントを作成し、プログラムの管理ができる。簡単に、以前のバージョンへ戻すことができる。

とりあえず
「git add」と「git commit」できたらいいと思う。

Gitのインストール

Linux Debian系(Ubuntu, Mint)では
「sudo apt-get install git」
他は、
https://git-scm.com/book/ja/v1/%E4%BD%BF%E3%81%84%E5%A7%8B%E3%82%81%E3%82%8B-Git%E3%81%AE%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB
参照

Gitで管理するフォルダを作成する。

git init:このフォルダをgitで管理する

[hnishi@hn workspace]$ mkdir gittest
[hnishi@hn workspace]$ cd gittest
[hnishi@hn gittest]$ git init
Initialized empty Git repository in /home/hnishi/workspace/gittest/.git/
[hnishi@hn gittest]$ ls -a
.  ..  .git 

gitで管理するための隠しフォルダ「.git」が、「git init」によって作成された。

ファイルを作成し、監視する。

git add:ファイルのステージング
git status:このフォルダの状況を表示する

[hnishi@hn gittest]$ touch test.txt
[hnishi@hn gittest]$ ls
test.txt
[hnishi@hn gittest]$ git status
On branch master

Initial commit

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

        test.txt

nothing added to commit but untracked files present (use "git add" to track)
[hnishi@hn gittest]$ git add test.txt
[hnishi@hn gittest]$ git status
On branch master

Initial commit

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

        new file:   test.txt

「git add」する前と、した後の「git status」を比較すると、
Untracked files:(監視されていないファイル)に入っていた「test.txt」が
Changes to be committed:(コミットされる予定のファイル)に入る。

コミットする

git commit:現在のバージョンのファイルを記録する
git log:これまでのコミットの情報を表示する

[hnishi@hn gittest]$ git commit -m "Initial commit"
[master (root-commit) a87350c] Initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
[hnishi@hn gittest]$ git log
commit a87350c4a60ddc96d507c722f806e49a1d2d6e0f
Author: hnishi <email-address@mail.com>
Date:   Mon Mar 7 12:59:17 2016 +0900

    Initial commit

「git commit」の「-m」オプションでコミットする際のメッセージを指定する。

ファイルを更新する

[hnishi@hn gittest]$ echo "hogehoge" >> test.txt
[hnishi@hn gittest]$ cat test.txt
hogehoge
[hnishi@hn gittest]$ 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:   test.txt

no changes added to commit (use "git add" and/or "git commit -a")
[hnishi@hn gittest]$ git commit -a
[master 05c6d4e]  Changes to be committed:      modified:   test.txt
 1 file changed, 3 insertions(+)
[hnishi@hn gittest]$ git log
commit 05c6d4e5151db1c41d03caaddce93aacbf8b3f8e
Author: hnishi <email-address@mail.com>
Date:   Mon Mar 7 14:09:57 2016 +0900

     Changes to be committed:
        modified:   test.txt

commit a87350c4a60ddc96d507c722f806e49a1d2d6e0f
Author: hnishi <email-address@mail.com>
Date:   Mon Mar 7 12:59:17 2016 +0900

    Initial commit

ファイルの内容を更新してから、もう一度コミットする。
更新のたびに「git add」しなければならないが、「git commit -a」というオプションで、以前にコミットしたことのあるファイルを再度addしなくてもcommitできる。
commitのときに「-m」オプションを付けない場合は、デフォルトのエディタが開かれコミット時のメッセージをエディタで編集できる。

デフォルトエディタの設定
いくつか方法がある
(1)Linuxのシェルの設定を変える
初期設定では、nanoだったりemacsだったりする。
たとえば、vimをデフォルトにしたい場合、
bashでは「~/.bashrc」の中に、「export EDITOR="vim"」を追記して
「source ~/.bashrc」するだけでよい。
(2)git側の設定を変える
[hnishi@hn gittest]$ git config --global core.editor "vim"
http://stackoverflow.com/questions/2596805/how-do-i-make-git-use-the-editor-of-my-choice-for-commits
参照

以前のバージョンに戻す

git checkout

[hnishi@hn gittest]$ cat test.txt
hogehoge
[hnishi@hn gittest]$ git checkout a87350c4a60ddc96d507c722f806e49a1d2d6e0f
Note: checking out 'a87350c4a60ddc96d507c722f806e49a1d2d6e0f'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at a87350c... Initial commit
[hnishi@hn gittest]$ cat test.txt
[hnishi@hn gittest]$ 

ちなみにこの状態は、「detached HEAD」という一時的な状態。この説明には、「branch」の理解が必要。

「git log」でSHA(Secure Hash Algorithm)と呼ばれる各バージョンに付加されたIDを確認し、「git checkout」の後ろにコピペすることで、その時点のファイルへ戻すことができる。
なお、SHAは最初の4文字だけでもいい。

以前のバージョンに戻しちゃったけど、やっぱり取り消す

git reflog:checkout前のSHAを見る

[hnishi@hn gittest]$ git reflog
a87350c HEAD@{0}: checkout: moving from master to a87350c4a60ddc96d507c722f806e49a1d2d6e0f
05c6d4e HEAD@{1}: commit: Changes to be committed:
a87350c HEAD@{2}: commit (initial): Initial commit
[hnishi@hn gittest]$ git checkout 05c6d4e
Previous HEAD position was a87350c... Initial commit
HEAD is now at 05c6d4e...  Changes to be committed:     modified:   test.txt
[hnishi@hn gittest]$ git status
HEAD detached at 05c6d4e
nothing to commit, working directory clean

以前のバージョンと比較する

git show:1つ前のバージョンとdiffを取る

[hnishi@hn gittest]$ echo "add something" >> test.txt
[hnishi@hn gittest]$ git commit -am "3rd commit"
[master cfdf4ed] 3rd commit
 1 file changed, 1 insertion(+), 2 deletions(-)
[hnishi@hn gittest]$ git show
commit cfdf4eda757429fe831afdb65ea7e4d30c776918
Author: hnishi <email-address@mail.com>
Date:   Mon Mar 7 15:05:26 2016 +0900

    3rd commit

diff --git a/test.txt b/test.txt
index e40ff14..6c054f8 100644
--- a/test.txt
+++ b/test.txt
@@ -1,1 +1,2 @@
 hogehoge
+add something

git diff:特定のバージョンとdiffを取る

[hnishi@hn gittest]$ git diff a87350c4a60ddc96d507c722f806e49a1d2d6e0f
diff --git a/test.txt b/test.txt
index e69de29..e40ff14 100644
--- a/test.txt
+++ b/test.txt
@@ -0,0 +1,3 @@
+
+hogehoge
+hogehoge

関連記事

GitHub 管理指南
GitHubレポジトリの作成からアップロードまで
http://qiita.com/hnishi/items/6c3b37d747b15cb37ec8

7
7
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
7
7