git(ギット)とは?

gitインストール

# yum -y update
# yum -y install git
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
Setting up Install Process
No package git available.
Nothing to do
# wget http://ftp-srv2.kddilabs.jp/Linux/distributions/fedora/epel/5/i386/epel-release-5-4.noarch.rpm 
# rpm -ivh epel-release-5-4.noarch.rpm
# vi /etc/yum.repos.d/epel.repo
--------------------
   [epel]
   ...
   enabled=1
   priority=1
   ...
--------------------
# yum -y install git
# git --version
git version 1.8.2.1

バージョン管理の流れ

  1. 作業ディレクトリ
  2. ステージングエリア(インデックス)
  3. リポジトリ(ローカル、リモート)

gitの設定

$ git config --global user.name "Yuji Shimojo"
$ git config --global user.email "yjshimojo@gmail.com"
$ git config --global color.ui true
$ git config -l
user.name=Yuji Shimojo
user.email=yjshimojo@gmail.com
color.ui=true
$ git config --help
$ git help config

リポジトリへのコミット

$ mkdir myweb
$ cd myweb/
$ git init
Initialized empty Git repository in /home/yuji/myweb/.git/
$ vi index.html
--------------------
    line 1
--------------------
$ cat index.html
line 1
$ git add index.html
$ git commit
--------------------
    initial commit // コミットメッセージ(任意)
--------------------
[master (root-commit) 12662e4] initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 index.html

コミット履歴(ログ)の確認・オプション

$ git log
commit 12662e4f226f299708ddb5c89274739fd355f9b8 // 一意のコミットID
Author: Yuji Shimojo <yjshimojo@gmail.com>
Date:   Sun Sep 20 03:03:31 2015 +0900
    initial commit          // コミットメッセージ
$ git log --oneline
12662e4 initial commit
$ git log -p
diff --git a/index.html b/index.html
new file mode 100644
index 0000000..89b24ec
--- /dev/null
+++ b/index.html
@@ -0,0 +1 @@
+line 1
$ git log --stat
commit 12662e4f226f299708ddb5c89274739fd355f9b8
Author: Yuji Shimojo <yjshimojo@gmail.com>
Date:   Sun Sep 20 03:03:31 2015 +0900

    initial commit

 index.html | 1 +
 1 file changed, 1 insertion(+)

状態確認

$ vi index.html
--------------------
    line 1
    line 2
--------------------
$ git status
# On branch master
# Changes not staged for commit:        // ステージング未反映、未コミット
#   (use "git add <file>..." to update what will be committed)    // git addでステージングに反映可能
#   (use "git checkout -- <file>..." to discard changes in working directory)    // git checkoutで変更前の状態に戻すことが可能
#
#	modified:   index.html
#
no changes added to commit (use "git add" and/or "git commit -a")
$ git checkout -- index.html
$ cat index.html
line 1

差分確認

$ git diff
diff --git a/index.html b/index.html
index 89b24ec..7bba8c8 100644
--- a/index.html
+++ b/index.html
@@ -1 +1,2 @@
 line 1
+line 2
$ git add index.html    // ステージングエリアへ追加
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	modified:   index.html
#
$ git diff --cached
diff --git a/index.html b/index.html
index 89b24ec..7bba8c8 100644
--- a/index.html
+++ b/index.html
@@ -1 +1,2 @@
 line 1
+line 2

gitでのファイル操作

$ git add [filename1] [filename2] [filename3] ...
$ git add .
$ git rm index.html
$ git mv file1 file2

git管理からの除外設定

$ ls
error.log  index.html
$ vi .giignore
--------------------
  *.log
--------------------

直前のコミットを変更

$ git commit -m "ライン2を追加"
[master 51e7ce3] ライン2を追加
 1 file changed, 1 insertion(+)
$ git commit -am "ライン2を追加"
$ git commit --amend

過去のバージョンに戻る

$ git reset --hard HEAD
$ git reset --hard HEAD^
$ git reset --hard 12662e4f226f
$ git reset --hard ORIG_HEAD

ブランチを活用

$ git branch
$ git branch hoge
$ git checkout hoge
Switched to branch 'hoge'

ブランチをマージ

$ git branch
  hoge
* master
$ git merge hoge
Updating 556ff5f..054edfd
Fast-forward
 myscript.js | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 myscript.js
$ git branch -d hoge
Deleted branch hoge (was 054edfd).

マージの衝突を解決

$ git checkout -b hogehoge
Switched to a new branch 'hogehoge'
$ vi index.html 
--------------------
  line first
--------------------
$ git add .
$ git commit -m "not 1 but first"
$ checkout master
$ vi index.html 
--------------------
  line 1st
--------------------
$ git add .
$ git commit -m "not 1 but 1st"
$ git merge hogehoge
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
$ git status
# On branch master
# You have unmerged paths.
#   (fix conflicts and run "git commit")
#
# Unmerged paths:
#   (use "git add <file>..." to mark resolution)
#
#	both modified:      index.html
#
no changes added to commit (use "git add" and/or "git commit -a")
$ vi index.html
--------------------
    <<<<<<< HEAD
    line 1st
    =======
    line first
    >>>>>>> hogehoge
--------------------
--------------------
    line 1st
--------------------
$ git add .
$ git commit -m "conflict fixed"

タグを利用

$ git tag v1.0
$ git tag
v1.0
$ git show v1.0
commit 1c720200dff693a0f3a867e33e5cbe7df89be7cc
Merge: 934787b 0a79932
Author: Yuji Shimojo <yjshimojo@gmail.com>
Date:   Sun Sep 20 07:21:50 2015 +0900

    conflic fixed

diff --cc index.html
index 4457139,cfbee75..12ead6b
--- a/index.html
+++ b/index.html
@@@ -1,2 -1,2 +1,1 @@@
 -line first
 +line 1st
$ git tag v0.9 [commit id]
$ git tag -d v0.9

gitコマンドにエイリアス(短縮名)を付与

$ git config --global alias.co checkout
$ git config --global alias.st status
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config -l
...
alias.co=checkout
alias.st=status
alias.br=branch
alias.ci=commit
...

共有リポジトリを活用して共同作業

$ mkdir ourweb.git
$ cd ourweb.git/
$ git init --bare    // 共有リポジトリ作成の際は「bare」オプションを付ける
$ git remote add origin ~/ourweb.git
$ git config -l
...
remote.origin.url=/home/yuji/ourweb.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
$ git remote rm origin
$ git push origin master
Counting objects: 20, done.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (20/20), 1.38 KiB, done.
Total 20 (delta 5), reused 0 (delta 0)
To /home/yuji/ourweb.git
 * [new branch]      master -> master
$ git clone ~/ourweb.git/ myweb2
Cloning into 'myweb2'...
done.
$ vi index.html
$ git add .
$ git commit -m "line 2 added"
$ git push origin master
$ git pull origin master
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /home/yuji/ourweb
 * branch            master     -> FETCH_HEAD
Updating 1c72020..2a78028
Fast-forward
 index.html | 1 +
 1 file changed, 1 insertion(+)

共有時の衝突(コンフリクト)解決

$ git commit -am "line 3 added"
[master dbc4d95] line 3 added
 1 file changed, 1 insertion(+)
$ git push origin master
To /home/yuji/ourweb.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '/home/yuji/ourweb.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
$ git pull origin master
$ vi index.html
--------------------
    <<<<<<< HEAD
    line 3
    =======
    line third
    >>>>>>> 5229c27320cbb89e55945b7321c731526ef72d93
--------------------
--------------------
    line 3
--------------------
$ git commit -am "conflic fixed"
$ git log
commit c019c365bcb943295f80ff61c1cbe02855b7af44
Merge: dbc4d95 5229c27
Author: Yuji Shimojo <yjshimojo@gmail.com>
Date:   Sun Sep 20 08:17:02 2015 +0900

    conflic fixed
...

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS