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.查看git分支 *表示当前所在的分支
[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.冲突合并
----------------------------------
修改文件的内容不影响分支的合并
修改主干分支的文件增加bbbbb
[root@gitlab dawn]#vim b.txt
aaaaa
bbbbb
使用一条命令可以直接创建并切换到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).
|