最近在做自己的個人博客項目,部署在阿里雲主機上,系統為ubuntu 16.04.4。項目開發在自己的Windows電腦上,每次項目進行改動后都需要手動上傳文件到服務器上,感覺很是麻煩。所以准備在服務器上安裝git並關聯github賬號,通過github進行中轉,拉取上面的項目信息,希望能夠簡便項目變更所帶來的麻煩。
連接遠程主機使用的工具是 Xshell
一、安裝git
首先輸入git命令查看git是否已經安裝
root@wangjun:~# git
The program 'git' is currently not installed. You can install it by typing:
apt install git
系統提示git未安裝,並提示安裝方式,依照上面的提示輸入命令:
root@wangjun:~# apt install git
注意:這里使用的安裝方式是安裝方式,也可以使用源碼安裝等其他的方式,這里就不在介紹。
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
linux-headers-4.4.0-87 linux-headers-4.4.0-87-generic linux-image-4.4.0-87-generic
linux-image-extra-4.4.0-87-generic
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
git-man liberror-perl
Suggested packages:
git-daemon-run | git-daemon-sysvinit git-doc git-el git-email git-gui gitk gitweb git-arch git-cvs git-mediawiki
git-svn
The following NEW packages will be installed:
git git-man liberror-perl
0 upgraded, 3 newly installed, 0 to remove and 109 not upgraded.
Need to get 3,914 kB of archives.
After this operation, 25.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://mirrors.cloud.aliyuncs.com/ubuntu xenial/main amd64 liberror-perl all 0.17-1.2 [19.6 kB]
Get:2 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/main amd64 git-man all 1:2.7.4-0ubuntu1.4 [736 kB]
Get:3 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/main amd64 git amd64 1:2.7.4-0ubuntu1.4 [3,158 kB]
Fetched 3,914 kB in 0s (29.3 MB/s)
Selecting previously unselected package liberror-perl.
(Reading database ... 185354 files and directories currently installed.)
Preparing to unpack .../liberror-perl_0.17-1.2_all.deb ...
Unpacking liberror-perl (0.17-1.2) ...
Selecting previously unselected package git-man.
Preparing to unpack .../git-man_1%3a2.7.4-0ubuntu1.4_all.deb ...
Unpacking git-man (1:2.7.4-0ubuntu1.4) ...
Selecting previously unselected package git.
Preparing to unpack .../git_1%3a2.7.4-0ubuntu1.4_amd64.deb ...
Unpacking git (1:2.7.4-0ubuntu1.4) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up liberror-perl (0.17-1.2) ...
Setting up git-man (1:2.7.4-0ubuntu1.4) ...
Setting up git (1:2.7.4-0ubuntu1.4) ...
root@wangjun:~#
一大串信息,看着很爽,注意沒報錯信息安裝就完成了。
可以在命令行輸入git,會提示git命令
root@wangjun:~# git
usage: git [--version] [--help] [-C <path>] [-c name=value]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
These are common Git commands used in various situations:
start a working area (see also: git help tutorial)
clone Clone a repository into a new directory
init Create an empty Git repository or reinitialize an existing one
git命令有很多,有時間可以仔細看一下。
二、進行git配置
后面的步驟其實和在Windows上配置git基本相同。
不過Windows上可以使用git bash進行命令的執行,或者使用圖形化工具進行處理。喜歡的話也可以在Windows的命令行中執行命令,哈哈哈
這里就直接在linux命令行中輸入git命令就行了。
root@wangjun:~# git config --global user.name '自定義用戶名'
root@wangjun:~# git config --global user.email '郵箱'
這樣的話,git最基本的配置就完成了,雖說是最基本的,不過對於我們來說已經夠用了,接下來就可以像在Windows上同樣使用了。
三、連接github
接下來就是最后一步也就是最重要的一步了。
1.生成公鑰
命令行執行生成公鑰命令
root@wangjun:~# ssh-keygen -C '郵箱' -t rsa
這里的郵箱是剛才配置里的郵箱,生成的公鑰文件保存在當前路徑下,當然你可以指定任意保存路徑。
接下來會提示輸入保存公鑰的文件名稱,以及文件打開密碼。這里的密碼可以不填,默認為空。不過建議為了安全可以設置一個密碼,公鑰文件請妥善保存,以免遺失。
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): publickey
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in publickey.
Your public key has been saved in publickey.pub.
The key fingerprint is:
SHA256:這里是指紋信息
The key's randomart image is:
+---[RSA 2048]----+
| oo +BO|
| .. =.+O|
| ..o++o.|
| = =.oo.|
| .S.+ o. .|
| +=. . . |
| E + ...o .|
| ...+o.= . o.|
| 哈哈很有趣的圖片
+----[SHA256]-----+
上面的信息都是很有用的,請妥善保存。
2.連接github
進入github網站,打開setting,選擇SSH and GPG key 條目:
然后
在這里添加剛才保存的公鑰信息
打開保存公鑰的文件,復制里面的信息粘貼到上面的添加公鑰的地方,然后提交就行了。
root@wangjun:~# cat publickey.pub
ssh-rsa AAAAB3NzaC1yc2EAAc2ps3Yy4/a這里的一大串就是公鑰信息UzcH7DFBVFlgE4X3v5BH/adtRYabiFKW5sewf0n+75kz3Q6YBu1DiPOEmwpoSmPBEoLGBJCmiydkjdop0pfjn2dGYv 1273477277@qq.com
root@wangjun:~#
到此,任務就基本完成了,可以試着在github上克隆一個項目試試