mac使用git管理Github

前言

學習筆記 -- 使用git管理Github

正文

最新版本Xcode,在terminal下可以發現git已經被安裝。

MacBookPro:~ huwenkuan$ git --version
...
git version 2.9.3 (Apple Git-75)

需要知道的是本地的git倉庫和Github服務器之間是通過ssh加密的。

在終端執行

MacBookPro:~ huwenkuan$ ssh -v
usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
           [-D [bind_address:]port] [-E log_file] [-e escape_char]
           [-F configfile] [-I pkcs11] [-i identity_file]
           [-L [bind_address:]port:host:hostport] [-l login_name] [-m mac_spec]
           [-O ctl_cmd] [-o option] [-p port]
           [-Q cipher | cipher-auth | mac | kex | key]
           [-R [bind_address:]port:host:hostport] [-S ctl_path] [-W host:port]
           [-w local_tun[:remote_tun]] [user@]hostname [command]

表示Mac已經安裝了ssh

1 接下來是創建SSH Key
(這里直接借鑒下大牛的代碼)

QQ20170217-2@2x.png

接著可以在用戶主目錄里找到.ssh目錄,里面有github_rsa和github_rsa.pub兩個文件,這兩個就是SSH Key的秘鑰對

MacBookPro:~ huwenkuan$ cd .ssh
MacBookPro:.ssh huwenkuan$ ls
github_rsa  github_rsa.pub  known_hosts

2 在Github設置ssh key

登陸Github, “Settings”->SSH keys->Add SSH key

QQ20170217-3@2x.png
QQ20170217-4@2x.png

這里我已經設置過了

title:可以隨便填名字

key:在Key文本框里粘貼github_rsa.pub文件的內容

點擊add key 配置完成

3 測試本地是否和Github連接上

MacBookPro:.ssh huwenkuan$ ssh -T git@github.com
Hi yun591855479! You've successfully authenticated, but GitHub does not provide shell access.

第一次鏈接Github,會有一個確認,需要你確認GitHub的Key的指紋信息是否真的來自GitHub的服務器,輸入yes回車即可。

一 使用git在本地建立的項目更新到Github
MacBookPro:github倉庫 huwenkuan$ mkdir hellogithub
MacBookPro:github倉庫 huwenkuan$ cd hellogithub/
MacBookPro:hellogithub huwenkuan$ ls -ah
.   ..
MacBookPro:hellogithub huwenkuan$ git init
Initialized empty Git repository in /Users/huwenkuan/Documents/github倉庫/hellogithub/.git/
MacBookPro:hellogithub huwenkuan$ ls -a
.   ..  .git
MacBookPro:hellogithub huwenkuan$ touch newfile
MacBookPro:hellogithub huwenkuan$ ls
newfile
MacBookPro:hellogithub huwenkuan$ git add newfile
MacBookPro:hellogithub huwenkuan$ git commit -m 'first commit'
[master (root-commit) e1e2cb8] first commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 newfile

配置本地用戶和郵箱

用戶名郵箱作用 : 我們需要設置一個用戶名 和 郵箱, 這是用來上傳本地倉庫到GitHub中, 在GitHub中顯示代碼上傳者;
使用命令 :

MacBookPro:hellogithub huwenkuan$ git config --global user.name xxx //設置用戶名 
MacBookPro:hellogithub huwenkuan$ git config --global user.email xxx@xx.com //設置郵箱

為了把本地庫推到遠程服務器,首先需要在Github上面也建一個項目

7FA56C39-2AEC-42B8-8910-5D686C2F5D80.png

在Repository name填上我們的項目名字,description隨便填,別的大家可以暫時參照我的選擇勾選。

然后會生成項目

QQ20170217-5@2x.png

(其實他有提示你接下來該怎么做)

然后把遠程項目和本地庫關聯

MacBookPro:hellogithub huwenkuan$ git remote add origin git@github.com:yun591855479/hellogithub.git
MacBookPro:hellogithub huwenkuan$ git push -u origin master

<tips:
“origin”經常被用作遠端倉庫別名,就因為 git clone 默認用它作為克隆自的鏈接的別名。
“git remote add xxx 遠程服務器倉庫地址aaa”:
該方法是關聯本地與服務器的倉庫,為服務器的倉庫取個別名叫xxx,一般情況下該別名就叫origin,此時他關聯的就是遠程服務器地址aaa這個倉庫。
而刪除某個別名用:“git remote rm tt” 刪除現存的某個別名tt,也就是斷開別名tt所對應的遠程倉庫的連接,不再需要它了、項目已經沒了,等等。
例如:git remote rm origin

可能會報以下錯

To github.com:yun591855479/hellogithub.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:yun591855479/hellogithub.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

這是本地沒有README文件造成的
現在去將該文件pull下來

MacBookPro:hellogithub huwenkuan$ git pull --rebase origin master
warning: no common commits
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (5/5), done.
From https://github.com/yun591855479/hellogithub
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
First, rewinding head to replay your work on top of it...
Applying: first commit

更新github代碼倉庫到本地。
使用下面的命令即可更新代碼

git pull origin master

然后查看是否已經同步下來

MacBookPro:hellogithub huwenkuan$ ls
LICENSE     README.md   newfile

然后再執行

MacBookPro:hellogithub huwenkuan$ git push -u origin master

<tips:想要與他人分享你牛鼻的提交,你需要將改動推送到遠端倉庫。 執行 git push [alias] [branch],就會將你的 [branch] 分支推送成為 [alias] 遠端上的 [branch] 分支。>

遇到的問題

當我再次關聯時會報錯

MacBookPro:hellogithub huwenkuan$ git remote add origin git@github.com:yun591855479/hellogithub.git
fatal: remote origin already exists.

我們需要移除

MacBookPro:hellogithub huwenkuan$ git remote rm origin

再執行

MacBookPro:hellogithub huwenkuan$ git remote add origin https://github.com/yun591855479/hellogithub.git


MacBookPro:hellogithub huwenkuan$ git push -u origin master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 263 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local objects.
To https://github.com/yun591855479/hellogithub.git
   546c5fe..f355d1c  master -> master
Branch master set up to track remote branch master from origin.
二 直接將github上的倉庫clone下來

直接在gitHub上創建倉庫,本地不創建。
在終端輸入’git clone’命令就能下載github的代碼了:
例如:我們在github上創建了一個倉庫test2
命令行執行:

MacBookPro:github倉庫 huwenkuan$ git clone https://github.com/yun591855479/test2.git
Cloning into 'test2'...
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (5/5), done.
Checking connectivity... done.
MacBookPro:github倉庫 huwenkuan$ ls

此時就能查看到test2

MacBookPro:github倉庫 huwenkuan$ cd test2
MacBookPro:test2 huwenkuan$ ls
LICENSE     README.md
補充

在下載下來的下面做一些修改并提交。
$ 進入剛下載的代碼倉庫:cd example/
$ 修改”README.md”文件:echo "hello github" > README.md
$ 添加修改的代碼:git add README.md
$ 查看修改的狀態:git status
$ 填寫提交信息:git commit -m "add test message"
$ 正式提交代碼:git push origin master

MacBookPro:hellogithub huwenkuan$ echo "#hello github">> README.md
MacBookPro:hellogithub huwenkuan$ vim README.md 
MacBookPro:hellogithub huwenkuan$ cat README.md 
# hellogithub
My pro first time visit you!
#hello github

我們可以通過vim查看README.md的內容,也可以通過cat 來直接查看。

參考文獻:

github常見操作和常見錯誤!
Git版本控制軟件結合GitHub從入門到精通常用命令學習手冊
mac使用git管理Github
如何解決failed to push some refs to git
git pull和git pull --rebase的不同

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,362評論 6 537
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,013評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,346評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,421評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,146評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,534評論 1 325
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,585評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,767評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,318評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,074評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,258評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,828評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,486評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,916評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,156評論 1 290
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,993評論 3 395
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,234評論 2 375

推薦閱讀更多精彩內容