Git メモ

はじめに

Gitの個人メモ。
文書中のコマンドもハイライトにしたかったけど、やり方がわからんかった・・・。

目次

インストール

インストールされている事が多いですが、念の為。yum install でインストールしよう。

[centos@ip-172-31-3-100 ~]$ sudo yum install git

git --version でバージョンが確認できればインストールは完了

[centos@ip-172-31-3-100 ~]$ git --version
git version 1.7.1
[centos@ip-172-31-3-100 ~]$

使い方

初期セットアップ

git config コマンドにパラメータを指定して、ユーザ名とメールアドレスを設定。これをしないとcommit 出来ないので、最初にやっておく。

[centos@ip-172-31-3-100 ~]$ git config --global user.email "hogehoge@domain.com"
[centos@ip-172-31-3-100 ~]$ git config --global user.name "hogehoge"

設定内容の確認方法。git config --list で全てのパラメータを確認出来る。--list を個別パラメータに変更すれば、個別に確認可能。

[centos@ip-172-31-3-100 shellscript]$ git config user.email
hogehogehoge@domain.com
[centos@ip-172-31-3-100 shellscript]$
[centos@ip-172-31-3-100 shellscript]$ git config --list
user.email=hogehoge@domain.com
user.name=hogehoge
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
remote.origin.url=git@bitbucket.org:hogehoge/shellscript.git
branch.master.remote=origin
branch.master.merge=refs/heads/master
[centos@ip-172-31-3-100 shellscript]$

設定内容はいろいろな場所に保存される可能性が、基本的にはユーザのホームディレクトリの .gitconfig に保存されています。 修正したい場合は、対象ファイルをを直接編集してもOK。

[centos@ip-172-31-3-100 ~]$ cat ~/.gitconfig
[user]
        email = hogehoge@domain.com
        name = hogehoge
[centos@ip-172-31-3-100 ~]$

SSHのセットアップ

HTTPだと毎回パスフレーズ入力が必要なので、パスなしの秘密鍵を作成しておく。

秘密鍵の作成は、下記記事を参考。 chimay.hatenablog.com

公開鍵を確認して、Github,GitBucketなどのアカウント設定でSSH公開鍵を登録する

[centos@ip-172-31-3-100 .ssh]$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAvXUH47WGmheQRJxtp3t572Srn6NLy/SdPvPstrtKz330B+il9xaNHhPkPRmqA3HtgkR/jKo4OFYX1oU7/ql0XLVt+E7JKnE/dyhkW893pP0ROOMB4lr1gA/h+GgAkB/JtYjGhMjbZrMc7NCGrB8x6o5cv6EI+b5e/tGSpj1JuokCI9/7w6J9ZaJkQuu65DaCBIJKXEMJZY3GU8klDdDZgQdQP2ANdmmCrefZfIZiYG0Aq4Fr+rdYXnijIqAjCHAgOa/12hcHhcIDeF0LUbYpvwN2HQ20jmu0ez7+9wA2NaGWPHZipRcP33o9+aZLs84mSUZ6aAw92dRJG6qO9F45uQ== centos@ip-172-31-3-100
[centos@ip-172-31-3-100 .ssh]$

秘密鍵の権限を確認する。
owner と group がユーザでかつ権限が 600 のみになっている事。

[centos@ip-172-31-3-100 ~]$ ls -la ~/.ssh/id_rsa
-rw-------. 1 centos centos 1675 Jun 24 21:31 /home/centos/.ssh/id_rsa
[centos@ip-172-31-3-100 ~]$

権限が異なる場合は、変更する

[centos@ip-172-31-3-100 ~]$ chown centos:centos /home/centos/.ssh/id_rsa
[centos@ip-172-31-3-100 ~]$ chmod 600 /home/centos/.ssh/id_rsa
[centos@ip-172-31-3-100 ~]$ ls -la ~/.ssh/id_rsa
-rw-------. 1 centos centos 1675 Jun 24 21:31 /home/centos/.ssh/id_rsa
[centos@ip-172-31-3-100 ~]$

リポジトリの複製

SSHの設定が問題なく登録できれば、パスなしでリモートからローカルに複製できる。URLは適宜置き換えてください。

[centos@ip-172-31-3-100 ~]$ git clone git@bitbucket.org:hogehoge/shellscript.git
Initialized empty Git repository in /home/centos/shellscript/.git/
remote: Counting objects: 21, done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 21 (delta 7), reused 0 (delta 0)
Receiving objects: 100% (21/21), done.
Resolving deltas: 100% (7/7), done.
[centos@ip-172-31-3-100 ~]$

ブランチの確認

git branch コマンドで確認可能。-a オプションを付ける事で remote のbranch も確認出来ます。

[centos@ip-172-31-3-100 shellscript]$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
[centos@ip-172-31-3-100 shellscript]$

ブランチの作成

git branch コマンドの引数に作成したいブランチ名を指定。例ではローカルで開発用のdevというブランチを作成しています。作成するだけでは、ブランチが切り替わらない点も注意です。

[centos@ip-172-31-3-100 shellscript]$ git branch dev
[centos@ip-172-31-3-100 shellscript]$
[centos@ip-172-31-3-100 shellscript]$ git branch -a
  dev
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
[centos@ip-172-31-3-100 shellscript]$

ブランチの切り替え

git checkout コマンドでブランチ名を指定する事でブランチが切り替わります。切り替わった事を確認したら、実際に編集してみる。

[centos@ip-172-31-3-100 shellscript]$ git checkout dev
Switched to branch 'dev'
[centos@ip-172-31-3-100 shellscript]$ git branch -a
* dev
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
[centos@ip-172-31-3-100 shellscript]$

変更履歴を確認する

git status コマンドを使って確認できる。変更前の確認。

[centos@ip-172-31-3-100 shellscript]$ git status
# On branch dev
nothing to commit (working directory clean)
[centos@ip-172-31-3-100 shellscript]$

新規ファイル(test.sh)を作成。再度、git status で変更履歴がしっかり表示されていることを確認

[centos@ip-172-31-3-100 shellscript]$ git status
# On branch dev
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       test.sh
nothing added to commit but untracked files present (use "git add" to track)
[centos@ip-172-31-3-100 shellscript]$

変更差分を確認する

git diff コマンドで変更したファイルの変更差分を確認する事ができる。diff のあとにファイルを指定する事で特定ファイルのみの差分確認も可能。なお、空ファイルの作成では diff の結果として出力されなかった。

[centos@ip-172-31-3-100 shellscript]$ git status
# Not currently on any branch.
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   test2.sh
#
no changes added to commit (use "git add" and/or "git commit -a")
[centos@ip-172-31-3-100 shellscript]$ git diff
diff --git a/test2.sh b/test2.sh
index e69de29..48b1ba2 100644
--- a/test2.sh
+++ b/test2.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+echo "Hellow World"
[centos@ip-172-31-3-100 shellscript]$

インデックスへの追加

commit の前に変更したファイルをインデックスに追加する必要がある。git add コマンドの引数にファイルパスを指定する事で、指定したファイルをインデックスに登録できる。

[centos@ip-172-31-3-100 shellscript]$ git add test.sh
[centos@ip-172-31-3-100 shellscript]$
[centos@ip-172-31-3-100 shellscript]$ git status
# On branch dev
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   test.sh
#
[centos@ip-172-31-3-100 shellscript]$

もし、誤って違うファイルを追加した場合でも git resetコマンドで対象ファイルを指定すればインデックスから削除する事が出来る

[centos@ip-172-31-3-100 shellscript]$ git reset test.sh
[centos@ip-172-31-3-100 shellscript]$ git status
# On branch dev
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       test.sh
nothing added to commit but untracked files present (use "git add" to track)
[centos@ip-172-31-3-100 shellscript]$

複数ファイルを変更した場合だといちいちファイルを指定してられないので、git add --all (もしくは -A) を付与する事で全てのファイルを追加できる。

[centos@ip-172-31-3-100 shellscript]$ git status
# On branch dev
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       test.sh
#       test2.sh
nothing added to commit but untracked files present (use "git add" to track)
[centos@ip-172-31-3-100 shellscript]$
[centos@ip-172-31-3-100 shellscript]$ git add --all
[centos@ip-172-31-3-100 shellscript]$ git status
# On branch dev
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   test.sh
#       new file:   test2.sh
#
[centos@ip-172-31-3-100 shellscript]$

コミットする

git commit コマンドでインデックスに登録した変更内容をコミットする。-m オプションでコミットメッセージを指定。コメントは必須なので、作業内容を誰が見てもわかるように解りやすく指定する。

[centos@ip-172-31-3-100 shellscript]$ git commit -m "initial commit"
[dev 439582b] initial commit
 1 files changed, 3 insertions(+), 0 deletions(-)
 create mode 100644 test.sh
 create mode 100644 test2.sh
[centos@ip-172-31-3-100 shellscript]$
[centos@ip-172-31-3-100 shellscript]$ git status
# On branch dev
nothing to commit (working directory clean)
[centos@ip-172-31-3-100 shellscript]$

なお、git commit コマンドに -a オプションを付与する事でインデックスへの登録とコミットを同時に行う事ができる。但し、削除・変更のみで 新規ファイルの追加は -a オプションではコミットされないので注意。

[centos@ip-172-31-3-100 shellscript]$ git status
# Not currently on any branch.
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   test2.sh
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       test3.sh
no changes added to commit (use "git add" and/or "git commit -a")
[centos@ip-172-31-3-100 shellscript]$
[centos@ip-172-31-3-100 shellscript]$ git commit -a -m "create test3.sh"
[detached HEAD b206404] create test3.sh
 1 files changed, 3 insertions(+), 0 deletions(-)
[centos@ip-172-31-3-100 shellscript]$ git status
# Not currently on any branch.
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       test3.sh
nothing added to commit but untracked files present (use "git add" to track)
[centos@ip-172-31-3-100 shellscript]$

コミットログを確認する

git log コマンドで変更履歴を確認する事ができる。--oneline を付与する事で、各コミットログを1行にまとめる事ができる。短縮されたハッシュ値でもコマンドの引数としては有効に動作します。

[centos@ip-172-31-3-100 shellscript]$ git log
commit 439582b1b43ae87e84e9bbaa8cade472ff57f835
Author: hogehoge <hogehogehoge@domain.com>
Date:   Sun Jun 26 19:48:08 2016 +0000

    initial commit
[centos@ip-172-31-3-100 shellscript]$
[centos@ip-172-31-3-100 shellscript]$ git log --oneline
439582b initial commit
[centos@ip-172-31-3-100 shellscript]$

ファイルを削除する

git rm コマンドで不要なファイルを削除することができる。インデックスに登録済みの場合でも、-f を付与する事で削除できる。

[centos@ip-172-31-3-100 shellscript]$ git rm -f test2.sh
rm 'test2.sh'
[centos@ip-172-31-3-100 shellscript]$

特定のリビジョンに戻る

git checkout コマンドで特定のリビジョンのコミットハッシュ値を指定する事で、指定したリビジョンまで戻る事ができる。短縮されたハッシュ値でも有効。

[centos@ip-172-31-3-100 shellscript]$ git commit -m "delete test2.sh"
[detached HEAD 0ff40e8] delete test2.sh
 0 files changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 test2.sh
[centos@ip-172-31-3-100 shellscript]$
[centos@ip-172-31-3-100 shellscript]$ git log --oneline
0ff40e8 delete test2.sh
439582b initial commit
2bf942c 変数化
5417639 new file
61979b7 backup2s3 created online with Bitbucket
6fa1a30 sendmessage_perl.sh created online with Bitbucket
1d79198 swap.sh created online with Bitbucket
ccabd68 create check_access.sh
6be6f02 Initial commit with contributors
[centos@ip-172-31-3-100 shellscript]$ git checkout 439582b
Previous HEAD position was 0ff40e8... delete test2.sh
HEAD is now at 439582b... initial commit
[centos@ip-172-31-3-100 shellscript]$

未登録のファイルを削除する

git cleanを使う事で未登録ファイルを削除する事ができる。未登録とは git status コマンドで Untracked になっているファイルの事。この例だと、test3.sh のみインデックスに登録したのでそれ以外は削除される事になる。デフォルトの設定では -f オプションを付与しないと事故防止の意味合いもあって、エラーになる。clean.requireForce という設定値を変更する事で、変えられるがあまりお勧めはしない。

また、-n オプションを付与する事で削除対象ファイルを確認する事ができる。削除はくれぐれも慎重に!

[centos@ip-172-31-3-100 shellscript]$ touch test{3..30}.sh
[centos@ip-172-31-3-100 shellscript]$ git add test3.sh
[centos@ip-172-31-3-100 shellscript]$ git status
# Not currently on any branch.
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   test3.sh
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       test10.sh
#       test11.sh
#       test12.sh
#       test13.sh
#       test14.sh
#       test15.sh
#       test16.sh
#       test17.sh
#       test18.sh
#       test19.sh
#       test20.sh
#       test21.sh
#       test22.sh
#       test23.sh
#       test24.sh
#       test25.sh
#       test26.sh
#       test27.sh
#       test28.sh
#       test29.sh
#       test30.sh
#       test4.sh
#       test5.sh
#       test6.sh
#       test7.sh
#       test8.sh
#       test9.sh
[centos@ip-172-31-3-100 shellscript]$ git clean -n
Would remove test10.sh
Would remove test11.sh
Would remove test12.sh
Would remove test13.sh
Would remove test14.sh
Would remove test15.sh
Would remove test16.sh
Would remove test17.sh
Would remove test18.sh
Would remove test19.sh
Would remove test20.sh
Would remove test21.sh
Would remove test22.sh
Would remove test23.sh
Would remove test24.sh
Would remove test25.sh
Would remove test26.sh
Would remove test27.sh
Would remove test28.sh
Would remove test29.sh
Would remove test30.sh
Would remove test4.sh
Would remove test5.sh
Would remove test6.sh
Would remove test7.sh
Would remove test8.sh
Would remove test9.sh
[centos@ip-172-31-3-100 shellscript]$
[centos@ip-172-31-3-100 shellscript]$ git clean -f
Removing test10.sh
Removing test11.sh
Removing test12.sh
Removing test13.sh
Removing test14.sh
Removing test15.sh
Removing test16.sh
Removing test17.sh
Removing test18.sh
Removing test19.sh
Removing test20.sh
Removing test21.sh
Removing test22.sh
Removing test23.sh
Removing test24.sh
Removing test25.sh
Removing test26.sh
Removing test27.sh
Removing test28.sh
Removing test29.sh
Removing test30.sh
Removing test4.sh
Removing test5.sh
Removing test6.sh
Removing test7.sh
Removing test8.sh
Removing test9.sh
[centos@ip-172-31-3-100 shellscript]$ git status
# Not currently on any branch.
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   test3.sh
#
[centos@ip-172-31-3-100 shellscript]$

まとめ

まだまだできる事があるので、気が向いたら随時アップデートしていきます。