git常用配置與使用
一、git安裝
在使用Git前我們需要先安裝 Git。Git 目前支援 Linux/Unix、Solaris、Mac和 Windows 平臺上執行。
Git 各平臺安裝包下載地址為:http://git-scm.com/downloads
(1)windows 環境
完成安裝之後,就可以使用命令列的 git 工具(已經自帶了 ssh 客戶端)了,另外還有一個圖形介面的 Git 專案管理工具。
在 開始菜/右鍵/安裝目錄 單裡找到"Git"->"Git Bash",會彈出 Git 命令視窗,你可以在該視窗進行 Git 操作。
二、git常用配置
1)個人資訊配置
配置個人的使用者名稱稱和電子郵件地址,這是在你每次提交程式碼時的說明,知道誰提交了,聯絡誰
2. git config –global user.name “caiping”設定使用者名稱
3. git config –global user.email [email protected] 設定郵箱
4. git config –list檢視git配置資訊
2) github 無金鑰登入配置(在git bash下面操作)
- 授權
ssh-keygen -t rsa -b 4096 -C “[email protected]”
在本地git授權github,有兩種方式授權HTTPS或者SSH
這裡介紹SSH授權:
首先生成ssh key,出現提示一直回車
cat ~/.ssh/id_rsa.pub, 檢視公鑰,新增至
測試是否配置成功
ssh [email protected]
- 生成SSH key
id_rsa_code_aliyun是SSH中的私鑰,id_rsa_code_aliyun.pub則是SSH中對應的公鑰,我們要把公鑰的內容釋出出去,自己保留私鑰就可以了.
xx@yy:~/.ssh$ ssh-keygen -t rsa -b 4096 -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/google/.ssh/id_rsa): id_rsa_code_aliyun
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa_code_aliyun.
Your public key has been saved in id_rsa_code_aliyun.pub.
The key fingerprint is:
ff:af:52:d0:e8:23:4f:84:40:ae:5e:44:6a:cb:a7:0c [email protected]
The key's randomart image is:
+--[ RSA 4096]----+
| .o |
| +. |
| o o. . o |
| o + . + . |
| E + o So . |
| + + ..+ . |
| + +.o |
| o. |
| .oo. |
+-----------------+
google@H:~/.ssh$ ls id_rsa_code_aliyun* -a
id_rsa_code_aliyun id_rsa_code_aliyun.pub - 新增公鑰id_rsa_code_aliyun.pub到阿里雲Code
用notepad++ 開啟,ctr+a,ctr+c 複製(因為生成的公鑰可能含有空格) 配置.ssh config檔案
可以參考阿里雲ssh的README
檔案地址 ~/.ssh/config ,如果沒有的話 touch config(新建一個config) 即可,然後在裡面新增下面的內容
Host code.aliyun.com
HostName code.aliyun.com
IdentityFIle ~/.ssh/id_rsa_code_aliyun
驗證連通成功
ssh -T [email protected]
* Welcome to aliyun Code *
Hi ${your_email}, you have successfully connected over SSH.
To clone a hosted Git repository, use:
git clone git@localhost/REPOSITORY_NAME.git如果沒有配置config檔案,報錯如下:
ssh -T [email protected]
The authenticity of host 'code.aliyun.com (120.55.150.20)' can't be established.
RSA key fingerprint is 69:ab:cb:07:eb:a3:e1:f3:0b:2e:f4:23:b0:c1:c6:9a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'code.aliyun.com,120.55.150.20' (RSA) to the list of known hosts.
Received disconnect from 120.55.150.20: 2: Too many authentication failures
4) 中央倉庫搭建, gitblit
三、git常用命令
git clone 【ssh地址】 , 在當前目錄下下載x.git檔案
git state , 檢視當前倉庫資訊資訊
git pull origin master:master,git <拉取> <遠端主機名> <遠端倉庫名?:<本地倉庫名>
git push orgin master:master, git<推> <遠端主機名> <本地倉庫名>:<遠端倉庫名>
git checkout [branch-name],切換到指定分支,並更新工作區