拉取遠端代碼:使用Git命令下載遠程倉庫到本地
文章目錄
簡單復習 - 專欄 Git原理詳解與實操指南
學習如何獲取一個遠程倉庫的代碼。
我們這就以代碼托管平台Github為例;https://github.com/。
1、創建遠程代碼倉庫
注冊個賬號
代碼托管平台碼雲 、Github、Gitlab等我們選個一個注冊賬號,
我們這選擇Github吧 官網:https://github.com/
注冊:https://github.com/join?source=header-home
2、創建倉庫
登錄之后,右上角有一個New repository選擇,我們點擊新建一個倉庫。
進入創建倉庫的頁面,我們簡單填寫一下倉庫名稱等信息就行了。
點擊“Create repository”,我們就成功建立了一個遠程倉庫了。
3、進入倉庫
創建完畢后,就進入倉庫。
我們可以通過 git 的命令將遠程倉庫拉取到本地,一般會提供 HTTPS 協議和 SSH 兩種協議提供管理。
4、HTTP(S)獲取遠程倉庫
HTTP 協議方式拉取代碼(遠程倉庫)是比較簡單的,直接執行 git 的 clone 命令即可,不需要額外的配置操作,但相對 SSH協議來說安全性較低。
首次拉取
HTTP 協議首次拉取代碼的命令格式如下所示:
git clone 版本庫地址 [本地文件夾名稱]
我要把剛才新建的倉庫代碼拉取到本地,並且本地的文件夾名稱叫play-spring-family
(也可以不指定本地文件夾名稱,默認名字為遠程倉庫名字),參考命令如下所示
git clone https://github.com/liuawen/play-spring-family.git play-spring-family
那我去操作一下,先復制版本庫地址吧
https://github.com/liuawen/play-spring-family.git
創建文件夾play-spring-family
,執行命令git clone https://github.com/liuawen/play-spring-family.git play-spring-family
即可把遠程倉庫拉取到本地。
命令執行完成后,會要求你輸入用戶名和密碼,只有當你輸入正確的用戶名和密碼之后代碼才能正常拉取。應該是會出現這個。
Username for 'https://github.com': liuawen
Password for 'https://liuawen@github.com':
操作如下:
liuawen@DESKTOP-HVI7SH0:~$ su root
Password:
➜ liuawen pwd
/home/liuawen
➜ liuawen mkdir play_spring_family
➜ liuawen ls
git play_spring_family sources.list
➜ liuawen git clone https://github.com/liuawen/play-spring-family.git play-spring-family
Cloning into 'play-spring-family'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
Unpacking objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
➜ liuawen cd play-spring-family
➜ play-spring-family git:(master) ls
README.md
➜ play-spring-family git:(master) ls -al
total 0
drwxr-xr-x 1 root root 4096 Mar 21 19:24 .
drwxr-xr-x 1 liuawen liuawen 4096 Mar 21 19:24 ..
drwxr-xr-x 1 root root 4096 Mar 21 19:25 .git
-rw-r--r-- 1 root root 44 Mar 21 19:24 README.md
➜ play-spring-family git:(master)
更新代碼
假設遠程代碼有變更,我想本地代碼也更新一波時,我應該怎么做呢?
我可以在本地的版本庫目錄下通過git pull
命令更新,不需要再指定遠程地址,
命令如下
git pull
默認情況下會再次提示輸入密碼,因為 git 默認沒有緩存 HTTP 認證權限。我是設置了
➜ play-spring-family git:(master) git pull
Already up to date.
➜ play-spring-family git:(master)
臨時記住密碼
如果不想每次都輸入 git 的認證信息,可以設置緩存認證數據,默認記住 15 分鍾,如下命令所示:
git config --global credential.helper cache
當然也可以指定緩存時長,比如下面是自定義配置記住 1 分鍾的命令:
git config credential.helper ‘cache –timeout=60’
credential n. 證書;憑據
timeout的單位為秒。
永久記住密碼
感覺很麻煩啦,頻繁輸入用戶名、密碼。
我不想每次提交代碼都要輸入用戶名密碼,我要讓 Git 永久記住密碼,那怎么操作呢?
git config --global credential.helper store
命令執行完畢之后,會在當前用戶主目錄的.gitconfig
文件中新增一項配置,執行命令查看
➜ .git git:(master) vim ~/.gitconfig
配置如下所示
上面是設計全局的,也可以設計局部的,那我設置下本地當前倉庫的。
git config credential.helper store
我去當前倉庫.config文件查看下,是否設置好了
config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = https://github.com/liuawen/play-spring-family.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[credential]
helper = store
~
執行過的命令:
➜ play-spring-family git:(master) git config credential.helper store
➜ play-spring-family git:(master) pwd
/home/liuawen/play-spring-family
➜ play-spring-family git:(master) ls -al
total 0
drwxr-xr-x 1 root root 4096 Mar 21 19:24 .
drwxr-xr-x 1 liuawen liuawen 4096 Mar 21 19:24 ..
drwxr-xr-x 1 root root 4096 Mar 21 20:34 .git
-rw-r--r-- 1 root root 44 Mar 21 19:24 README.md
➜ play-spring-family git:(master) ls al
ls: cannot access 'al': No such file or directory
➜ play-spring-family git:(master) cd .git
➜ .git git:(master) ls -al
total 0
drwxr-xr-x 1 root root 4096 Mar 21 20:34 .
drwxr-xr-x 1 root root 4096 Mar 21 19:24 ..
-rw-r--r-- 1 root root 107 Mar 21 20:22 FETCH_HEAD
-rw-r--r-- 1 root root 23 Mar 21 19:24 HEAD
-rw-r--r-- 1 root root 41 Mar 21 20:22 ORIG_HEAD
drwxr-xr-x 1 root root 4096 Mar 21 19:24 branches
-rw-r--r-- 1 root root 304 Mar 21 20:33 config
-rw-r--r-- 1 root root 73 Mar 21 19:24 description
drwxr-xr-x 1 root root 4096 Mar 21 19:24 hooks
-rw-r--r-- 1 root root 137 Mar 21 19:25 index
drwxr-xr-x 1 root root 4096 Mar 21 19:24 info
drwxr-xr-x 1 root root 4096 Mar 21 19:24 logs
drwxr-xr-x 1 root root 4096 Mar 21 19:24 objects
-rw-r--r-- 1 root root 114 Mar 21 19:24 packed-refs
drwxr-xr-x 1 root root 4096 Mar 21 19:24 refs
➜ .git git:(master) vim config
如果沒有加上--global
,則會在當前項目下的.git/config
文件增加配置
[credential]
helper = store
git 永久記住密碼是根據配置文件所決定,我也可以直接復制配置文件。
https拉取遠程倉庫每次都要輸用戶名密碼,用了git config --global credential.helper store
,這樣會把賬號和密碼明文保存在.gitconfig-credentials里。
5、 SSH拉取
SSH
( Secure Shell )方式拉取代碼,相比HTTP(S)來說更加安全。
為什么呢更安全呢?因為SSH方式使用的是非對稱加密,采用公鑰與私鑰的方式。
相對來說配置起來會麻煩一些;好處是一次配置之后,后續不需要每次都進行認證,也更加安全效率也高點吧。
拉取代碼
SSH
git clone git@github.com:liuawen/play-spring-family.git play-spring-family-ssh
SSH地址:
執行git clone git@github.com:liuawen/play-spring-family.git play-spring-family-ssh
命令,提示需要權限驗證。
➜ liuawen ls
git play-spring-family play_spring_family sources.list
➜ liuawen git clone git@github.com:liuawen/play-spring-family.git play-spring-family-ssh
Cloning into 'play-spring-family-ssh'...
The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)?
沒有配置公鑰與私鑰,所以我們這第一次拉取代碼失敗了。
➜ liuawen git clone git@github.com:liuawen/play-spring-family.git play-spring-family-ssh
Cloning into 'play-spring-family-ssh'...
The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
➜ liuawen
怎么辦呢?看下面吧。
創建一個ssh key
通過 ssh 協議拉取代碼要保證當前用戶的主目錄存在一個.ssh
的文件夾,並且里面已經存在私鑰文件,如果沒有的話我們可以通過ssh-keygen
,生成一份公鑰與私鑰。
➜ liuawen ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:z5hcJzLsnISUNxjOFeZz9R1PPsWk8SirFwoRyWYe0VE root@DESKTOP-HVI7SH0
The key's randomart image is:
+---[RSA 2048]----+
| ..*=.oE .++|
| o B*... . O=|
| *+*.. . +.=|
| . +.= o .|
| . S o + |
| = @ = . |
| B = . |
| . |
| |
+----[SHA256]-----+
➜ liuawen cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRaxJM1CAl0u+ImEVhC2P2iiGloo1Bx4NQ2bOjQxgy96ncTwUbrG3QmUmX86Oyfl49DRgpnwYe3wBA2wy7fxrqz2sYJAPdhqVlyJpL9SiKdOAw9Vu8aA1IVzDBhKN4W2bHJ7xQ6X7OnQ5tTz3Mcjdyk8cbwVIg1zYI56ABQZaEewt3deTlU24rAaqb+0voA/RrzoaBqtv6XHXpef/s4QIWQZ21m2IyppUkQERC28xVaAwJGLedIrjQ6AyLLxAkgd5BZkhCv02PPDYWT2ixlHK2DDpX45BEgUNDbi6kqKMglK0G67kLFiFssmDwF2vLDGjlKIyWYPwgwNYyvR73d7SF root@DESKTOP-HVI7SH0
➜ liuawen
ssh-keygen
,最終會在當前用戶目錄下生成公鑰和私鑰,查看生成的公鑰的命令為cat ~/.ssh/id_rsa.pub
要保存好私鑰哦。
添加公鑰到服務器
ssh-keygen
cat ~/.ssh/id_rsa.pub
當我們確認公鑰和私鑰生成完畢之后,我們還需要將公鑰放到遠程的 Github 倉庫中去。
把上面的cat ~/.ssh/id_rsa.pub
命令查看到的密鑰復制過來,點擊"Add SSH key"。
再次拉取代碼
當我們把公鑰添加到Github版本庫進去之后,就已經完成了權限配置。
我們再次使用ssh方式拉取代碼,就不會提示沒有權限。
➜ liuawen git clone git@github.com:liuawen/play-spring-family.git play-spring-family-ssh
Cloning into 'play-spring-family-ssh'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
➜ liuawen ls
git play-spring-family play-spring-family-ssh play_spring_family sources.list
➜ liuawen cd play-spring-family-ssh
➜ play-spring-family-ssh git:(master) ls -al
total 0
drwxr-xr-x 1 root root 4096 Mar 21 21:04 .
drwxr-xr-x 1 liuawen liuawen 4096 Mar 21 21:04 ..
drwxr-xr-x 1 root root 4096 Mar 21 21:06 .git
-rw-r--r-- 1 root root 44 Mar 21 21:04 README.md
➜ play-spring-family-ssh git:(master)
執行git clone git@github.com:liuawen/play-spring-family.git play-spring-family-ssh
命令之后,代碼已經成功拉取咯。
我們來更新一波代碼
更新代碼
ssh 方式更新代碼命令和上面的 http 方式拉取代碼命令一致,同樣需要在 目錄play-spring-family-ssh
下執行命令:git pull
,然后可以看到git成功的拉取到了代碼
➜ play-spring-family-ssh git:(master) git pull
Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
Already up to date.
➜ play-spring-family-ssh git:(master)
6、小結
使用過的命令
git clone 版本庫地址 [本地文件夾名稱]
git pull
git config --global credential.helper cache
git config credential.helper ‘cache –timeout=60’
git config --global credential.helper store
vim ~/.gitconfig
git clone git@github.com:liuawen/play-spring-family.git play-spring-family-ssh
ssh-keygen
cat ~/.ssh/id_rsa.pub
學習了如何在Github創建一個遠程倉庫,以及兩種方式拉取遠程倉庫代碼拉取到本地。
HTTP(S)和SSH協議方式拉取代碼。
HTTP拉取
git clone https://github.com/liuawen/play-spring-family.git play-spring-family
git pull
git config --global credential.helper cache
git config credential.helper ‘cache –timeout=60’
git config --global credential.helper store
Q:HTTP(S)
協議默認每次需要輸入賬號密碼,那我不想每次輸入賬號密碼怎么解決呢?
A:可以通過緩存認證方式處理,永久(臨時)記住密碼。
使用git config --global credential.helper store
命令,這樣會把賬號和密碼明文保存在.gitconfig-credentials里。
SSH拉取
SSH第一次直接拉取代碼,會提示沒有權限,
我們要將SSH
協議生成的公鑰放到 Git 服務器當中去,配置好了Git會自動通過ssh協議進行鑒權,不需要通過賬號加密碼。
git clone git@github.com:liuawen/play-spring-family.git play-spring-family-ssh
git pull
ssh-keygen
Q:換了計算機,要不要重新配置SSH?
A:可以重新配置,也可以將原來計算機的.ssh文件夾復制到新的計算機。