一般開發中在 Mac 上開發程序,並使用 Git 進行版本管理,在使用 React 編寫 Component 時,組件名一般建議首字母大寫。
一般開發中在 Mac 上開發程序,並使用 Git 進行版本管理,在使用 React 編寫 Component 時,組件名一般建議首字母大寫。
「有些同學對 React 組件的文件進行命名時,剛開始是小寫,後來為了保持團隊一致,又改成了大寫,然而 git 不會發現大小寫的變化,此時就出了問題。」
再梳理一遍這個邏輯:
來重現一下犯錯的這個過程:
# 剛開始 test 文件是由內容的
~/Documents/ignorecase-test(master ?) cat test
hello
# 把 test 文件改成首字母大寫的 Test 文件
~/Documents/ignorecase-test(master ?) mv test Test
# 注意此時 git status 並沒有發生改變
~/Documents/ignorecase-test(master ?)
~/Documents/ignorecase-test(master ?) git ls-files
test
~/Documents/ignorecase-test(master ?) ls
Test
解決方案
通過 git mv,在 Git 暫存區中再更改一遍文件大小寫解決問題
$ git mv test Test
但是修改文件夾時會出現一些問題:
fatal: renaming 'dir' failed: Invalid argument
使用下邊這個笨辦法修改:
$ git mv dir DirTemp
$ git mv DirTemp Dir
預防方案
那有沒有什麼預防措施?
「Git 默認是忽略大小寫的,如果改成不忽略大小寫是不就可以了?不行,這樣會產生更麻煩的問題。」
更改為不忽略大小寫
[core]
ignorecase = false
以下是產生的問題:
~/Documents/ignorecase-test(master ?) ls
test
~/Documents/ignorecase-test(master ?) mv test Test
~/Documents/ignorecase-test(master ?ls)
Test
~/Documents/ignorecase-test(master ?) git status
On branch master
Untracked files:
(use "git add
..." to include in what will be committed) Test nothing added to commit but untracked files present (use "git add" to track) ~/Documents/ignorecase-test(master ?) git add -A ~/Documents/ignorecase-test(master ?) git ls-files Test test ~/Documents/ignorecase-test(master ?) git rm test rm 'test' ~/Documents/ignorecase-test(master ?) git add -A ~/Documents/ignorecase-test(master ?) git ls-files ~/Documents/ignorecase-test(master ?)
總結
使用 git mv -f 和 mv 同時更改文件名,避免本地文件系統與倉庫中代碼不一致。
本文地址:https://www.linuxprobe.com/git-capitalize-letter.html
特別聲明:以上內容(如有圖片或視頻亦包括在內)為自媒體平臺「網易號」用戶上傳並發布,本平臺僅提供信息存儲服務。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.