Ubuntu系統如何安裝和配置Git


一、Git安裝:

  1、 二進制方式安裝:

  $ sudo apt-get install git-core

  安裝完成后,在終端中輸入 git 就可以看到相關的命令了。如果只是需要使用git來管理本地的代碼,那么現在 就 可 以使用了。如果需要和github上的項目結合,還需要做其他的一些操作。

  2、github帳號的申請

  如果只是需要將github上感興趣的代碼拷貝到本地,自己進行修改使用,而不打算共享發布的話,其實不申請 帳號也沒有關系,只需要 git clone 代碼到本地就可以了。 $ git clone git:// IP work(工作目錄名)。

我下載的是https://github.com/narendraprasath/Word-Segmentation-in-NLP-Python,只要這個命令就可以:git clone git://github.com/narendraprasath/Word-Segmentation-in-NLP-Python

  畢竟使用 github 就是為了開源的目的,首先去 github.com 上注冊一個帳號。

  3、在本地建立一個文件夾,然后做一些全局變量的初始化

  $ git config --global user.name = “用戶名或者用戶ID”

  $ git config --global user.email = “郵箱“

  這兩個選項會在以后的使用過程中自動添加到代碼中。

  4、創建驗證用的公鑰

  這個是比較復雜和困擾大多數人的地方,因為 git 是通過 ssh 的方式訪問資源庫的,所以需要在本地創建驗證 用的文件。使用命令:$ ssh-keygen -C ”you email address@gmail.com“ -t rsa會在用戶目錄 ~/.ssh/ 下建立相應 的密鑰文件。可以使用 $ ssh -v git@github.com 命令來測試鏈接是否暢通。

  5、上傳公鑰

  在 github.com 的界面中 選擇右上角的 Account Settings,然后選擇 SSH Public Keys ,選擇新加。

  Title 可以隨便命名,Key 的內容拷貝自 ~/.ssh/id_rsa.pub 中的內容,完成后,可以再使用 ssh -v git@github.com 進行測試。

我的連不上github,出現下面錯誤:

OpenSSH_7.2p2 Ubuntu-4ubuntu1, OpenSSL 1.0.2g-fips 1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to github.com [192.30.252.131] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu1
debug1: Remote protocol version 2.0, remote software version libssh-0.7.0
debug1: no match: libssh-0.7.0
debug1: Authenticating to github.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /root/.ssh/known_hosts:5
Warning: Permanently added the RSA host key for IP address '192.30.252.131' to the list of known hosts.
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: \342\200\2301056739724@qq.com\342\200\231
debug1: Server accepts key: pkalg ssh-rsa blen 279
sign_and_send_pubkey: signing failed: agent refused operation
debug1: Offering RSA public key: /root/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).

然后我在知乎上找到問題:

如果你曾經出現過這種問題,我有一個解決方案
首先,清除所有的key-pair
ssh-add -D
rm -r ~/.ssh
刪除你在github中的public-key

重新生成ssh密鑰對
ssh-keygen -t rsa -C "xxx@xxx.com"
chmod 0700 ~/.ssh
chmod 0600 ~/.ssh/id_rsa*

接下來正常操作
在github上添加公鑰public-key:
1、首先在你的終端運行 xclip -sel c ~/.ssh/id_rsa.pub將公鑰內容復制到剪切板
2、在github上添加公鑰時,直接復制即可
3、保存

測試:
在終端 ssh -T git@github.com
成功,出現下面信息

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

 

第二次,又出現問題:

Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.
sign_and_send_pubkey: signing failed: agent refused operation
Permission denied (publickey).

這時是用這個答案解決的

https://help.github.com/articles/error-agent-admitted-failure-to-sign/

 

  二、Git配置與使用

  利用 github 來管理自己的項目,可以按照下面的步驟進行

 1建立遠端倉庫

    在 Github 創建賬號后,點擊 New Repository,填寫一些本倉庫相關的信息,如倉庫名稱、描述、是否公開。設置完畢后,點擊 Create repository 即可創建新倉庫。如,建立olsr。

  2、建立本地倉庫

  在需要建立項目的文件夾中,使用 git init 進行倉庫的建立。完成后,可以看到文件家中多了一個 .git 隱藏目 錄。

  2、添加文件

  使用 git add 。 來進行初始文件的添加。這里 。 表示將文件夾下所有的文件都添加進去,我們也可以指定文件進 行添 加。

     如果輸入$ Git remote add origin git@github.com:djqiang(github帳號名)/gitdemo(項目名).git 

    提示出錯信息:fatal: remote origin already exists.

    解決辦法如下:

    1、先輸入$ git remote rm origin

    2、再輸入$ git remote add origin git@github.com:djqiang/gitdemo.git 就不會報錯了!

  3、提交文件

  使用 git commit -m ‘comment’ 提交,可以將編輯的內容進行提交。

  4、刪除或增加github遠程來源

  git remote add origin https://github.com/Git-Elite/CodeBase.git //藍色部分為github托管的倉庫地址

  5、提交至github倉庫

  git push -u origin master

//報錯了:
提示:更新被拒絕,因為遠程版本庫包含您本地尚不存在的提交。這通常是因為另外
提示:一個版本庫已向該引用進行了推送。再次推送前,您可能需要先整合遠程變更
提示:(如 'git pull ...')。
提示:詳見 'git push --help' 中的 'Note about fast-forwards' 小節。

//強制推送
git push origin +master

  上面就是Ubuntu安裝和配置Git的方法介紹了,在使用Git前,需要先建立一個github倉庫,之后就能使用git管理Linux項目了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM