前言
學習筆記 -- 使用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
(這里直接借鑒下大牛的代碼)
接著可以在用戶主目錄里找到.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
這里我已經設置過了
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上面也建一個項目
在Repository name填上我們的項目名字,description隨便填,別的大家可以暫時參照我的選擇勾選。
然后會生成項目
(其實他有提示你接下來該怎么做)
然后把遠程項目和本地庫關聯
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的不同