Linux环境下GIT初次使用

时间:2024-06-20 18:06:56

Git是一个功能强大的分布式版本控制系统,最初用来作Linux内核代码管理的。

第一次接触到github是关于一个报道:在2013年1月15日晚间,全球最大的社交编程及代码托管网站GitHub突然疑似遭遇DDOS攻击,访问大幅放缓,该网站管理员经过日志查询,发现是来自12306的抢票插件用户洪水般的访问导致GitHub出现问题。

进入正题,github的第一次使用大概分以下步骤:
(环境:readhat 5, 提前装好git。若没有安装,用“yum install git-core”命令安装,解决各种依赖问题,很方便。)

1. 申请github账户,并创建repository

2. 本地生成密钥对

3. 设置github上的公钥

4. 创建本地项目

  • 申请github账户,并创建repository

在https://github.com上申请github账户,我的账户名是toughhou,并创建了名为etl的repository。

  • 本地生成密钥对

Git 服务器通常使用 SSH 公钥来进行授权。SSH 公钥默认储存在账户的主目录下的 ~/.ssh 目录。系统中的每个用户都必须提供一个公钥用于授权,没有的话就要生成一个。我没有,这里创建一个。步骤如下:

[root@ora11g ~]# mkdir .ssh
[root@ora11g ~]# cd .ssh/

//用ssh-keygen命令来生成密钥对,passphrase我们输入或不输入都行。

[root@ora11g .ssh]# ssh-keygen -t rsa -C "toughhou@126.com"
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:
03:73:b8:13:4c:cb:63:d0:c3:0b:5d:03:7d:0f:7e:18 toughhou@126.com

[root@ora11g .ssh]# ll
total 8
-rw------- 1 root root 1675 May  5 23:25 id_rsa
-rw-r--r-- 1 root root  398 May  5 23:25 id_rsa.pub

id_rsa.pub就是公钥文件,id_rsa就是密钥。

  • 设置github上的公钥

步骤如下:
    a. 登陆github后 -> "Account Setting" -> "SSH Keys"
    b. "Add SSH Key"
    c. Title随便填写一个,Key那里我们把把id_rsa.pub里的内容拷贝进去。

//好了之后用以下命令测试,若显示信息如下,则说明设置成功了。

[root@ora11g etl]# ssh -T git@github.com
Hi toughhou! You've successfully authenticated, but GitHub does not provide shell access.

  • 创建本地项目

[root@ora11g ~]# mkdir etl
[root@ora11g ~]# cd etl/

//git初始化

[root@ora11g etl]# git init
Initialized empty Git repository in /root/etl/.git/

[root@ora11g etl]# touch readme.txt

[root@ora11g etl]# git add readme.txt

//使用git协议定义远程服务器别名origin

[root@ora11g etl]# git remote add origin git@github.com:toughhou/etl.git

此处需要注意:
    git默认使用https协议,每次pull或push都要输入密码,比较麻烦。而且因为操作系统版本及一些库文件不全,设置起来很繁琐。
    我们这里使用git协议,然后使用ssh密钥。这样可以省去每次都输密码。

用https协议方式定义远程服务器别名:git remote add origin https://github.com/toughhou/etl.git

//查看你当前的remote url

[root@ora11g etl]# git remote -v
origin  https://github.com/toughhou/etl.git (fetch)
origin  https://github.com/toughhou/etl.git (push)

//commit

[root@ora11g etl]# git commit -m "First commit!!"
git commit -m "First commitgit remote -v"
[master (root-commit) 4bd96e9] First commitgit remote -v
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 readme.txt

//push

[root@ora11g etl]# git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 215 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:toughhou/etl.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

push完成之后,登陆github (https://github.com/toughhou/etl)查看,文件已经同步上去。

测试完成。

//文中提到的如果用的是https协议的话,效果如下。push时需要输入密码。

[root@ora11g etl]# git remote add origin9 https://github.com/toughhou/etl.git

[root@ora11g etl]# git push -u origin9 master
Username for 'https://github.com': toughhou
Password for 'https://toughhou@github.com': 
Branch master set up to track remote branch master from origin9.
Everything up-to-date

用https协议的话,可能会碰到一些问题。这里把我碰到的问题及解决方式记录如下:

[root@ora11g etl]# git push -u origin master
error: SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed while accessing https://github.com/toughhou/etl.git/info/refs?service=git-receive-pack
fatal: HTTP request failed

[root@ora11g etl]# curl http://curl.haxx.se/ca/cacert.pem -o /etc/pki/tls/certs/ca-bundle.crt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  244k  100  244k    0     0   7785      0  0:00:32  0:00:32 --:--:--  2690

[root@ora11g etl]# git push -u origin master

(gnome-ssh-askpass:14850): Gtk-WARNING **: cannot open display:  
error: unable to read askpass response from '/usr/libexec/openssh/gnome-ssh-askpass'
Username for 'https://github.com': toughhou

(gnome-ssh-askpass:14856): Gtk-WARNING **: cannot open display:  
error: unable to read askpass response from '/usr/libexec/openssh/gnome-ssh-askpass'
Password for 'https://toughhou@github.com': 
Branch master set up to track remote branch master from origin.
Everything up-to-date

[root@ora11g etl]# echo 'unset SSH_ASKPASS' >> ~/.bashrc && source ~/.bashrc

[root@ora11g etl]# git push -u origin master                                
Username for 'https://github.com': toughhou
Password for 'https://toughhou@github.com': 
Branch master set up to track remote branch master from origin.
Everything up-to-date