【Mac系統 + Git】之上傳項目代碼到github上以及刪除某個文件夾


之前做開發的時候,用過一段時間git代碼管理工具,用命令行操作感覺十分高大上,今天我想從頭總結一篇Mac系統下如何利用git上傳代碼到github上的學習。

目錄

 

一、安裝Git                  

參考文章:《Mac下使用Git上傳代碼到Github倉庫

下載地址:https://git-scm.com/download/mac 

下載后為.dmg文件,解壓后雙擊安裝.pkg文件

輸入指令:

test:~ zhan$ git --version

 

git version 2.18.0

二、創建.ssh文件                 返回目錄

打開終端,輸入下面指令,查看.ssh是否存在

test:~ zhan$ cd ~/.ssh

 

test:.ssh zhan$ 

 

test:~ zhan$ cd .ssh/

 

test:.ssh zhan$ ls

 

known_hosts

 

查看文件夾下的文件,只有known_hosts,感覺少了點什么

如果沒有.ssh文件夾,請參考《Mac如何添加生成ssh》 、《Mac生成添加ssh公鑰

假設你在Github注冊賬號為: xxxx@xxx.com 
Terminal中運行

//默認直接按 回車 就可以了
test:.ssh zhan$ ssh-keygen -t rsa -C xxx@xxx.com Generating public/private rsa key pair. Enter file in which to save the key (/Users/zhan/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /Users/zhan/.ssh/id_rsa. Your public key has been saved in /Users/zhan/.ssh/id_rsa.pub. The key fingerprint is: SHA256:xCNUrcwiwpMVb2Se4zucMD0V9+SX+WeZ4/TdAdX1Spk xxx@xxx.com The key's randomart image is: +---[RSA 2048]----+ | ..+.o.. . .+| | .* o o.+ .=o| | . o Bo=. o.E .| | = .+.=+. o.oo| | oo.+.S .*+| | + + o.B| | = .+| | . | | | +----[SHA256]-----+

輸入命令:ls,查看.ssh下的文件

id_rsa id_rsa.pub known_hosts

 

多出兩個文件,.pub是公鑰,另一個是密鑰。

三、Github賬號中添加Key            返回目錄

點擊【頭像】 ->Settings ->SSH and GPG keys ->

點擊【New SSH key】按鈕

輸入Title、Key

 

執行下面的命令行,直接復制文件里的內容:

pbcopy < ~/.ssh/id_rsa.pub

或查看:

cat ~/.ssh/id_rsa.pub

復制到里面后,點擊【Add SSH key】按鈕。

 

下面繼續確認登錄github的密碼:

 如果添加Key成功的話,如下圖所示,同時你也會在郵箱里收到一個提醒郵件,內容是你添加了一個Key. 

 

 

四、創建版本庫Repository            返回目錄

首先,返回到主頁,www.github.com 

 

 進入到了 “Create a New Repository”頁面: 

 緊接着按照以下步驟進行本地倉庫的創建及代碼上傳。打開終端,輸入以下命令:

$ echo "TestRepository" >> README.md  //新建一個README文檔並添加內容,若上一步勾選了創建README.md,提交時導致沖突  
$ git init //初始化本地倉庫  
$ git add README.md   //添加剛剛創建的README文檔  
$ git commit -m "你的注釋...."   //提交到本地倉庫,並寫一些注釋  
$ git remote add origin git@github.com:yourname/xxxx.git  
//連接遠程倉庫並建了一個名叫:origin的別名,當然可以為其他名字,但是origin一看就知道是別名,youname記得替換成你的用戶名  
$ git push -u origin master                              
//將本地倉庫的文件提交到別名為origin的地址的master分支下,-u為第一次提交,需要創建master分支,下次就不需要了 

  或者:

//創建 README.md 文件, 並向里面寫入`This Is My First Testing Description....`字符串。

echo "# This Is My First Testing Description...." >> README.md
git init
git add README.md
git commit -m "first commit" //commit備注
git remote add origin https://github.com/imthinktwice/TestRepository.git
git push -u origin master

但是執行git commit -m "first commit"報錯:

test:Git zhan$ git commit -m "first commit"

 

*** Please tell me who you are.

 

Run

 

  git config --global user.email "you@example.com"

  git config --global user.name "Your Name"

 

to set your account's default identity.

Omit --global to set the identity only in this repository.

 

fatal: unable to auto-detect email address (got 'zhan@test.(none)')

 

參考:《git fatal: unable to auto-detect email address

 

 

解決辦法,輸入指令:

 

git config --global user.email "you@example.com"

#查看本地配置
git config --local -l
#編輯config文件
git config --local -e

如何在終端編輯文件,參照:《Mac Git 配置全局gitconfig

 再執行下面:

#再執行命令
git commit -m "first commit"
#顯示結果 test:Git zhan$ git commit -m "first commit" [master (root-commit) 4d3f7a6] first commit 1 file changed, 1 insertion(+) create mode 100644 README.md

commit成功!!

再繼續執行命令:

git remote add origin git@github.com:yourname/xxxx.git 

git push -u origin master
=====================================================
#result:
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 221 bytes | 221.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/Owen-ET/TestRepository/pull/new/master
remote: 
To github.com:Owen-ET/TestRepository.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

github中創建成功!!!!

再返回github網址查看:

 

 看看,上傳github上成功!! 

 五、上傳更新新的代碼到github上        返回目錄

首先在之前上傳的項目中,新建一個子項目,如圖

 

輸入命令:git status

查看項目下文件狀態,如下:

On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.DS_Store
	python_stu/

nothing added to commit but untracked files present (use "git add" to track)

其中“python_stu/”文件夾是我新建的,上傳到github需要add添加

所以執行命令:

#添加文件夾
git add python_stu/

#提交文件夾,並注釋
git commit -m "上傳py文件2018-09-19"

#繼續查看狀態,python_stu/文件夾已添加
git status

On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.DS_Store

nothing added to commit but untracked files present (use "git add" to track)

繼續執行

test:Git zhan$ git pull   #同步代碼

Already up to date.

test:Git zhan$ git push origin   #把代碼推到服務器上

Enumerating objects: 5, done.

Counting objects: 100% (5/5), done.

Delta compression using up to 4 threads.

Compressing objects: 100% (3/3), done.

Writing objects: 100% (4/4), 866 bytes | 866.00 KiB/s, done.

Total 4 (delta 0), reused 0 (delta 0)

To github.com:Owen-ET/TestRepository.git

   4d3f7a6..9110914  master -> master

上面可參考文章:《mac下如何把項目提交、更新到gitHub上

返回到github上,如圖:

 

六、刪除github中某個文件夾           返回目錄

只需要一下幾步就可以完成刪除

# 刪除文件夾
git rm -r --cached python_stu/

# 提交,添加操作說明
git commit -m '刪除stu文件夾'

# 將本次更改更新到github項目上去
git push -u origin master       

參考文章:《刪除github中某個文件夾

匯總:

GitHub上傳項目
126,(66)

先cd到git文件夾下,把新建的項目復制到git下
再查看狀態:git status

#添加文件夾
git add python_stu/

#提交文件夾,並注釋
git commit -m "上傳py文件2018-09-19"

#繼續查看狀態,python_stu/文件夾已添加
git status

test:Git zhan$ git pull   #同步代碼

test:Git zhan$ git push origin   #把代碼推到服務器上

====================================

刪除github上的文件

# 刪除target文件夾
git rm -r --cached python_stu/

# 提交,添加操作說明
git commit -m '刪除stu文件夾'

# 將本次更改更新到github項目上去
git push -u origin master               

 

 七、附錄:                  返回目錄

github常見操作和常見錯誤及其解決辦法

如何把本地文件上傳到github上(MAC版)

mac下如何上傳代碼到github(親測有效)

================擴展:==================================

.git目錄看不到怎么辦,參考:《Mac上如果看不到.git目錄的解決方法

 

.git路徑為就是自己初始化init創建git時的路徑!

 

=======================================================

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM