本人之前整理過相關資料——「個人學習Git以及研究」。如今爲了在GitHub上搭建個人部落格,故而在本人CentOS7系統上部署Git環境。現將相關操作記錄完整記錄下來,以饗諸賢達君子。

Preparartion

Software Official Website
CentOS7 https://www.centos.org/
Git https://github.com/git/git

系統相關信息

[flying@lemp ~]$ date -R
Thu, 21 Jan 2016 09:30:54 +0800
[flying@lemp ~]$ cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[flying@lemp ~]$ uname -r
3.10.0-327.4.4.el7.x86_64
[flying@lemp ~]$

Required Packages

安裝相關依賴包,執行如下命令

sudo yum install wget autoconf gcc zlib-devel perl-ExtUtils-MakeMaker curl-devel -y

解釋說明

Package Explanation
wget 使用wget命令下載時需用到
autoconf 執行make configure命令時需用到
gcc 執行./configure命令時用到
zlib-devel 執行make命令時用到
perl-ExtUtils-MakeMaker 執行make命令時用到
curl-devel 使用git clone時支持https

Download Git Source Package

Git發佈頁面鏈接 https://github.com/git/git/releases,以最新的 v.2.7.0 爲例

Git v.2.7.0.tar.gz下載地址: https://github.com/git/git/archive/v2.7.0.tar.gz


Compile and Install Git

Decompress

下載路徑以/tmp爲例

cd /tmp
wget https://github.com/git/git/archive/v2.7.0.tar.gz
tar -zxf v2.7.0.tar.gz

Compile

安裝路徑以/usr/local爲例

cd git-2.7.0
make configure
./configure --prefix=/usr/local/
sudo make
sudo make install

安裝結束後,執行命令git --version,如能正確顯示版本號則安裝成功

[flying@lemp tmp]$ git --version
git version 2.7.0
[flying@lemp tmp]$

創建符號鏈接(這一步很重要),如果不創建,使用git時可能會提示命令找不到。

操作過程

[flying@lemp ~]$ sudo ln -s /usr/local/bin/git /usr/bin
[sudo] password for flying:

[flying@lemp ~]$ which git
/usr/local/bin/git

[flying@lemp ~]$ whereis git
git: /usr/bin/git /usr/local/bin/git
[flying@lemp ~]$

Set Up Git

執行如下操作,其中的 * YOUR NAME是您的GitHub用戶名 * YOUR EMAIL ADDRESS是您的GitHub郵箱地址 注意:在執行命令時,必須使用雙引號""將其包裹起來。

git config --global user.name "YOUR NAME"
git config --global user.email "YOUR EMAIL ADDRESS"

之後執行如下命令

git config --list

會顯示如下信息

user.name=YOUR NAME
user.email=YOUR EMAIL ADDRESS

注意:在Git 2.0版本中,push.default的隱性值(implicit value)由 matching 更改爲 *simple*,在使用git push時,會出現如下信息

warning: push.default is unset; its implicit value has changed in Git 2.0 from 'matching' to 'simple'. To squelch this message and maintain the traditional behavior, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches to the remote branches that already exist with the same name.

Since Git 2.0, Git defaults to the more conservative 'simple' behavior, which only pushes the current branch to the corresponding remote branch that 'git pull' uses to update the current branch.

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode 'current' instead of 'simple' if you sometimes use older versions of Git)

根據提示,採用保守行爲simple,執行如下命令

git config --global push.default simple

再次執行命令

git config --list

顯示如下信息

user.name=YOUR NAME
user.email=YOUR EMAIL ADDRESS
push.default=simple

也可通過如下命令進行修改

git config --global --edit

信息保存在文件$HOME/.gitconfig

#cat ~/.gitconfig
[user]
	name = YOUR NAME
	email = YOUR EMAIL ADDRESS
[push]
	default = simple

Generating SSH Keys

使用Git連接GitHub需要進行認證,GitHub推薦的連接Git的方式是通過HTTPS,此處本人通過SSH方式。

注: 關於如何使用HTTPS進行連接,可參考GitHub官方文檔 Set Up Git 研究。

Install OpenSSH

使用SSH需要安裝有OpenSSH相關介紹」,使用如下命令查看是否安裝:

rpm -qa | grep openssh

操作過程

[flying@lemp ~]$ rpm -qa | grep openssh
openssh-6.6.1p1-23.el7_2.x86_64
openssh-clients-6.6.1p1-23.el7_2.x86_64
openssh-server-6.6.1p1-23.el7_2.x86_64
[flying@lemp ~]$

如未安裝,可使用如下命令安裝

sudo yum install openssh -y

Check For SSH Keys

輸入如下命令

ls -Al ~/.ssh

查看是否在本機已有GitHub的SSH Keys存在,如果沒有,會出現如下報錯

ls: cannot access .ssh/: No such file or directory

如果有,則跳過跳過接下來的 Generate A New SSH Key 再繼續操作。

Generate A New SSH Key

執行如下命令

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# 使用您GitHub帳號的郵箱生成SSH Key,須用雙引號包裹
# Creates a new ssh key, using the provided email as a label

提示 Enter file in which to save the key (/Users/you/.ssh/id_rsa) 時,GitHub官方建議使用默認路徑,直接點擊 Enter 回車鍵。

提示 Enter passphrase (empty for no passphrase) 時,自己設置一個密碼,輸入完成後點擊 Enter 回車鍵,會提示 Enter same passphrase again ,再次輸入剛輸入的密碼即可。

如下是操作過程

#將YOUR_EMAIL_ADDR替換爲您自己的GitHub郵箱
[flying@lemp ~]$ ssh-keygen -t rsa -b 4096 -C "YOUR_EMAIL_ADDR"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/flying/.ssh/id_rsa):
Created directory '/home/flying/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/flying/.ssh/id_rsa.
Your public key has been saved in /home/flying/.ssh/id_rsa.pub.
The key fingerprint is:
5e:35:3d:0b:89:0e:72:89:39:cf:3f:0e:26:c1:4f:72 lempstacker@hotmail.com
The key's randomart image is:
+--[ RSA 4096]----+
|                 |
|       o . . o   |
|      = + . = o  |
|     . * o . o o |
|      + E o   .  |
|       B o       |
|      . = o      |
|       o o .     |
|          .      |
+-----------------+
[flying@lemp ~]$

Add Your Key To The ssh-agent

  1. 確認ssh-agent已經啓用

    ssh-agent -s
    # start the ssh-agent in the background
    

操作過程

[flying@lemp ~]$ ssh-agent -s
SSH_AUTH_SOCK=/tmp/ssh-Ce3ko1MRBHqZ/agent.22732; export SSH_AUTH_SOCK;
SSH_AGENT_PID=22733; export SSH_AGENT_PID;
echo Agent pid 22733;
[flying@lemp ~]$
  1. 將SSH Keys添加入ssh-agent(Git Bash) ssh-add ~/.ssh/id_rsa 可能會出現如下報錯 Could not open a connection to your authentication agent.

GitHub官方有說明,如果使用的不是Git Bash,則需執行如下命令

eval $(ssh-agent -s)
# start the ssh-agent in the background

返回信息

[flying@lemp ~]$ eval $(ssh-agent -s)
Agent pid 22895
[flying@lemp ~]$

註:Stack Overflow中有相關提問Could not open a connection to your authentication agent,可前往查看。

再次執行

ssh-add ~/.ssh/id_rsa

如果在 Generate A New SSH Key 時設置有密碼,則此處會提示輸入密碼(passphrase)

[flying@lemp ~]$ ssh-add ~/.ssh/id_rsa
Enter passphrase for /home/flying/.ssh/id_rsa:
Identity added: /home/flying/.ssh/id_rsa (/home/flying/.ssh/id_rsa)
[flying@lemp ~]$

GitHub官方提示 >Tip: If you didn’t generate a new SSH key in Step 2(即本文的 Generate A New SSH Key 章節), and used an existing SSH key instead, you will need to replace id_rsa in the above command(即本文的 Check For SSH Keys 章節)with the name of your existing private key file.

Add Your SSH Key To Your Account

clip < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard

GitHub官方說此命令用於將~/.ssh/id_rsa.pub中內容複製到剪切板,但因本人使用CLI(命令行)界面,故採用cat命令顯示~/.ssh/id_rsa.pub中內容

cat ~/.ssh/id_rsa.pub

複製顯示的相關內容。

打開https://github.com/settings/ssh,登入自己的GitHub賬戶,點擊按鈕 Add SSH key,將複製到的信息粘貼進 Key 欄中,Title任意(最好能幫助自己記憶和辨識),最後點擊 Add key 按鈕確定。

Test Connection

使用如下命令嘗試連接GitHub

ssh -T git@github.com
# Attempts to ssh to GitHub

會出現如下提示

The authenticity of host 'github.com (192.30.252.128)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)?

直接輸入yes,再點擊Enter回車鍵。出現如下提示,可能會需要您輸入之前設置的passphrase

Warning: Permanently added 'github.com,192.30.252.128' (RSA) to the list of known hosts.
Enter passphrase for key '/home/flying/.ssh/id_rsa':
Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.

確認USERNAME是否是您GitHub賬戶用戶名,如果是,則表明連接成功。

[flying@lemp ~]$ ssh -T git@github.com
The authenticity of host 'github.com (192.30.252.128)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.252.128' (RSA) to the list of known hosts.
Enter passphrase for key '/home/flying/.ssh/id_rsa':
Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.
[flying@lemp ~]$

Fork Repository From GitHub

分爲兩種: 一種是在本地建空倉庫,再push到GitHub;另一種是再GitHub建立倉庫,使用clone複製到本地。

Create An Empty Repository

以下操作以名爲Empty的倉庫爲例,生成的HTTPS鏈接:https://github.com/UserName/empty.git,添加README.md

git init

echo '# empty' >> README.md
git add README.md 或 git add *

git commit -m "first commit"
git status #非必須操作
git remote add origin https://github.com/UserName/empty.git
git push -u origin master

Create Or Fork A Unempty Repository

以下操作以名爲Unempty的倉庫爲例,生成的HTTPS鏈接:https://github.com/UserName/unempty.git,添加README.md

git clone https://github.com/UserName/unempty.git

echo '# unempty' >> README.me
git add *

git commit -m 'add README.md'
git status
git push -u origin master

至此,所有操作完成。


References


Change Log

  • 2016.01.21 10:55 Thu Asia/Beijing
    • 初稿完成
  • 2017.02.03 12:06 Fri America/Boston
    • 添加push.default配置