Ubuntu 中安裝Git及其相關的配置

Ubuntu中安裝Git及其相關的配置

Github的服務主要用來託管代碼和協同寫作。對於我等非碼農用來在Ubuntu中同步和安裝相關軟件的最新版本(現在許多軟件的最新版都託管到Github上了)也是很有用的。本文記錄在Ubuntu中安裝Git並託管和安裝代碼軟件的簡單步驟筆記。不同其他軟件,在Ubuntu中安裝Git不會產生圖標~

下載和安裝Git軟件

$ sudo apt-get install git

配置Git軟件並基於SSH生產密鑰

首先在Git軟件中配置自己在Github上建立的帳號的用戶名和郵件地址。如果沒有可以到Github上去註冊先。註冊完成的可以跳過

$ git config --global user.name "Your Name Here"
# Sets the default name for git to use when you commit
$ git config --global user.email "[email protected]"
# Sets the default email for git to use when you commit

然後檢查系統中是否已經有了一個SSH密鑰:

$ cd ~/.ssh
# Checks to see if there is a directory named ".ssh" in your user directory


如果顯示“No such file or directory”,則沒有,需要重新建立。如果有的話,可以先備份老的密鑰然後移除:

$ ls
# Lists all the subdirectories in the current directory
# config  id_rsa  id_rsa.pub  known_hosts
mkdir key_backup
# Makes a subdirectory called "key_backup" in the current directory
cp id_rsa* key_backup
# Copies the id_rsa keypair into key_backup
rm id_rsa*
# Deletes the id_rsa keypair

然後產生一個新的SSH密鑰。當提示“Enter a file in which to save the key”時僅僅按Enter就可以保持默認設置,即存在自己的家目錄下(“you”換成自己的Ubuntu帳號名,下同)。郵件地址與前面的和Github帳號中的相同:


$ ssh-keygen -t rsa -C "[email protected]"
# Creates a new ssh key using the provided email
# Generating public/private rsa key pair.
# Enter file in which to save the key (/home/you/.ssh/id_rsa):

回車後會提示輸入密碼並確認密碼:

Enter passphrase (empty for no passphrase): [Type a passphrase]
# Enter same passphrase again: [Type passphrase again]

然後看到類似這樣的信息即表明密鑰生成成功了:

Your identification has been saved in /home/you/.ssh/id_rsa.
# Your public key has been saved in /home/you/.ssh/id_rsa.pub.
# The key fingerprint is:
# 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db [email protected]

遇到的問題

在Ubuntu下使用ssh命令連接github.com的SSH服務,登錄名爲[email protected](所有GitHub用戶共享此SSH用戶名)。

ssh -T [email protected]
執行之後提示:Permission denied (publickey).
把 ~/.ssh /id_rsa.pub   文件中的公鑰添加到下面SSH_Keys

這說明我們還沒有在GitHub賬戶中正確設置公鑰認證,如下圖所示:



然後就可以嘗試着檢測一下SSH連接git到Github服務器了:

$ ssh -T [email protected]
# Attempts to ssh to github

可能會看到這樣的警告,但沒有關係,輸入“yes”回車:

The authenticity of host 'github.com (207.97.227.239)' 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)?

最後就可以看到這樣的連接成功的消息了:

Hi username! You've successfully authenticated, but GitHub does not
# provide shell access.
 

簡單的上傳下載方法

上傳一個文件到Github,先需要在Github建立一個repo,然後上傳。例如建立“Hello-world”文件並上傳到剛建立的repo:

$ mkdir ~/Hello-World
# Creates a directory for your project called "Hello-World" in your user directory
$cd ~/Hello-World
# Changes the current working directory to your newly created directory
$git init
# Sets up the necessary Git files
# Initialized empty Git repository in /Users/you/Hello-World/.git/
$touch README
# Creates a file called "README" in your Hello-World directory


上傳建立的Hello-world文件,其中Hello-World爲剛開始在Github上建立的repo:

在git本地庫裏打開命令行

$ git status

其中會遇到本地落後於庫裏的版本,這樣就要

$ git pull

$ git add README
# Stages your README file, adding it to the list of files to be committed
git commit -m 'first commit'
# Commits your files, adding the message "first commit"
$ git remote add origin https://github.com/username/Hello-World.git
# Creates a remote named "origin" pointing at your GitHub repo
$ git push origin master
# Sends your commits in the "master" branch to GitHub
 

如果看到別人的好東西需要下載的話,先Fork別人的repo,然後Clone到你的電腦(當然也可以不Fork直接的Clone):

$ git clone https://github.com/username/Spoon-Knife.git
# Clones your fork of the repo into the current directory in terminal

本文內容參考Github,當然這是最最基礎的,其他的可以參考Github上的更詳細的教程。


更新本地代碼,並放棄本地的提交和改動:

git fetch --all
git reset --hard origin/master

git fetch 只是下載遠程的庫的內容,不做任何的合併 git reset 把HEAD指向剛剛下載的最新的版本



發佈了37 篇原創文章 · 獲贊 11 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章