Git是用於追蹤計算機文件變化、多人分工協作的版本控制系統,GitHub是基於Git的Web服務,除了提供Git的分佈式版本控制和源碼管理功能,還提供訪問控制、bug追蹤、任務管理、爲每個項目創建wiki頁面等功能,具體介紹見wikipedia

GitGitHub的配置、使用可查閱GitHub的官方幫助頁,網址如下

1
https://help.github.com/

另推薦閱讀Pro Git一書。

Sign Up GitHub Account

若要使用GitHub,需在其官方網站註冊帳號,只提供usernameemailpassword即可。爲保障帳號安全,官方建議驗證郵箱地址、採用兩重認證(2FA)。

相關幫助頁面如下

如果要配置個人Profile,可參閱如下幫助頁面

詳細的配置幫助頁

Git Installation

Git的官網下載頁爲https://git-scm.com/downloads。在GNU/Linux中,其安裝方式可分爲通過 包管理器安裝 和通過 源碼編譯安裝 2種。

通過包管理安裝的命令見 Download for Linux and Unix

1
2
3
4
5
6
7
8
#CentOS/RedHat
sudo yum -q -y install git
#Debian/Ubuntu
sudo apt-get -y install git
#OpenSUSE
sudo zypper in -y git

Source Code Compile Installation

Git的源碼託管在GitHub,在Release頁面可查看所有已釋出的版本。

源碼編譯安裝可參考如下文檔

源碼包中的安裝說明見 INSTALL

假設系統中已經安裝curltargzipgccautotoolsautoconfzlibopensslexpatlibiconv等命令。

1
2
3
4
5
#CentOS/RedHat
$ sudo yum -q -y install dh-autoreconf curl-devel expat-devel gettext-devel openssl-devel perl-devel zlib-devel
#Debian/Ubuntu
sudo apt-get -y install dh-autoreconf libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev

此處以版本v2.11.1爲例,假設下載路徑爲/tmp,安裝路徑爲/usr/local

以下爲源碼包的原始下載鏈接和真實下載鏈接

1
2
3
4
#原始下載鏈接
https://github.com/git/git/archive/v2.11.1.tar.gz
#重定向後的真實下載鏈接
https://codeload.github.com/git/git/tar.gz/v2.11.1

以下爲完整的操作命令

1
2
3
4
5
6
7
8
9
10
11
12
13
#刪除已存在的文件、創建目錄
[[ -f /tmp/git.tar.gz ]] && sudo rm -f /tmp/git.tar.gz
[[ -d /tmp/git ]] && sudo rm -rf /tmp/git/* || sudo mkdir /tmp/git/
#下載源碼包
curl -s# https://codeload.github.com/git/git/tar.gz/v2.11.1 -o /tmp/git.tar.gz
#解壓縮到指定目錄中
tar xf /tmp/git.tar.gz -C /tmp/git --strip-components=1
#編譯安裝
cd /tmp/git
make configure
./configure --prefix=/usr/local/
sudo make -j 4
sudo make install

操作完成後,執行如下命令以確定Git是否編譯安裝成功

1
git --version

如果顯示

git version 2.11.1

則說明安裝成功。如過提示找不到命令,則有可能目錄/usr/local/bin不在環境變量$PATH中,可通過如下命令將次路徑加入環境變量中

1
2
sudo bash -c 'echo "export PATH=$PATH:/usr/local/bin" > /etc/profile.d/git.sh'
. /etc/profile.d/git.sh

Git Setting

Git的設置可參考如下幫助文檔

通過git config命令對Git進行設置,配置文件保存路徑爲$HOME/.gitconfig。如果要修改配置可直接修改該配置文件,也可通過如下命令進行

1
git config --global --edit

完成設置後,可通過如下命令查看

1
git config --list

:此處的USERNAMEEMAIL爲註冊GitHub帳號時的用戶名和郵箱。

以下爲相關配置命令(全局,如果只針對單個repo中,在repo的目錄中執行如下操作,但不添加參數--global)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#Setting your username in Git
git config --global user.name "USERNAME"
#Setting your email in Git
#personal email or [email protected]
git config --global user.email "EMAIL"
#Setting the cache to timeout after 1 hour (setting is in seconds)
git config --global credential.helper 'cache --timeout=3600'
#Configure Git on Linux to properly handle line endings
git config --global core.autocrlf input
#Associating text editors with Git
git config --global core.editor vim
#Setting Push Method (simple|matching)
git config --global push.default simple

如果想在執行git config --global --edit命令時,自動調用圖形化編輯器進行配置,可參考Associating text editors with Git進行如下配置

1
2
3
4
5
6
7
8
# Atom Editor
git config --global core.editor "atom --wait"
# Sublime
git config --global core.editor "subl -n -w"
# TextMate
git config --global core.editor "mate -w"

如果要用GnuPG對commit進行數字簽名 (文檔),可在獲取gpg-key-id後,進行如下配置

1
2
#Signing commits with GPG
git config --global user.signingkey <gpg-key-id>

注意: 在生成GPG Key時,需要系統提供足夠的隨機數資源,通常系統提供的隨機數資源無法滿足操作要求,可通過haveged實現。具體見本人Blog Use Haveged & rng-tools To Speed Up Entropy For Random Number Generation On GNU/Linux

文件$HOME/.gitconfig中內容如下

1
2
3
4
5
6
7
8
9
10
11
[core]
editor = vim
autocrlf = input
[credential]
helper = cache --timeout=3600
[user]
name = USERNAME
email = EMAIL
signingkey = gpg-key-id
[push]
default = simple

Authenticating with GitHub from Git

GitHub從Git認證的方式有2種 link

  1. 通過HTTPS連接(官方推薦)
  2. 通過SSH鏈接

兩種方式的git地址展示如下

1
2
3
4
5
#HTTPS
https://github.com/git/git.git
#SSH
[email protected]:git/git.git

選用哪種方式可參閱官方幫助文檔 Which remote URL should I use?

以下是對2中方式的簡要說明

Via SSH

與HTTPS方式相比,SSH的優點是通過GPG密碼認證,無需像HTTPS一樣頻繁的輸入用戶名、密碼。不足之處:

  1. 必須登錄GitHub才能獲取個人Repo的SSH地址;
  2. 在某些應用場景下(如防火牆阻擋)無法正常連接到GitHub;
  3. 如果使用在GitHub中設置的個人Email,則其不能設爲private,必須取消勾選Keep my email address private;如果不想公開該Email,可選擇使用[email protected],官方幫助文檔見Keeping your email address private

若慾通過SSH連接Git,首先需在主機中創建SSH Keypair,並將生成的公鑰內容保存到GitHub中(Settings–>SSH and GPG Keys)。如何創建SSH Keypair,稍後介紹。

如果未創建SSH Keypair,或GitHub中沒有與本機對應的公鑰(Error: Permission denied (publickey) ),則在執行諸如

1
git clone [email protected]:git/git.git

的命令時,會報如下錯

Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

如果創建SSH Keypair時,設置了passphrase,則在執行如上命令時,會要求輸入對應的passphrase

Using SSH over the HTTPS port

但有時防火牆可能會拒絕SSH鏈接,如果仍想使用SSH進行操作,此時可通過HTTPS端口進行SSH連接。官方幫助文檔見 Using SSH over the HTTPS port

通過如下命令測試本機的SSH能否通過HTTPS端口進行連接

1
ssh -T -p 443 [email protected]

出現如下信息即表示成功

Hi USERNAME! You’ve successfully authenticated, but GitHub does not provide shell access.

爲啓用 SSH connections over HTTPS,可在SSH Client的配置文件$HOME/.ssh/config中添加如下內容

1
2
3
Host github.com
Hostname ssh.github.com
Port 443

通過如下命令測試

1
ssh -T [email protected]

出現如下信息即表示成功

Hi USERNAME! You’ve successfully authenticated, but GitHub does not provide shell access.

Via HTTPS

GitHub的Web頁默認只提供HTTPS地址,如果通過HTTPS連接Git,在執行

1
git clone https://github.com/git/git.git

時不會提示輸入認證信息,但在執行git pushgit pull時會要求輸入GitHub的用戶名和密碼,且每一次提交都會要求輸入。

爲避免每次提交都要輸入用戶名和密碼,通過設置credential.helper可將用戶名、密碼緩存一段時間(默認爲15min),具體見官方幫助文檔 Caching your GitHub password in Git

執行如下命令

1
2
#Setting the cache to timeout after 1 hour (setting is in seconds)
git config --global credential.helper 'cache --timeout=3600'

SSH Keypair Generating

官方幫助文檔如下

具體文檔

首先確保系統已經安裝了OpenSSH,若未安裝,可執行如下命令進行安裝

1
2
3
4
5
6
7
8
9
# CentOS
sudo yum -q -y install openssh openssh-server openssh-clients
# Debian/Ubuntu
sudo apt-get update
sudo apt-get install -y openssh-client openssh-server openssh-sftp-server
# OpenSUSE
sudo zypper in -y openssh

SSH的使用可參考本人Blog OpenSSH Configurations Optimization & Simple Usages

創建SSH Keypair須使用ssh-keygen命令,密鑰類型建議使用RSAED25519,不建議使用DSAECDSA。需要向後兼容的(如CentOS6.8)使用RSA,不需要向後兼容的使用ED25519

生成的密鑰默認保存$HOME/.ssh/中($HOME是用戶家目錄),如果該目錄不存在,則會自動創建,也可通過參數-f手動指定路徑。

執行如下命令創建認證密鑰

1
2
3
4
5
6
# RSA
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -N '' -C "Comments"
#D25519
# This is only compatible with OpenSSH 6.5+ and fixed-size (256 bytes).
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519 -N '' -C "Comments"

參數解釋

option explanation
-t Specifies the type of key to create.
-b bits Specifies the number of bits in the key to create.
-f Specifies the filename of the key file.
-C comment Provides a new comment.

參數-f如果不指定,則默認文件名爲~/.ssh/id_rsa~/.ssh/id_ed25519

Adding your SSH key to the ssh-agent

執行如下命令確保ssh-agent已經啓用

  1. 如果使用的是Git Bash,執行

    1
    2
    # start the ssh-agent in the background
    ssh-agent -s
  2. 如果使用的不是Git Bash,執行

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

否則會報如下錯

Could not open a connection to your authentication agent.

執行如下命令將SSH Key添加到ssh-agent

1
ssh-add ~/.ssh/id_ed25519

將文件~/.ssh/id_ed25519.pub中的內容添加到GitHub中(Settings–>SSH and GPG Keys)

執行如下命令嘗試連接

1
ssh -T [email protected]

演示過程如下

1
2
3
4
5
6
7
[email protected]:~$ ssh -T [email protected]
The authenticity of host 'github.com (192.30.253.113)' 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.253.113' (RSA) to the list of known hosts.
Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.
[email protected]:~$

如果需要通過HTTPS端口進行SSH連接,參見上文的 Using SSH over the HTTPS port部分。

Managing Remotes

遠程倉庫相關的幫助文檔見

如果在GitHub中創建了空倉庫,會出現如下信息

create a new repository on the command line

1
2
3
4
5
6
echo "# test" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/USERNAME/test.git
git push -u origin master

push an existing repository from the command line

1
2
git remote add origin https://github.com/USERNAME/test.git
git push -u origin master

常見操作命令

1
2
3
4
5
6
7
8
9
git clone https://github.com/USERNAME/RepoName.git
git add /PAHT/File
#or
git rm /PATH/File
git commit -m 'Commit Message'
git push

Error Occur

push.default warning

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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)

git push via ssh

1
2
3
remote: error: GH007: Your push would publish a private email address
remote: You can make your email public or disable this protection by visiting:
remote: http://github.com/settings/emails

Bibliography

Change Logs

  • 2017.02.22 18:29 Wed Asia/Shanghai
    • 初稿完成
  • 2017.05.19 23:31 Fri America/Boston
    • 添加通過SSH提交代碼且不用公開Email的設置