ubuntu下安装git

最近在做自己的个人博客项目,部署在阿里云主机上,系统为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 条目:
步骤一.PNG
然后
步骤二.PNG

步骤三.PNG
在这里添加刚才保存的公钥信息

打开保存公钥的文件,复制里面的信息粘贴到上面的添加公钥的地方,然后提交就行了。

root@wangjun:~# cat publickey.pub
ssh-rsa AAAAB3NzaC1yc2EAAc2ps3Yy4/a这里的一大串就是公钥信息UzcH7DFBVFlgE4X3v5BH/adtRYabiFKW5sewf0n+75kz3Q6YBu1DiPOEmwpoSmPBEoLGBJCmiydkjdop0pfjn2dGYv [email protected]
root@wangjun:~#

到此,任务就基本完成了,可以试着在github上克隆一个项目试试

猜你喜欢

转载自www.cnblogs.com/wangjunget/p/9721202.html