安装

git官网直接下载(windows或tar包)或者查看命令(linux),然后按默认选项安装即可。

账户配置

账户的配置有两种方式:
(1)name、pwd
(2)SSH(推荐)

在通常情景下第一种其实也是足够的,配置的方式是

  1. $ git config --global user.name "Your Name"
  2. $ git config --global user.email "Your Email"

注意git config命令的--global参数,用了这个参数,表示机器上所有的Git仓库都会使用这个配置,当然也可以对某个仓库指定不同的用户名和Email地址。

在网上搜索到的很多资料上到这一步也就结束了,因为这已经足够日常使用了,在各种图形化页面或者工具中都会记录密码来减少录入,但这对CI/CD来说其实不足够,原因是在终端模拟器(各类shell)或者web时,是不能再录入信息的(这也是为什么很多命令中都提供了-y这个参数表示确定)。解决这个问题的方式是ssh登录。

github的ssh文档中已经把操作过程写的很清楚了,这里只是对照操作罢了,注意windows以gitbash操作而不是cmd

  1. 检查是否已经有ssh

    1. $ ls -al ~/.ssh #列出ssh,若存在idxxx.pub的则说明已经存在了
  2. 生成ssh

    1. ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

    这个命令的过程是:

    Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/WJ/.ssh/id_rsa): #回车,默认即可 Enter passphrase (empty for no passphrase): #回车,不要密码 Enter same passphrase again: #回车即可 Your identification has been saved in /c/Users/WJ/.ssh/id_rsa
    Your public key has been saved in /c/Users/WJ/.ssh/id_rsa.pub

在windows上ssh的存储位置是:C:\Users\WJ.ssh,在linux上ssh的存储位置是:/root/.ssh/id_rsa.pub

  1. 写ssh到站点

把第2步的pub文件写到站点配置中。配置项名称也许不通,比如github的配置方式是settings—ssh and gpg keys
项目中添加的方式是项目settings—deploy keys,添加之后对应的机器就有写权限了。