记录一次使用terminal进行git管理与提交到Github的过程

Posted gdwkong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记录一次使用terminal进行git管理与提交到Github的过程相关的知识,希望对你有一定的参考价值。

1、环境的构建:

使用Mac系统自带的Git进行版本管理存在,Git是系统的Xcode集成的

查看版本的命令:

1 $ git --version
2 git version 2.14.3 (Apple Git-98)

查看git的安装目录:

1 $ which git
2 /usr/bin/git

2、常用命令

 1 #使用git命令初始化文件夹,即创建git本地仓库
 2 $ git init
 3 Initialized empty Git repository in /Users/Mac/Desktop/myapp/.git/
 4 #配置全局变量
 5 $ git config --global user.name "***"
 6 $ git config --global user.email \'****@qq.com\'
 7 #将index.jsp 添加到git仓库中
 8 $ git add index.jsp
 9 #获取git本地仓库状态
10 $ git status
11 #使用通配符添加文件到本地仓库,此命令表示将需要添加的本地仓库的所有html文件添加到本地仓库
12 $ git add *.html
13 #此命令表示将需要添加的本地仓库的所有文件添加到本地仓库
14 $ git add .
15 #从git本地仓库中移除index.jsp文件
16 $ git rm --cached index.jsp
17 #将已添加到本地仓库的文件全部提交,当输入此命令时,会切换到一个编辑页面,此时需输入此次提交的备注信息,保存退出即可
18 $ git commit
19 #此命令中参数\'-m\'后添加备注信息
20 $ git commit -m \'another change\'
21 #创建.gitignore文件,该文件的作用是忽略文件中的指定文件,即不用添加提交的文件
22 $ touch .gitignore
23 #演示创建分支的方法,在分支下修改文件,除非合并,否则不会实质性修改主线的内容
24 $ git branch login
25 #切换到master角色下
26 $ git checkout master
27 #在master角色下合并分支命令
28 $ git merge login
29 #以下演示同步到GitHub上
30 #查看是否已有远程连接
31 $ git remote
32 #创建远程连接命令,"***"此内容需要在GitHub指定查看上复制
33 $ git remote add origin git@github.com:****/***.git
34 #将本地仓库中的文件推送到GitHub仓库上
35 $ git push -u origin master
36 #由于之前已建立连接,故此时只需直接即可push
37 $ git push
38 #从GitHub仓库中克隆项目到本地仓库
39 $ git clone **@github.com:***/homework.git

3、示例

  1 Last login: Fri Mar  9 16:47:24 on ttys000
  2 #配置git,生成公钥密钥(输完命令需要敲四次空格,后三次为提示一行敲一次),运行完之后会在~/.shh文件内生成id_rsa和id_rsa.pub两个文件,
  3 #复制id_rsa.pub内容复制黏贴到GitHub的指定位置,此操作用于git连接github网站,获取读写权限
  4 Mac$ ssh-keygen -t rsa -b 4096 -C "1037345628@qq.com"
  5 Generating public/private rsa key pair.
  6 Enter file in which to save the key (/Users/Mac/.ssh/id_rsa): 
  7 Enter passphrase (empty for no passphrase): 
  8 Enter same passphrase again: 
  9 Your identification has been saved in /Users/Mac/.ssh/id_rsa.
 10 Your public key has been saved in /Users/Mac/.ssh/id_rsa.pub.
 11 
 12 --------------------------------------
 13 
 14 #使用git进行项目版本管理.
 15 
 16 #1.创建myapp,并切换至该目录下
 17 :~ Mac$ cd /Users/Mac/Desktop/myapp 
 18 #创建index.jsp app.jsp文件
 19 :myapp Mac$ touch index.jsp
 20 :myapp Mac$ touch app.jsp
 21 #使用git命令初始化文件夹,即创建git本地仓库
 22 :myapp Mac$ git init
 23 Initialized empty Git repository in /Users/Mac/Desktop/myapp/.git/
 24 #配置全局变量
 25 :myapp Mac$ git config --global user.name "****"
 26 :myapp Mac$ git config --global user.email \'****@qq.com\'
 27 #将index.jsp 添加到git仓库中
 28 :myapp Mac$ git add index.jsp
 29 #获取git本地仓库状态
 30 :myapp Mac$ git status
 31 #表示正在以master(项目创建者或主线管理)角色操作
 32 On branch master
 33 #表示没有提交过
 34 No commits yet
 35 
 36 Changes to be committed:
 37   (use "git rm --cached <file>..." to unstage)
 38     #新文件,表示需要提交的文件
 39     new file:   index.jsp
 40 
 41 Untracked files:
 42   (use "git add <file>..." to include in what will be committed)
 43 
 44     app.jsp
 45 
 46 #从git本地仓库中移除index.jsp文件
 47 :myapp Mac$ git rm --cached index.jsp
 48 rm \'index.jsp\'
 49 #获取git本地仓库状态
 50 :myapp Mac$ git status
 51 On branch master
 52 
 53 No commits yet
 54 
 55 Untracked files:
 56   (use "git add <file>..." to include in what will be committed)
 57     #表示需要添加git仓库的文件
 58     app.jsp
 59     index.jsp
 60 #没有可提交的文件
 61 nothing added to commit but untracked files present (use "git add" to track)
 62 #创建index.html文件
 63 :myapp Mac$ touch index.html
 64 #查看git本地仓库状态
 65 :myapp Mac$ git status
 66 On branch master
 67 
 68 No commits yet
 69 
 70 Untracked files:
 71   (use "git add <file>..." to include in what will be committed)
 72 
 73     app.jsp
 74     index.html
 75     index.jsp
 76 
 77 nothing added to commit but untracked files present (use "git add" to track)
 78 #使用通配符添加文件到本地仓库,此命令表示将需要添加的本地仓库的所有html文件添加到本地仓库
 79 :myapp Mac$ git add *.html
 80 :myapp Mac$ git status
 81 On branch master
 82 
 83 No commits yet
 84 
 85 Changes to be committed:
 86   (use "git rm --cached <file>..." to unstage)
 87 
 88     new file:   index.html
 89 
 90 Untracked files:
 91   (use "git add <file>..." to include in what will be committed)
 92 
 93     app.jsp
 94     index.jsp
 95 #此命令表示将需要添加的本地仓库的所有文件添加到本地仓库
 96 :myapp Mac$ git add .
 97 :myapp Mac$ git status
 98 On branch master
 99 
100 No commits yet
101 
102 Changes to be committed:
103   (use "git rm --cached <file>..." to unstage)
104 
105     new file:   app.jsp
106     new file:   index.html
107     new file:   index.jsp
108 
109 
110 #修改index.jsp 文件
111 :myapp Mac$ git status
112 On branch master
113 
114 No commits yet
115 
116 Changes to be committed:
117   (use "git rm --cached <file>..." to unstage)
118 
119     new file:   app.jsp
120     new file:   index.html
121     new file:   index.jsp
122 
123 Changes not staged for commit:
124   (use "git add <file>..." to update what will be committed)
125   (use "git checkout -- <file>..." to discard changes in working directory)
126     #表示该文件已被修改过,需要重新添加本地仓库
127     modified:   index.html
128 
129 :myapp Mac$ git add .
130 :myapp Mac$ git status
131 On branch master
132 
133 No commits yet
134 
135 Changes to be committed:
136   (use "git rm --cached <file>..." to unstage)
137 
138     new file:   app.jsp
139     new file:   index.html
140     new file:   index.jsp
141 
142 #将已添加到本地仓库的文件全部提交,当输入此命令时,会切换到一个编辑页面,此时需输入此次提交的备注信息,保存退出即可
143 :myapp Mac$ git commit
144 [master (root-commit) f81a0ad] first commit;
145  3 files changed, 10 insertions(+)
146  create mode 100644 app.jsp
147  create mode 100644 index.html
148  create mode 100644 index.jsp
149 :myapp Mac$ git status
150 On branch master
151 #表示没有需要提交的文件
152 nothing to commit, working tree clean
153 #修改app.jsp文件,获取本地仓库状态
154 :myapp Mac$ git status
155 On branch master
156 Changes not staged for commit:
157   (use "git add <file>..." to update what will be committed)
158   (use "git checkout -- <file>..." to discard changes in working directory)
159       #表示需要添加的文件
160     modified:   app.jsp
161 #表没有已经改变的已添加文件
162 no changes added to commit (use "git add" and/or "git commit -a")
163 :myapp Mac$ git add .
164 :myapp Mac$ git status
165 On branch master
166 Changes to be committed:
167   (use "git reset HEAD <file>..." to unstage)
168     #需要提交的已经添加到本地仓库的修改文件
169     modified:   app.jsp
170 #提交,输入备注信息
171 :myapp Mac$ git commit
172 #change is app.jsp 添加备注信息
173 [master 24e3cd2] changed is app.jsp
174  1 file changed, 1 insertion(+)
175 
176 :myapp Mac$ git commit
177 On branch master
178 nothing to commit, working tree clean
179 
180 #创建log.txt文件
181 :myapp Mac$ git status
182 On branch master
183 Untracked files:
184   (use "git add <file>..." to include in what will be committed)
185 
186     log.txt
187 
188 nothing added to commit but untracked files present (use "git add" to track)
189 #创建.gitignore文件,该文件的作用是忽略文件中的指定文件,即不用添加提交的文件
190 :myapp Mac$ touch .gitignore
191 #在.gitignore文件输入log.txt
192 :myapp Mac$ git status
193 On branch master
194 Untracked files:
195   (use "git add <file>..." to include in what will be committed)
196       #需要添加文件,已忽略log.txt文件
197     .gitignore
198 
199 nothing added to commit but untracked files present (use "git add" to track)
200 :myapp Mac$ git add .
201 :myapp Mac$ git status
202 On branch master
203 Changes to be committed:
204   (use "git reset HEAD <file>..." to unstage)
205 
206     new file:   .gitignore
207 
208 #创建文件夹dir1,dir2
209 :myapp Mac$ git status
210 On branch master
211 Changes to be committed:
212   (use "git reset HEAD <file>..." to unstage)
213 
214     new file:   .gitignore
215 
216 Untracked files:
217   (use "git add <file>..." to include in what will be committed)
218 
219     dir1/
220     dir2/
221 
222 #在.gitignore中添加"/dir1"内容
223 :myapp Mac$ git status
224 On branch master
225 Changes to be committed:
226   (use "git reset HEAD <file>..." to unstage)
227 
228     new file:   .gitignore
229 
230 Changes not staged for commit:
231   (use "git add <file>..." to update what will be committed)
232   (use "git checkout -- <file>..." to discard changes in working directory)
233 
234     modified:   .gitignore
235 
236 Untracked files:
237   (use "git add <file>..." to include in what will be committed)
238 
239     dir2/
240 
241 :myapp Mac$ git add .
242 #此命令中参数\'-m\'后添加备注信息
243 :myapp Mac$ git commit -m \'another change\'
244 [master 50d5a2f] another change
245  2 files changed, 3 insertions(+)
246  create mode 100644 .gitignore
247  create mode 100644 dir2/app2.js
248 
249 #演示创建分支的方法,在分支下修改文件,除非合并,否则不会实质性修改主线的内容
250 #创建login分支命令 
251 :myapp Mac$ git branch login
252 :myapp Mac$ git status
253 On branch master
254 nothing to commit, working tree clean
255 #切换到login分支下
256 :myapp Mac$ git checkout login
257 Switched to branch \'login\'
258 
259 #在分支下创建文件
260 :myapp Mac$ touch login.html
261 :myapp Mac$ git status
262 On branch login
263 Changes not staged for commit:
264   (use "git add <file>..." to update what will be committed)
265   (use "git checkout -- <file>..." to discard changes in working directory)
266 
267     modified:   dir2/app2.js
268     modified:   index.html
269 
270 Untracked files:
271   (use "git add <file>..." to include in what will be committed)
272 
273     login.html
274 
275 no changes added to commit (use "git add" and/or "git commit -a")
276 :myapp Mac$ git commit -m \'login form\'
277 On branch login
278 Changes not staged for commit:
279     modified:   dir2/app2.js
280     modified:   index.html
281 
282 Untracked files:
283     login.html
284 
285 no changes added to commit
286 :myapp Mac$ git add .
287 :myapp Mac$ git commit -m \'login form\'
288 [login 57202e2] login form
289  3 files changed, 12 insertions(+), 1 deletion(-)
290  create mode 100644 login.html
291 :myapp Mac$ git status
292 On branch login
293 nothing to commit, working tree clean
294 
295 #切换到master角色下
296 :myapp Mac$ git checkout master
297 Switched to branch \'master\'
298 
299 #在master角色下合并分支命令
300 :myapp Mac$ git merge login
301 Updating 50d5a2f..57202e2
302 Fast-forward
303  dir2/app2.js |  2 +-
304  index.html   |  1 +
305  login.html   | 10 ++++++++++
306  3 files changed, 12 insertions(+), 1 deletion(-)
307  create mode 100644 login.html
308 
309 #以下演示同步到GitHub上
310 #查看是否已有远程连接
311 :myapp Mac$ git remote
312 #创建远程连接命令,"git@github.com:****/homework.git"此内容需要在GitHub指定查看上复制
313 :myapp Mac$ git remote add origin git@github.com:*****/homework.git
314 #查看是否已有远程连接
315 :myapp Mac$ git remote
316 origin
317 :myapp Mac$ git push -u origin master
318 git@github.com: Permission denied (publickey).
319 fatal: Could not read from remote repository.
320 
321 Please make sure you have the correct access rights
322 and the repository exists.
323 :myapp Mac$ git remote add origin https://github.com/*****/homework.git
324 fatal: remote origin already exists.
325 
326 #将本地仓库中的文件推送到GitHub仓库上
327 WGB:myapp Mac$ git push -u origin master
328 Counting objects: 18, done.
329 Delta compression using up to 4 threads.
330 Compressing objects: 100% (11/11), done.
331 Writing objects: 100% (18/18), 1.46 KiB | 213.00 KiB/s, done.
332 Total 18 (delta 3), reused 0 (delta 0)
333 remote: Resolving deltas: 100% (3/3), done.
334 To github.com:gdwkong/homework.git
335  * [new branch]      master -> master
336 Branch master set up to track remote branch master from origin.
337 
338 #切换到master角色下
339 :myapp Mac$ git checkout master
340 A    README.md
341 Already on \'master\'
342 Your branch is up-to-date with \'origin/master\'.
343 #创建文件README.md文件
344 :myapp Mac$ touch README.md
345 :myapp Mac$ git add .
346 :myapp Mac$ git status
347 On branch master
348 Your branch is up-to-date with \'origin/master\'.
349 
350 Changes to be committed:
351   (use "git reset HEAD <file>..." to unstage)
352 
353     new file:   README.md
354 
355 :myapp Mac$ git commit -m \'README.md\'
356 [master 2dcd73c] README.md
357  1 file changed, 1 insertion(+)
358  create mode 100644 README.md
359 
360 #由于之前已建立连接,故此时只需直接即可push
361 WGB:myapp Mac$ git push
362 
363 Counting objects: 3, done.
364 Delta compression using up to 4 threads.
365 Compressing objects: 100% (2/2), done.
366 Writing objects: 100% (3/3), 283 bytes | 283.00 KiB/s, done.
367 Total 3 (delta 1), reused 0 (delta 0)
368 remote: Resolving deltas: 100% (1/1), completed with 1 local object.
369 To github.com:gdwkong/homework.git
370    57202e2..2dcd73c  master -> master
371 
372 #从GitHub仓库中克隆项目到本地仓库
373 :myapp Mac$ cd /Users/Mac/Desktop/myapp2 
374 :myapp2 Mac$ git命令与使用_learn_git_branching_notes

git命令与使用_learn_git_branching_notes

#yyds干货盘点#Git学习-如何修改已经提交的记录

版本管理·玩转git(日志查看与版本切换)

Git学习

Git学习