Notes|Git|常用筆記

d.l.spm
10 min readApr 19, 2020

--

Git 筆記

  • 實用指令
  • Push 本地專案
  • 刪除本地文件後從重新 pull、修改後在 push
  • Commit Message
  • 刪除 push commit
  • 刪除 github 檔案
  • 斷頭(detached HEAD)
  • Error

實用指令

Git 筆記思維導圖

使用者設定

git config --global user.name 'input your name'
git config --global user.email inpuy_your_name@xx.com.tw

上方 global 是全域設定,如果想要不同專案設定不同 github 可以使用 local

git config --local user.name 'input your name'
git config --local user.email inpuy_your_name@xx.com.tw
//檢視設定
git config — list
//查看commit歷史紀錄
git log --oneline

Push 本地專案

  • git hub 設定

new repository

右上角 -> new repository -> Create repository
  • terminal 設定

初始化一個本地儲存庫

$ git init

選擇我們 github 頁面的 …or push an existing repository from the command line 部分的 code

$ git remote add origin https://github.com/spmdl/Blog_Basic.git

添加所有文件

$ git add --all

提交代碼到 HEAD

$ git commit -m "first push"

提交到遠端倉庫

$ git push -u origin master

回到 github 重新整理就可以了。

發現資料夾變成子模塊,但不知道 url 所以不能打開
  • Debug (兩步驟解決)

刪除本地文件後從重新 pull

會強行 pull 覆蓋本地端文件

$ git fetch --all  
$ git reset --hard origin/master
$ git pull

修改後在 push

$ git status #查看是否有修改
$ git add .
$ git commit -m ""
$ git push origin master

Fork

第一步:到大神專案按下 Fork

Fork 的頁面稍等一下好了就會跳到自己帳號底下了

第二步:Fork 到自己帳號底下後 clone 下來(記得是自己帳號底下的專案)

第三步:修改

第四步:Push

$ git status #查看是否有修改
$ git add .
$ git commit -m ""
$ git push

第五步:PR(Pull Request)

在自己專案底下 -> New pull request (點選)-> 選擇 Branch + 說明相關資訊 ->Create pull request

分支 branch

建立分支

$ git branch [branch_name]

顯示所有分支

$ git branch

切換分支

$ git checkout [branch_name]

建立與切換分支

$ git checkout -b [branch_name]

合併分支

$ git checkout master
$ git merge [branch_name]
# 取消合併
$ git reset --hard HEAD~

刪除分支

$ git branch -d [branch_name]

rebase 合併(歷史紀錄顯示的會更清楚)

$ git checkout [branch_name]
$ git rebase master

參考:

Commit Message

規範

Header: <type>(<scope>): <subject>
- type: 代表 commit 的類別:feat, fix, docs, style, refactor, test, chore,必要欄位。
- scope 代表 commit 影響的範圍,例如資料庫、控制層、模板層等等,視專案不同而不同,為可選欄位。
- subject 代表此 commit 的簡短描述,不要超過 50 個字元,結尾不要加句號,為必要欄位。
Body: 72-character wrapped. This should answer:
* Body 部份是對本次 Commit 的詳細描述,可以分成多行,每一行不要超過 72 個字元。
* 說明程式碼變動的項目與原因,還有與先前行為的對比。
Footer:
- 填寫任務編號(如果有的話).
- BREAKING CHANGE(可忽略),記錄不兼容的變動,
以 BREAKING CHANGE: 開頭,後面是對變動的描述、以及變動原因和遷移方法。

type: subject 是簡述不要超過 50 個字元
Body:每一行不要超過 72 個字

範例

src : https://wadehuanglearning.blogspot.com/2019/05/commit-commit-commit-why-what-commit.html
  • type 說明

feat: 新增/修改功能 (feature)。
fix: 修補 bug (bug fix)。
docs: 文件 (documentation)。
style: 格式 (不影響程式碼運行的變動 white-space, formatting, missing semi colons, etc)。
refactor: 重構 (既不是新增功能,也不是修補 bug 的程式碼變動)。
perf: 改善效能 (A code change that improves performance)。
test: 增加測試 (when adding missing tests)。
chore: 建構程序或輔助工具的變動 (maintain)。
revert: 撤銷回覆先前的 commit 例如:revert: type(scope): subject (回覆版本:xxxx)。

步驟:

1. 先到 issue Assign 給自己
2.

完整範例參考:

刪除 push commit

# 1.通过找到想要退回到的commit_id
$ git log
# 2.本地回到上一个commit_id
$ git reset --hard <commit_id>
# 3.推送到服务器,一定要加 --force 参数
$ git push origin HEAD:master --force

刪除 github 檔案

cached:只作用於 github 上
可以用*來選擇全部的副檔名:cached *.sql

# 刪除名為 .idea folder
git rm -r --cached .idea

# 提交到git
git commit -m 'delete .idea dir'

# push GitHub
git push -u origin master
# 或者直接 command+shift+K

斷頭(detached HEAD)

HEAD 會指向某一個分支,而分支會指向某一個 Commit。但 HEAD 偶爾會發生「沒有指到某個分支」的情況,這個狀態的 HEAD 便稱之「detached HEAD」。

  • 解決:建立分支指向他
git branch tiger
git branch tiger b6d204e
//要切換到遠端分支而不呈現 detached HEAD 狀態,可以加上 --track 或 -t
git checkout -t origin/refactoring
//離開 detached HEAD 狀態
git checkout master

Error

push 時出現 failed to push some refs to

參考:git push错误failed to push some refs to的解决

$ git pull --rebase origin master

GitHub 上資料夾顯示灰色無法點選

參考:GitHub 上資料夾顯示灰色無法點選

$ git rm -r --cached "資料夾的名稱" 
$ git commit -m "更新log"
$ git push -u origin master
# 再重新 add/commit/push

Could not read from remote repository

四步驟解決,沒有 ssh-key

step1: C 後面是自己的 github 設定的 mail

ssh-keygen -t rsa -C xxx@xxx.com

step2: 一直按 Enter
step3:尋找產生的 ssh key (打開複製裡面的內容)

\Users\用戶名\.ssh\id_rsa.pub

step4: 回到 github → New SSH key → 貼上
https://github.com/settings/keys

--

--

d.l.spm
d.l.spm

No responses yet