01. Git のインストールとリポジトリの初期化

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@gitlab ~]#yum install -y git
[root@gitlab ~]#git --version
git version 1.8.3.1
[root@gitlab ~]#git config --global user.name "dawn"
[root@gitlab ~]#git config --global user.email "dawn@mail.com"
[root@gitlab ~]#git config --global color.ui true
[root@gitlab ~]#cat .gitconfig
[user]
        name = dawn
        email = dawn@mail.com
[color]
        ui = true
[root@gitlab ~]#mkdir dawn
[root@gitlab ~]#cd dawn
[root@gitlab dawn]#ll
total 0
[root@gitlab dawn]#git init
Initialized empty Git repository in /root/dawn/.git/
[root@gitlab dawn]#ll
total 0
[root@gitlab dawn]#ll -a
total 0
drwxr-xr-x  3 root root  18 Jun 11 10:57 .
dr-xr-x---. 4 root root 174 Jun 11 10:57 ..
drwxr-xr-x  7 root root 119 Jun 11 10:57 .git

02. Git コマンド

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
1. リポジトリの状態を確認する(常にクリーンに保つ)
[root@gitlab dawn]#git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)

2. 通常のファイルを作成する
[root@gitlab dawn]#touch a.txt

3. 作業ツリーの内容をステージングする
[root@gitlab dawn]#git add a.txt
# git add . は作業ツリーの全内容をステージング領域に追加する
[root@gitlab dawn]#git add .

4. ステージング領域の内容を削除する
[root@gitlab dawn]#git rm --cached a.txt
rm 'a.txt'

5. ステージングしてローカルリポジトリにコミットする
[root@gitlab dawn]#git add a.txt
[root@gitlab dawn]#git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   a.txt
#

# git commit -m にメッセージを付けてローカルリポジトリにコミットする
[root@gitlab dawn]#git commit -m "create a.txt"
[master (root-commit) b65d348] create a.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a.txt

状態を確認:
[root@gitlab dawn]#git status
# On branch master
nothing to commit, working directory clean

ファイル作成       touch a.txt
ステージング       git add a.txt
ローカルリポジトリにコミット   git commit -m "xxx"

2. git での削除

[root@gitlab dawn]#git rm a.txt
rm 'a.txt'
[root@gitlab dawn]#git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    a.txt
#
[root@gitlab dawn]#git commit -m "del a.txt"
[master 41fc71b] del a.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 a.txt
[root@gitlab dawn]#git status
# On branch master
nothing to commit, working directory clean

注意: 作業ツリーのファイルをステージングした後に取り消したい場合は、ステージング領域から削除する
[root@gitlab dawn]#touch c.txt
[root@gitlab dawn]#git add c.txt
[root@gitlab dawn]#git rm --cached c.txt
rm 'c.txt'

3. 作業ツリーの内容を復元する
[root@gitlab dawn]#git reset HEAD b.txt
[root@gitlab dawn]#git checkout -- b.txt

4. git での差分確認
git diff  作業ツリーとステージング領域の差分を比較する
[root@gitlab dawn]#git diff
diff --git a/b.txt b/b.txt
index e69de29..ccc3e7b 100644
--- a/b.txt
+++ b/b.txt
@@ -0,0 +1 @@
+aaaaa

git diff --cached ステージング領域とローカルリポジトリの差分を比較する
[root@gitlab dawn]#git diff --cached
diff --git a/b.txt b/b.txt
index e69de29..ccc3e7b 100644
--- a/b.txt
+++ b/b.txt
@@ -0,0 +1 @@
+aaaaa

5. ファイル名の変更
[root@gitlab dawn]#git mv b.txt  b.bak
[root@gitlab dawn]#git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   renamed:    b.txt -> b.bak
#
[root@gitlab dawn]#git commit -m "newname b.txt-->b.bak"
[master 55335a7] newname b.txt-->b.bak
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename b.txt => b.bak (100%)
[root@gitlab dawn]#git status
# On branch master
nothing to commit, working directory clean

6. 任意のスナップショットにロールバックする
すべてのスナップショットを表示:
[root@gitlab dawn]#git log --oneline
ae6585c xx
55335a7 newname b.txt-->b.bak
09ffe53 modifile b.txt
043a297 newfile b.txt
41fc71b del a.txt
b65d348 create a.txt
[root@gitlab dawn]#ll
total 8
-rw-r--r-- 1 root root 6 Jun 11 12:10 b.bak
-rw-r--r-- 1 root root 6 Jun 11 12:07 b.txt

git reset --hard でロールバックする
[root@gitlab dawn]#git reset --hard b65d348
HEAD is now at b65d348 create a.txt
[root@gitlab dawn]#ll
total 0
-rw-r--r-- 1 root root 0 Jun 11 12:12 a.txt

7. 過去のすべての操作履歴を確認する:
[root@gitlab dawn]#git reflog
b65d348 HEAD@{0}: reset: moving to b65d348
ae6585c HEAD@{1}: commit: xx
55335a7 HEAD@{2}: commit: newname b.txt-->b.bak
09ffe53 HEAD@{3}: commit: modifile b.txt
043a297 HEAD@{4}: commit: newfile b.txt
41fc71b HEAD@{5}: commit: del a.txt
b65d348 HEAD@{6}: commit (initial): create a.txt
[root@gitlab dawn]#git reset --hard ae6585c
HEAD is now at ae6585c xx
[root@gitlab dawn]#ll
total 8
-rw-r--r-- 1 root root 6 Jun 11 12:16 b.bak
-rw-r--r-- 1 root root 6 Jun 11 12:16 b.txt

Git ブランチ
1. ブランチ一覧を表示する(* は現在のブランチ)
[root@gitlab dawn]#git branch
* master
2. ブランチを作成する
[root@gitlab dawn]#git branch dev
[root@gitlab dawn]#git branch
  dev
* master

3. dev ブランチに切り替える
[root@gitlab dawn]#git checkout dev
Switched to branch 'dev'
[root@gitlab dawn]#git branch
* dev
  master

4. ブランチ上でファイルを作成し、ローカルリポジトリにコミットする
[root@gitlab dawn]#touch aa.txt
[root@gitlab dawn]#git add .
[root@gitlab dawn]#git commit -m "newfile aa.txt"
[dev 2694c7f] newfile aa.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 aa.txt

5. ブランチをマージする
まず master ブランチに戻る
[root@gitlab dawn]#git checkout master
Switched to branch 'master'
[root@gitlab dawn]#git branch
  dev
* master
dev ブランチを master にマージする
[root@gitlab dawn]#git merge dev
Updating ae6585c..2694c7f
Fast-forward
 aa.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 aa.txt

6. 機能を作り終えたらすぐにブランチを削除する(リモートブランチより遅れるため)
[root@gitlab dawn]#git branch -d dev
Deleted branch dev (was 2694c7f).
[root@gitlab dawn]#git branch
* master

7. コンフリクトの解決
----------------------------------
ファイルの内容を変更しても必ずしもマージを妨げるわけではない
master ブランチのファイルを編集して bbbbb を追加する
[root@gitlab dawn]#vim b.txt 
aaaaa
bbbbb

1 つのコマンドで dev ブランチの作成と切り替えを同時に行える
[root@gitlab dawn]#git checkout -b dev
Switched to a new branch 'dev'
[root@gitlab dawn]#
[root@gitlab dawn]#
[root@gitlab dawn]#git branch
* dev
  master

b.txt の内容を編集する
[root@gitlab dawn]#vim b.txt 
aaaaa
bbccc

ローカルリポジトリにコミットする
[root@gitlab dawn]#git add .
[root@gitlab dawn]#git commit -m "modifle bbbccc"

master ブランチに戻って dev ブランチをマージする
[root@gitlab dawn]#git checkout master
Switched to branch 'master'
[root@gitlab dawn]#git branch
  dev
* master
[root@gitlab dawn]#cat b.txt 
aaaaa
bbbbb
[root@gitlab dawn]#git merge dev
Updating f56faba..dc17f6d
Fast-forward
 b.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@gitlab dawn]#cat b.txt
aaaaa
bbccc
------------------------------------------------------

コンフリクトが発生するケース
dev ブランチを作成する
[root@gitlab dawn]#git branch dev

master ブランチで b.txt の 3 行目を編集する
[root@gitlab dawn]#cat b.txt
aaaaa
iiiii
bbbbb
[root@gitlab dawn]#git commit -am "modifile b.txt bbbbb"
[master ae4ab87] modifile b.txt bbbbb
 1 file changed, 1 insertion(+)

dev ブランチに切り替えて b.txt の 3 行目を編集する
[root@gitlab dawn]#cat b.txt
aaaaa
iiiii
ccccc

ローカルリポジトリにコミットする
[root@gitlab dawn]#git commit -am "modifile b.txt ccccc"
[dev 925c06c] modifile b.txt ccccc
 1 file changed, 1 insertion(+)

master ブランチに切り替えてブランチをマージする
[root@gitlab dawn]#git checkout master
Switched to branch 'master'

[root@gitlab dawn]#git branch
  dev
* master
[root@gitlab dawn]#git merge dev
Auto-merging b.txt
CONFLICT (content): Merge conflict in b.txt
Automatic merge failed; fix conflicts and then commit the result.

ファイルを確認すると競合が表示される
[root@gitlab dawn]#cat b.txt
aaaaa
iiiii
<<<<<<< HEAD
bbbbb
=======
ccccc
>>>>>>> dev

手動で競合を解決し、片方だけ、あるいは両方を残す
[root@gitlab dawn]#vim b.txt 
aaaaa
iiiii
bbbbb
ccccc

[root@gitlab dawn]#git commit -am "merge dev"
[master 325eef2] merge dev
[root@gitlab dawn]#
[root@gitlab dawn]#
[root@gitlab dawn]#git status
# On branch master
nothing to commit, working directory clean
[root@gitlab dawn]#cat b.txt
aaaaa
iiiii
bbbbb
ccccc

マージ後に dev ブランチを削除する
[root@gitlab dawn]#git branch -d dev
Deleted branch dev (was 925c06c).