git常用技巧2::如何用git與Github連接來管理代碼

Git是一款開源的分佈式版本控制系統,而GItHub是依託Git的代碼託管平臺

準備工作

一、註冊賬號

進入GitHub官網首頁,GitHub官網網址:https://github.com/,其中需要設置
賬號:liuzy1
密碼:************
郵箱:*************@163.com

二、設置SSH Key

由於本地GIt倉庫與GitHub網站倉庫之間的傳輸通過SSH加密的,這時需要設置SSH Keys。

1.創建SSH Key

首先在用戶主目錄下查看手否有.ssh目錄,

cd ~/.ssh
ls

查看是否存在id_rsa,id_rsa.pub兩個文件。
如果沒有,則運行

ssh-keygen -t rsa -C "*********@163.com"

運行此命令後,一直回車,會生成/home/grq/…ssh文件夾,裏面有id_rsa,id_rsa.pub。它們是SSH Key的祕鑰對。其中id_rsa是私鑰,不能泄露,id_rsa.pub是公鑰,可以告訴其他人
整個過程如下:

lzy@lzy-Lenovo-ideapad-Y700-17ISK:~$ cd ~/.ssh
lzy@lzy-Lenovo-ideapad-Y700-17ISK:~/.ssh$ ls
known_hosts
lzy@lzy-Lenovo-ideapad-Y700-17ISK:~/.ssh$ ssh-keygen -t rsa -C "*********@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/lzy/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/lzy/.ssh/id_rsa.
Your public key has been saved in /home/lzy/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:5d9cB2PudrUbExxqTxpA0l0KglM0iUL/I4Um2Gx9NtI changan_03053399@163.com
The key's randomart image is:
+---[RSA 2048]----+
|    ..  =*oo. .. |
|    +.o+o.=....  |
|   . =.*.E.. .+. |
|    . o *o. .ooo.|
|       .So.  +.++|
|        . ...+=.=|
|            ..==.|
|             . .+|
|               . |
+----[SHA256]-----+
lzy@lzy-Lenovo-ideapad-Y700-17ISK:~/.ssh$ ssh -v git@github.com
OpenSSH_7.2p2 Ubuntu-4ubuntu2.7, OpenSSL 1.0.2g  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 [52.74.223.119] port 22.
debug1: Connection established.
debug1: identity file /home/lzy/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /home/lzy/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/lzy/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/lzy/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/lzy/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/lzy/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/lzy/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/lzy/.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-4ubuntu2.7
debug1: Remote protocol version 2.0, remote software version babeld-9d924d26
debug1: no match: babeld-9d924d26
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: rsa-sha2-512
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 /home/lzy/.ssh/known_hosts:1
Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-rsa,rsa-sha2-512,rsa-sha2-256,ssh-dss>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/lzy/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Trying private key: /home/lzy/.ssh/id_dsa
debug1: Trying private key: /home/lzy/.ssh/id_ecdsa
debug1: Trying private key: /home/lzy/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).
lzy@lzy-Lenovo-ideapad-Y700-17ISK:~/.ssh$ ssh-agen -s
No command 'ssh-agen' found, did you mean:
 Command 'ssh-agent' from package 'openssh-client' (main)
ssh-agen: command not found
lzy@lzy-Lenovo-ideapad-Y700-17ISK:~/.ssh$ ssh-agent -s
SSH_AUTH_SOCK=/tmp/ssh-zG7300szo7zJ/agent.5421; export SSH_AUTH_SOCK;
SSH_AGENT_PID=5422; export SSH_AGENT_PID;
echo Agent pid 5422;
lzy@lzy-Lenovo-ideapad-Y700-17ISK:~/.ssh$ ssh-add ~/.ssh/id_rsa
Identity added: /home/lzy/.ssh/id_rsa (/home/lzy/.ssh/id_rsa)

2.在Github端設置,SSH Key

登錄GitHub,點擊右上角頭像,Settings -> Personal settings -> SSH and GPG keys。在SSH Keys標籤右方點擊New SSH Key。
彈出兩個文本框。其中的Title,可以隨意命名。筆者此處隨便命名爲grq-Ubuntu。
另一個Key文本框,需要輸入剛剛生成的id_rsa.pub文件中的內容。粘貼後點擊Add SSH Key,即可生成SSH Key。
在這裏插入圖片描述

三、上傳項目

1.準備工作

ubuntu安裝Git:sudo apt-get install git
查看Git版本信息:git version
配置Git 用戶信息

lzy@lzy-Lenovo-IdeaPad-S500:~$ git config --global user.name"liuzy1"
lzy@lzy-Lenovo-IdeaPad-S500:~$ git config --global user.email"*********@163.com"

進入到我們需要上傳項目所在文件夾,打開終端,使用git的初始化命令

git init

這個命令可以把當前目錄變成git可以管理的倉庫。
現在我們就可以進行git操作將項目上傳到GitHub了。

2.添加需要上傳的文件

上傳之前,我們需要添加上傳的文件。這裏使用到的指令是:

git add file

其中,file是我們想要添加的文件。這裏筆者想要將整個文件夾內容都添加進去,所以此處筆者輸入的指令如下:

git add ./

3.檢查當前git 狀態

我們可以使用git指令,查看當前git的狀態。指令如下:

git status

該指令可以查看當前的分支以及添加文件的情況。

4.commit 推送

將緩存區的修改提交到本地倉庫。指令如下:

git commit -m "Update Readme Files(Version of Chinese & English)"

-m後的內容是筆者添加的描述。由於筆者這次是更新該倉庫,更新內容是中文與英文的Readme文件
若出現錯誤:

** Please tell me who you are.
Run
  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'XXX@YYY.(none)')

這時候的解決辦法是,在進行git add ./操作的路徑中,實際上已經生成了一個隱藏的.git文件夾。在該路徑下輸入指令cd ./.git便進入.git文件夾,使用gedit或vim打開文件config,在文件末尾加入內容:

[user]
	email=*********@163.com
	name=liuzy1

填的GitHub賬號中的Email和用戶名。填好後如圖所示:
在這裏插入圖片描述這時候再進行commit指令,應該就可以成功了。完成後就可以等待提交了。

5.添加文件到遠程庫

構建倉庫,得到該倉庫的SSH Key 向其上傳內容。進入新建的倉庫,點擊Clone or download,在彈出的Clone with SSH框中點擊小按鈕Copy to clipboard。如下圖所示:
在這裏插入圖片描述1.添加 一個遠程倉庫,命名爲origin,這樣隨後纔可以添加文件到遠程庫中。指令如下:

git remote add origin git@github.com:liuzy1/god.git

如果此時出現錯誤:fatal: remote origin already exists. 這時候說明遠程倉庫已經存在。這時候需要先刪除origin倉庫,然後再重新添加該遠程倉庫。指令如下:

git remote rm origin
git remote add origin git@github.com:liuzy1/god.git

2.添加文件到遠程庫

git remote set-url origin git@github.com:liuzy1/god.git

3.push指令進行上傳。
if 如果該倉庫是第一次進行push,則指令如下:

git push origin master

當執行到push時,就會報錯,報錯代碼如下:

$ git push origin master
To git@github.com:liuzy1/sensor_fusion.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]:liuzy1/sensor_fusion.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解決方法:
出現這個問題是因爲github中的README.md文件不在本地代碼目錄中,可以通過如下命令進行代碼合併

git pull --rebase origin master

然後再

git push origin master

else 之前傳輸過,此處爲更新

git push

case2 :
有時候運行了git push origin master遇到這樣的錯誤:

To git@github.com:liuzy1/shenlancollege_laser_slam.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:liuzy1/shenlancollege_laser_slam.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解決方法:
push前先將遠程repository修改pull下來

$ git pull origin master
$ git push -u origin master

最後在repository後面添加readme.md也是可以的。
push結束後,在GitHub端的對應倉庫上刷新一下,內容就改變了。說明項目內容已經上傳成功了~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章