Git- 連接遠程倉庫


  如何使用Git 連接遠程倉庫呢?遠程倉庫->一般指的是代碼托管平台。那就先來瞅瞅三個較熟悉的版本(代碼)托管服務平台。

 

版本(代碼)托管服務平台:

碼雲(gitee.com):是開源中國社區團隊推出的基於Git的快速的、免費的、穩定的在線代碼托管平台,不限制私有庫和公有庫數量.

Coding(coding.net): 是CODING 旗下的一站式開發平台,提供 git/svn 代碼托管,免費支持私有庫(限定)和公有庫

github(github.com):是全球最大的開源社區,基於git的版本托管平台。私有庫需要付費,訪問速度慢。

 

前提准備:

1.git工具的下載和安裝(一直next就行了)。 下載 >>> 

2.github/碼雲/Coding上進行注冊。  碼雲>>> coding>>>   github>>>

前提准備好了就可以開始進行Git與遠程倉庫的連接,這里以github為例。

 

一、Git的配置

1.設置用戶名和郵箱(--global 為全局參數,表明本地所有Git倉庫都會使用這個配置)

git config --global user.name "yourname"

git config --global user.email "your_email@youremail.com"

2.生成密鑰(SSH key)

ssh-keygen -t rsa -C "your_email@youremail.com"

3.添加密鑰(SSH key),並驗證是否成功

添加密鑰:將上一步驟生成的密鑰即.ssh/id_rsa.pub中內容全部復制。在github的 Settings-->SSH and GPG keys-->New SSH key,key中粘貼復制的內容(Title自定義)。

驗證:github輸入第一條的命令,碼雲輸入第二條

a.ssh -T git@github.com 
b.ssh -T git@gitee.com

二、創建項目工程

1.遠程倉庫:在github中New repository 輸入Repository name。[例如:TestDemo]

2.項目工程:在自己本地電腦上新建一個與github新項目工程同名的文件夾。[例如:TestDemo]

三、創建版本庫

 進入步驟二中的文件夾下,輸入以下命令初始化倉庫,若出現:Initialized empty Git repository in E:/** /**/.git/ 則表示創建成功[注意:此時會生成一個.git目錄(隱藏目錄)]

git init

四、連接遠程倉庫(下面兩種方式都可以)

git remote add origin git@github.com:yourName/repositoryname.git
git remote add origin https://github.com/yourName/repositoryname.git

五、從遠程倉庫pull文件(若遠程倉庫沒有文件,直接執行步驟六)

git pull origin master

六、將本地文件push到遠程倉庫(若沒有文件則手動創建)

git status          查看工作目錄的狀態

git add <file>        將文件添加到暫存區
 git commit -m "commnet"   提交更改,添加備注信息(此時將暫存區的信息提交到本地倉庫)

git push origin master    將本地倉庫的文件push到遠程倉庫(若 push 不成功,可加 -f 進行強推操作)

 注: 至此已經完成了 遠程與本地倉庫的配置,若需要單獨配置可見以下操作

七、生成多個密鑰(多個賬戶)配置不同的遠程倉庫【賬號配置為局部變量】

 

a.添加新的ssh-key
如果報錯:Could not open a connection to your authentication agent.無法連接到ssh agent;可執行ssh-agent bash命令后再執行ssh-add命令
  ssh-add ./id_rsa_github
  ssh-add ./id_rsa_gitee
 
b.配置config文件
在./ssh目錄下若沒有 config文件則創建
# 配置 github
Host github.com
HostName github.com
IdentityFile C:\\Users\\zzw\\.ssh\\id_rsa_github
PreferredAuthentications publickey
User ZeroBound

# 配置 gitee
Host gitee.com
HostName gitee.com
IdentityFile C:\\Users\\zzw\\.ssh\\id_rsa_gitee
PreferredAuthentications publickey
User zhzw
 
c.到github或碼雲上添加 密鑰,之后驗證是否成功
  1.ssh -T git@github.com
  2.ssh -T git@gitee.com
 
d.進入倉庫目錄配置用戶名和郵箱
  git config user.name "yourname"
  git config user.email "your_email@youremail.com"

 

八、相關問題

Q1git pull origin master 無法進行pull,出現如下提示

git pull origin master
fatal: unable to access 'https://github.com/yourName/Demo.git': error setting certificate verify locations:
  CAfile: G:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
  CApath: none

分析:ca-bundle.crt文件是證書文件。根據提示CApath:none 沒有該文件,所以無法訪問遠程倉庫

解決:修改為正確路徑 或者 將證書驗證設置false

git config --system http.sslcainfo E:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
git config
--system http.sslverify false

 Q2.git pull origin master 出現如下提示:

fatal: refusing to merge unrelated histories

解決:如下操作即可解決

git pull origin master --allow-unrelated-histories

Q3.每次git push origin master 時都需要輸入用戶名和密碼:

 因為配置的時候使用的是https協議,所以每次都需要輸入

git remote -v  查看遠程連接

git remote rm origin  刪除遠程連接

git remote add origin git@github.com:yourName/repositoryname.git
 


免責聲明!

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



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