forked from michaelliao/learngit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaesarNapoleon_GITNote.txt
280 lines (193 loc) · 6.83 KB
/
CaesarNapoleon_GITNote.txt
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
#登录自己的Github账号
#测试utf-8编码在pull
$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"
#创建自己需要管理的目录 注意进入D盘使用 cd D:
$ mkdir learngit
$ cd learngit
$ pwd
/Users/michael/learngit
#git add xxx 添加文件到git仓库
$ git add readme.txt
#git commit 把文件提交到仓库
$ git commit -m "wrote a readme file"
[master (root-commit) eaadf4e] wrote a readme file
1 file changed, 2 insertions(+)
create mode 100644 readme.txt
# 可以多次提交文件
$ git add file1.txt
$ git add file2.txt file3.txt
$ git commit -m "add 3 files."
#git status 可以查看结果
$ 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: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
#git diff xxx 可以查看这个文件做过哪些修改
$ git diff readme.txt
diff --git a/readme.txt b/readme.txt
index 46d49bf..9247db6 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
-Git is a version control system.
+Git is a distributed version control system.
Git is free software.
# git log 可以查看所有的修改
$ git log
commit 1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master)
Author: Michael Liao <[email protected]>
Date: Fri May 18 21:06:15 2018 +0800
append GPL
commit e475afc93c209a690c39c13a46716e8fa000c366
Author: Michael Liao <[email protected]>
Date: Fri May 18 21:03:36 2018 +0800
add distributed
commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0
Author: Michael Liao <[email protected]>
Date: Fri May 18 20:59:18 2018 +0800
wrote a readme file
#可以使用 --pretty=oneline去精简输出信息
$ git log --pretty=oneline
1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master) append GPL
e475afc93c209a690c39c13a46716e8fa000c366 add distributed
eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0 wrote a readme file
#版本回退 git reset
$ git reset --hard HEAD^
HEAD is now at e475afc add distributed
#可以使用那串16进制数精确回归版本
$ git reset --hard 1094a
HEAD is now at 83b0afe append GPL
#可以使用 git reflog查看自已使用了哪些命令
$ git reflog
e475afc HEAD@{1}: reset: moving to HEAD^
1094adb (HEAD -> master) HEAD@{2}: commit: append GPL
e475afc HEAD@{3}: commit: add distributed
eaadf4e HEAD@{4}: commit (initial): wrote a readme file
#查看工作区和版本库中的最新情况
git diff HEAD -- readme.txt
#git checkout 可以丢弃原来的修改
$ git checkout -- readme.txt
# add 了自己的文件 恩也可以删除
$ git reset HEAD readme.txt
Unstaged changes after reset:
M readme.txt
#现在你有两个选择,一是确实要从版本库中删除该文件,那就用命令git rm删掉,并且git commit:
#错删可以使用git checkout -- test.txt恢复错删文件
$ git rm test.txt
rm 'test.txt'
$ git commit -m "remove test.txt"
[master d46f35e] remove test.txt
1 file changed, 1 deletion(-)
delete mode 100644 test.txt
#连接远程仓库步骤
1.创建sshkey(查看有无.ssh文件夹)
$ ssh-keygen -t rsa -C "[email protected]"
如果一切顺利的话,可以在用户主目录里找到.ssh目录,里面有id_rsa和id_rsa.pub两个文件,
这两个就是SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,
id_rsa.pub是公钥,可以放心地告诉任何人。
2.用网页版Github 添加此电脑的ssh公钥
3.用网页版Github 创建一个新的 Create a new repo 新的仓库
4.建立本地与网页仓库的连接
$ git remote add origin [email protected]:michaelliao/learngit.git
5.推送本地分支(注意本地分支与网页分支名字一致)
$ git push -u origin master
6.推送过一次后可以使用精简命令
$ git push origin master
7.远程clone 一个建好的仓库
$ git clone [email protected]:michaelliao/gitskills.git
Cloning into 'gitskills'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 3
Receiving objects: 100% (3/3), done.
#分支管理
#创建一个新的分支dev
git checkout = $ git branch dev
$ git checkout dev
#命令加上-b参数表示创建并切换,
#相当于以下两条命令
git branch 可查看当前分支情况
#现在dev分支上修改后可以合并到master分支上
使用命令$ git merge dev
Updating d46f35e..b17d20e
Fast-forward
readme.txt | 1 +
1 file changed, 1 insertion(+)
#现在合并结束之后可以删除dev分支,因为主分支
#已经改变了他
使用git branch -d dev
$ git branch -d dev
Deleted branch dev (was b17d20e).
#合并分支会遇到的问题
#两个分支都有内容提交不统一,
#需要统一后才能继续前进
git log --graph命令可以看到分支合并图。
#git merge 使用Fast forward模式合并这个会丢失分支信息
#可以禁止使用这个模式进行合并
#merge with no-ff
$ git merge --no-ff -m "merge with no-ff" dev
Merge made by the 'recursive' strategy.
readme.txt | 1 +
1 file changed, 1 insertion(+)
#使用三个命令
$ git log --graph --pretty=oneline --abbrev-commit
* e1e9c68 (HEAD -> master) merge with no-ff
|\
| * f52c633 (dev) add merge
|/
* cf810e4 conflict fixed
#bug 分支管理
#保存工作区
$ git stash
Saved working directory and index state
WIP on dev: f52c633 add merge
#查看缓存的工作区
$ git stash list
stash@{0}: WIP on dev: f52c633 add merge
#一是用git stash apply恢复,
#但是恢复后,stash内容并不删除,
#你需要用git stash drop来删除;
#使用git stash pop
$ git stash pop
On branch dev
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: hello.py
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: readme.txt
#feature分支
#如果要丢弃一个没有被合并过的分支
#可以通过
#git branch -D <name>强行删除。
#查看远程库信息
git remote
#查看详细信息
git remote -v
#git标签管理
git tag v1.0
#查看标签
git tag
git show v1.0
#查看详细信息
#还可以创建带有说明的标签,
#用-a指定标签名,-m指定说明文字:
$ git tag -a v0.1 -m "version 0.1 released" 1094adb
#打错可删除本地标签
$ git tag -d v0.1
Deleted tag 'v0.1' (was f15b0dd)
#推送标签
$ git push origin v1.0
#一次推送所有标签
$ git push origin --tags
#如果标签已经推送到远程,要删除远程标签就麻烦一点,先从本地删除:
$ git tag -d v0.9
Deleted tag 'v0.9' (was f52c633)
#然后,从远程删除。删除命令也是push,但是格式如下:
$ git push origin :refs/tags/v0.9
To github.com:michaelliao/learngit.git
- [deleted] v0.9