1. 程式人生 > >mac使用git管理Github

mac使用git管理Github

pop penssh terminal them posit blog don ucc text

工欲善其事,必先利其器。

在OS X Yosemite 10.10.3安裝最新版本號Xcode。在terminal下能夠發現git已經被安裝。

~ mesut$ git --version
git version 2.3.2 (Apple Git-55)

之前就已經註冊而且使用Github了,只是一直都是在window 系統下遠程管理。

如今開始設置Mac管理Github,有一點須要知道的是本地的git倉庫和Githubserver之間是通過ssh加密的。

在終端運行

ozil:tmp mesut$ ssh -v
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
           [-D [bind_address:]port] [-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]
           [-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

ozil:tmp mesut$ cd ~
ozil:~ mesut$ pwd
/Users/mesut
ozil:~ mesut$ cd .ssh
-bash: cd: .ssh: No such file or directory
ozil:~ mesut$ 

進入當前的用戶文件夾。波浪線表示的是當前文件夾。

推斷是否已經安裝了.ssh,避免默認安裝會覆蓋之前安裝的。

明顯當前文件夾沒有該文件

運行創建 ssh key

ssh-keygen -t rsa -C "[email protected]
/* */"(你的Github登陸名)

接著都是回車,選擇默認的文件夾,默認的password就可以

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/mesut/.ssh/id_rsa):     
Created directory '/Users/mesut/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/mesut/.ssh/id_rsa.
Your public key has been saved in /Users/mesut/.ssh/id_rsa.pub.


接著能夠在用戶主文件夾裏找到.ssh文件夾,裏面有id_rsaid_rsa.pub兩個文件,這兩個就是SSH Key的秘鑰對

ozil:~ mesut$ cd .ssh
ozil:.ssh mesut$ ls
id_rsa		id_rsa.pub


2:在Github設置ssh key

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

技術分享



技術分享


title:能夠順便填名字

key:在Key文本框裏粘貼id_rsa.pub文件的內容

點擊add key 配置完畢

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

史蒂夫

ozil:.ssh mesut$ ssh -T [email protected]
The authenticity of host 'github.com (xxx)' can't be established.
RSA key fingerprint is xxx.
Are you sure you want to continue connecting (yes/no)? yes
第一次鏈接Github,會有一個確認。須要你確認GitHub的Key的指紋信息是否真的來自GitHub的server,輸入yes回車就可以。

Warning: Permanently added ‘github.com,xxx‘ (RSA) to the list of known hosts.Hi tanchongshi! You‘ve successfully authenticated, but GitHub does not provide shell access.

4:使用git在本地建立的項目更新到Github

ozil:githubspace mesut$ mkdir hellogithub
ozil:githubspace mesut$ cd hellogithub/
ozil:hellogithub mesut$ ls -ah
.	..
ozil:hellogithub mesut$ git init
Initialized empty Git repository in /Users/mesut/Documents/githubspace/hellogithub/.git/
ozil:hellogithub mesut$ ls -ah
.	..	.git
ozil:hellogithub mesut$ touch newfile
ozil:hellogithub mesut$ ls
newfile
ozil:hellogithub mesut$ git add newfile 
ozil:hellogithub mesut$ git commit -m 'first commit'
[master (root-commit) 2295d3e] first commit
 Committer: >v< <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 newfile

首先在本地初始化一個git倉庫

因為之前沒有配置username。所以首次commit會有提示。自己主動建立

設置方式

$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"


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

技術分享

在Repository name填上我們的項目名字。description隨便填,別的默認。

然後會生成項目技術分享

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

ozil:hellogithub mesut$ git remote add origin [email protected]:tanchongshi/hellogithub.git
ozil:hellogithub mesut$ git push -u origin master
Warning: Permanently added the RSA host key for IP address 'XXX' to the list of known hosts.
Counting objects: 3, done.
Writing objects: 100% (3/3), 217 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:tanchongshi/hellogithub.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.


https://github.com/tanchongshi/hellogithub
技術分享

so far so good~


mac使用git管理Github