【Git】之搭建 git 环境

╰+攻爆jí腚メ 2022-02-17 00:12 425阅读 0赞

Windows 环境


1、安装 Git

Git官网:https://git-scm.com/download 上下载 Windows 版的 Git 应用程序,根据自己的操作系统下载安装对应的版本,安装过程直接一路默认安装即可。

2、配置 Git

查看用户名和邮箱

  1. git config user.name //获取当前登录的用户
  2. git config user.email //获取当前登录用户的邮箱

设置或修改用户名和邮箱

安装完成后配置环境,打开git bash,输入下面面命:

  1. git config --global user.name "Your Name"
  2. git config --global user.email "email@example.com"
  3. git config --global core.autocrlf false # 解决 LF will be replaced by CRLF 问题

--global 参数表示全局设定,以后所有的Git仓库的操作都使用这个所设定的用户名和邮箱。

添加 ssh 秘钥

  1. ssh-keygen -t rsa -C "youremail@example.com"

参数说明:

  • -t:指定密钥类型,默认是 rsa ,可以省略
  • -C:设置注释文字,比如邮箱
  • -f:指定密钥文件存储文件名,可以省略

一直回车直到提示成功,这样就会在用户家目录里生成 .ssh 目录:

  1. $ cd ~
  2. $ ls .ssh
  3. id_rsa id_rsa.pub

里面有 id_rsaid_rsa.pub 两个文件,这两个就是 SSH Key 的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥。

ssh key 的公钥内容就是我们上传到 GitHub 上面的密钥内容。如果添加公钥到 GitHub 成功则可以使用命令测试是否成功:

  1. $ ssh -T git@github.com
  2. Hi! You've successfully authenticated, but GitHub does not provide shell access.

3、设置字体

有时候在配置 GitBash 字体的时候会出现字体缺失的问题,比如没有自己想要的字体,这就需要我们手动修改配置文件来使用我们喜欢的字体。

打开 GitBash 输入命令:

  1. $ cd ~
  2. $ vim .minttyrc
  3. # 修改里面的字体设置内容
  4. Font=Courier New
  5. FontHeight=17

第一个代表字体,第二个代表字体大小根据自己的喜好修改对应的值就可以了,当然你选择的字体必须是要系统已经安装了的,不然修改了也是没法生效的。

当添加了自己定义的字体后,配置窗口也会对应多出这种字体的选择项:

在这里插入图片描述

3、NullReferenceException 异常

使用 git push 时:

  1. $ git push origin master

遇到报错:

  1. fatal: NullReferenceException encountered.

出现这种情况是因为 git 的 credential manager 在版本 2.19.1 会出现问题。解决方法为下载安装 Git Credential Manager for Windows

在这里插入图片描述
安装完成后再使用 push 命令就不会出现上面的报错了!

Linux 环境


查看是否已经安装过 git

  1. [root@centos] git --version
  2. git version 1.8.3

卸载旧版 git

  1. [root@centos] yum remove git

安装方式一:yum 安装

  1. [root@centos] yum -y install git

安装方式二:源码安装:

安装依赖包(根据实际情况安装,有些可能已经安装过来):

  1. [root@centos] yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
  2. [root@centos] yum install gcc-c++ perl-ExtUtils-MakeMaker

下载源码包(下载的版本为 2.25.0,下载路径为:/opt/):

  1. [root@centos] cd /opt/
  2. [root@centos] wget https://www.kernel.org/pub/software/scm/git/git-2.25.0.tar.gz

解压:

  1. [root@centos] tar -zxvf git-2.25.0.tar.gz

编译安装(安装在 /usr/local/git 目录下):

  1. [root@centos] mkdir /usr/local/git
  2. [root@centos] cd git-2.25.0
  3. [root@centos] make prefix=/usr/local/git all
  4. [root@centos] make prefix=/usr/local/git install

安装成功后配置 git 环境变量:

  1. [root@centos] vim /etc/profile

在最后一行添加:

  1. export PATH=/usr/local/git/bin:$PATH

生效文件:

  1. [root@centos] source /etc/profile

查看版本:

  1. [root@centos] git --version
  2. git version 2.25.0

生成 RSA 密钥:

  1. [root@centos] ssh-keygen -t rsa

发表评论

表情:
评论列表 (有 0 条评论,425人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Git实战(三)环境

            上次的博文中,我们介绍了一下关于Git作为版本控制工具的基本原理,接下来我们来搭建一个基本的Git环境(由于我使用的是Windows系统,所以重点侧重在这方面)