1. 程式人生 > >記錄一次使用terminal進行git管理與提交到Github的過程

記錄一次使用terminal進行git管理與提交到Github的過程

忽略文件 pack com res ota pac sed clone fatal

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 [email protected]:****/***.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 "[email protected]"
  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 #創建遠程連接命令,"[email protected]:****/homework.git"此內容需要在GitHub指定查看上復制
313 :myapp Mac$ git remote add origin [email protected]:*****/homework.git
314 #查看是否已有遠程連接
315 :myapp Mac$ git remote
316 origin
317 :myapp Mac$ git push -u origin master
318 [email protected]: 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$ ls
375 :myapp2 Mac$ git clone [email protected]:***/homework.git
376 Cloning into homework...
377 remote: Counting objects: 21, done.
378 remote: Compressing objects: 100% (9/9), done.
379 Receiving objects: 100% (21/21), done.
380 Resolving deltas: 100% (4/4), done.
381 remote: Total 21 (delta 4), reused 21 (delta 4), pack-reused 0
382 $ 

4、提交後GitHub中相應的倉庫中的內容

技術分享圖片

記錄一次使用terminal進行git管理與提交到Github的過程