開發與維運

Windows環境下Git的相關配置與使用

1. 前提條件

  • 需要一個Github賬號,註冊地址:Github
  • 本地電腦需要安裝Git工具,下載地址:Git

2. 在GitHub創建一個repository項目

  • 進入Github首頁,點擊New repository新建一個項目
  • 填寫相應信息後點擊create即可

Repository name: 倉庫名稱

Description(可選): 倉庫描述介紹

Public, Private : 倉庫權限(公開共享,私有或指定合作者)

Initialize this repository with a README: 添加一個README.md

gitignore: 不需要進行版本管理的倉庫類型,對應生成文件.gitignore

license: 證書類型,對應生成文件LICENSE

3. 將Github上的repository項目克隆到本地

  • 在本地電腦選擇一個用於保存repository的目錄
  • 在該目錄下右擊,如果成功安裝Git工具將出現Git Gui Here和Git Bash Here,這裡我們選擇Git Bash Here
  • 在創建的repository頁面點擊Clone or download,複製SSH鏈接
  • 在Git Bash Here執行如下命令

    git colone [email protected]:Jeapwu/Notes.git
  • 如果出現如下錯誤

    fatal: Could not read from remote repository.
    Please make sure you have the correct access rights
    and the repository exists.
    fatal: Could not read from remote repository.Please make sure you have the correct access rights and the repository exists.

解決辦法:出現這個問題是因為沒有在github賬號添加SSH key,可以執行如下命令解決

在Git Bash repository執行

ssh-keygen -t rsa -C "username" (注:username為你Git Bash Here上顯示的用戶名)

出現出現如下提示,直接按Enter進入下一步

Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Jeapw/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

如果執行成功,出現生成的SSH-Key密鑰

Your identification has been saved in /c/Users/Jeapw/.ssh/id_rsa
Your public key has been saved in /c/Users/Jeapw/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:cPqCkycrWsZrxmr/87kvKbL8DeOQk2W3NAHywJ+l/i4 Jeapw
The key's randomart image is:
+---[RSA 3072]----+
|  .o .           |
|   .+ ..         |
|    ..=..        |
|     + +.        |
|    .o.+S        |
| .  =+o.o        |
| .+==o+.o        |
| +*o+E+=.        |
|+=o=*+**+.       |
+----[SHA256]-----+

然後在/c/Users/Jeapw/.ssh/id_rsa.pub中複製生成的密鑰

回到Github網站的"account settings",依次點擊"Setting" -> "SSH Keys"->"New SSH key",Title處填寫“id_rsa.pub”或其他任意信息。 key處原樣拷貝複製的生成密鑰,至此Github的SSH-Key綁定完成

  • 如果出現如下錯誤

    Permission denied (publickey)

解決辦法:默認使用id_rsa嘗試連接,如果你在新建祕鑰的時候使用了自定義的名稱,比如github_rsa,你需要在。/ssh目錄下再配置一個config文件

Host github.com
 HostName github.com
 User git
 IdentityFile ~/.ssh/github_rsa
  • 最後進入打開Git Bash Here的目錄便可以執行git clone

4. 將本地項目上傳GitHub的repository中

  • 執行命令

    git add .        (注:別忘記後面的.,此操作是把新添加到文件夾下面的文件都添加進來)
  • 執行命令

    git commit  -m  "提交信息"  (注:“提交信息”裡面換成你需要,如“first commit”)
  • 如果出現錯誤

    $ git commit -m "2020-5-17-WU"
    *** 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 'Jeapw@DESKTOP-KJ8KJ9H.(none)')

錯誤解決:是因為在創建git文件夾的時候信息不完善導致的,按照提示添加郵箱與用戶名即可

git config --global user.email "[email protected]"
git config --global user.name "Your Name"
  • 執行命令

    git push -u origin master   (注:此操作目的是把本地倉庫push到github上面,此步驟需要你輸入帳號和密碼)
  • 如果出現如下提示,而事實本地項目並沒有上傳repository,則是沒有執行git commit -m "提交信息"所致

    git push -u origin master
    Everything up-to-date
    Branch 'master' set up to track remote branch 'master' from 'origin'.

參考博客:

【1】Git的使用--如何將本地項目上傳到Github(兩種簡單、方便的方法)

【2】git "Could not read from remote repository.Please make sure you have the correct access rights."解決方案

【3】github提示Permission denied (publickey),如何才能解決?

【4】git Please tell me who you are解決方法

Leave a Reply

Your email address will not be published. Required fields are marked *