github上傳代碼

0.開頭

在github上註冊賬號並簡歷倉庫後,上傳本地數據前需要配置SSH,SSH用於進行身份認證(git是分佈式的代碼管理工具,遠程代碼管理是基於ssh的,所以使用遠程的git需要ssh的配置)。

關於windows:
如果你使用的是windows系統,建議使用 GitHub for Windows ,安裝這個軟件也會附帶Git Bash工具,並且比通過 git 命令進行版本管理方便的多。

1.git配置

windows用戶需要下載安裝Git Bash,linux通過終端安裝git。安裝完後對git進行配置(配置過的用戶可以通過git –lis命令查看配置):


$ git config --global user.name "xxx"
$ git config --global user.email "[email protected]"

2.SSH生成

在Git Bash或者終端中,輸入以下代碼,用你的github郵箱替換其中的郵箱地址:


ssh-keygen -t rsa -b 4096 -C "*[email protected]*"
# Creates a new ssh key, using the provided email as a label
Generating public/private rsa key pair.

當界面提示:”Enter a file in which to save the key,”時,直接回車使用默認存儲地址(默認地址爲:~/.ssh):


Enter a file in which to save the key (/Users/*you*/.ssh/id_rsa): *[Press enter]*

然後提示輸入密碼和確認密碼,此處可以按回車跳過。


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

然後可以看見提示,ssh生成到此完成。


Your identification has been saved in /home/<user>/.ssh/id_rsa.
Your public key has been saved in /home/<user>/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:********** <youemail>@123.com
The key's randomart image is:
******

ssh生成完成後會在~/.ssh文件夾看到兩個文件(默認存儲路徑時):

默認ssh路徑下

3.添加SSH key到ssh-agent

確定ssh-agent是否可用:

  • 如果你使用Git Bash或者終端,鍵入如下命令打開ssh-agent:
# start the ssh-agent in the background
$ eval "$(ssh-agent -s)"
Agent pid 59566
  • 如果你使用其他的終端,例如Git for Windows,請鍵入如下命令打開ssh-agent:
# start the ssh-agent in the background
$ eval $(ssh-agent -s)
Agent pid 59566

將SSH key添加到ssh-agent:


$ ssh-add ~/.ssh/id_rsa

4.在你的gitbug賬戶中添加一個新的SSH key

拷貝SSH key:

  • 通過xclip拷貝,在終端中執行以下命令:
$ sudo apt-get install xclip
# Downloads and installs xclip. If you don't have `apt-get`, you might need to use another installer (like `yum`)

$ xclip -sel clip &lt; ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard
  • 直接拷貝。通過編輯器打開id_rsa.pub,或者通過cat等命令直接查看id_rsa.pub文件,並且選中內容拷貝。

在你的github主頁選擇Settings->SSH and GPG keys->New SSH key or Add SSH key
在title中輸入這個ssh key的名稱,用於自己辨識。在key中粘貼剛剛複製的數據。
然後點擊Add SSH key,進行github密碼驗證後即可添加成功。

5.git常用操作

  • git init //初始化,在此文件夾中建立空的git庫
  • git clone url //同步遠程的庫
  • git add . //添加所有修改過的文件(僅添加,未上傳,或者說未保存到本地庫)
  • git commit -m “說明” //真正的上傳文件(保存到本地庫)
  • git remote add origin url //遠程上傳到指定地址
  • git push -u origin master    //上傳到遠程庫
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章