Gitee初探

學習路上的一顆學渣發表於2020-12-26

公司開始用git處理提交了,因為是給我們搭建好的環境,直接用repo提交了,初次在windows下用git的時候難免遇到一些問題。現在記錄一下。

git和svn的區別就不說了。為什麼不用github,也是因為不想太麻煩。

 

# 背景

STM32F401的主控,初次用了STM32CUBE MX IDE開發,但是編輯器環境不好,所以後面用了 visual studio code。

 

# 搭建

  1. 首先在git上註冊一個賬號。新建一個倉庫。略。
  2. 本地下載git 64bit.exe,全程下一步安裝。
  3. 用visualcode開啟命令提示行 Ctrl + Alt + ~ ,輸入git確定是否有安裝成功。

  4. 配置Git提交的名稱和郵箱,此時配置資訊在 C:\Users\[username]\.gitconfig 檔案裡面可以看到
    > git config --global user.name "xxxx"
    > git config --global user.email "xxxx@xxx.com"
  5. 生成ssh公鑰,輸入下列命令,一路回車,此時生成的金鑰資訊在 C:\Users\[username]\.ssh 資料夾中可以看到
    > ssh-keygen -t rsa
    Generating public/private rsa key pair.
    Enter file in which to save the key (C:\Users\[username]/.ssh/id_rsa): 
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in C:\Users\[username]/.ssh/id_rsa.
    Your public key has been saved in C:\Users\[username]/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:va###lue###value###value### [username]@[計算機名稱]
    The key's randomart image is:
    |                 |
    |                 |
    |                 |
    |                 |
    |                 |
    |                 |
    |                 |
    |                 |
    |                 |
    +----[SHA256]-----+

     

  6. 保留金鑰資訊,避免每次都需要輸入密碼 
    > git config --global credential.helper store

     

  7. 將 C:\Users\[username]\.ssh\id_rsa.pub 檔案內容全部拷貝在gitee的ssh公鑰裡面


     
  8. 在gitee新建一個倉庫
    設定-倉庫空間管理-新建倉庫
  9. 切換到工程所在目錄,初始化本地倉庫
    > git init

     

  10. 關聯遠端倉庫

    > git remote add origin https://gitee.com/使用者個性地址/倉庫地址

     

  11. 新增所有檔案並且提交。
     

    > git add .
    > git commit -m "註釋"
    > git push origin master

    在git add . 時報 fatal: adding files failed ,可以通過下指令處理

    > git add --ignore-errors .

    push時需要輸入帳號密碼:


    在push時可能會報錯:

    > git push origin master
    To https://gitee.com/個性地址/倉庫地址
     ! [rejected]        master -> master (fetch first)
    error: failed to push some refs to 'https://gitee.com/個性地址/倉庫地址'
    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.

    可以通過傳送下述命令:

    > git pull origin master
    > git push origin master

    此時還是報錯:

    > git push origin master
    To https://gitee.com/個性地址/倉庫地址
     ! [rejected]        master -> master (non-fast-forward)
    error: failed to push some refs to 'https://gitee.com/個性地址/倉庫地址'
    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.

    強制推送:

    > git push -f origin master
    Enumerating objects: 426, done.
    Counting objects: 100% (426/426), done.
    Delta compression using up to 16 threads
    Compressing objects: 100% (401/401), done.
    Writing objects: 100% (426/426), 6.70 MiB | 3.97 MiB/s, done.
    Total 426 (delta 119), reused 0 (delta 0), pack-reused 0
    remote: Resolving deltas: 100% (119/119), done.
    remote: Powered by GITEE.COM [GNK-5.0]
    remote: error: GE007: Your push would publish a private email address.
    remote: You can make your email public or disable this protection by visiting:
    remote: https://gitee.com/profile/emails
    remote: error: hook declined to update refs/heads/master
    To https://gitee.com/個性地址/倉庫地址
     ! [remote rejected] master -> master (hook declined)

    可以看到是郵箱問題,因為我隱藏了私人郵箱,取消第二個勾選即可。