Git commit


設置Commit郵箱和用戶名

git中每次commit時git都會自動在這次commit中添加提交人信息,用來mark這次commit是誰提交的,並記錄該人的郵箱,否則你的同時看到commit歷史記錄他怎么知道這是誰提交的呢。
所以git要求必須要進行用戶名和用戶郵箱設置,否則不允許你提交。設置方式如下:

Global 設置

>> git config --global user.name yourName

>> git config --global user.email yourEmail

一旦這樣設置后,在該計算機賬戶下所有倉庫使用git commit的時候git都默認用這一組郵箱和用戶名來mark你的commit信息

但是如果在同一個計算機賬戶下由於有多個人在工作或其他原因導致你對某個項目提交的時候需要用單獨的郵箱和用戶名提交怎么辦呢?git中允許你正對單獨的project進行設置,設置方法如下:

當前Project設置

>> git config user.name yourName

>> git config user.email yourEmail

上面的命令如果不跟yourName或者yourEmail參數的話就是查詢,跟了參數的話就是設置命令

 

Commit Message編寫指南

commit的時候message請盡量的清晰明了,便於查看和溯源,閱讀如下文章學習正確的git提交姿勢:

詳見鏈接:Git 提交的正確姿勢:Commit message 編寫指南

 

使用VIM編輯commit注釋信息

在命令輸入模式下面,輸入字母”i”,則VIM進入到插入模式,接着輸入自己的注釋內容;

完成注釋后需要退出:

1)按鍵Esc,如果無效,連續按兩次

2)當底部提示行出現空白時,輸入冒號“:”

3)再輸入字母“q”,回車 (輸入wq,為保存退出)

但是實際上使用vim非常不方便,

好吧,我是在設置其他編輯器失敗后沒有辦法才有那么幾天被迫使用了vim…

修改默認編輯器

關於默認編輯器最好是安裝git之前你就已經安裝了相應的編輯器比如notepad++或VS Code,這樣就可以在安裝git的時候直接在安裝配置界面中配置。如果在安裝完了git后再要修改默認編輯器參照如下:

在git中設置默認使用的文本編輯器為notepad++

$ git config --global core.editor notepad++

設置成功會如下顯示。

$ git config --global core.editor 
notepad++

有的時候設置不成功,提交的時候不彈出notepad++,可以再使用如下命令試試

git config --global core.editor "'D:\Notepad++\notepad++.exe' -multiInst -notabbar -nosession -noPlugin '$*'"

將默認編輯器修改為VS Code

git config --global core.editor "code -w"

設置成功會如下顯示。

$ git config --global core.editor
code -w

設置了編輯器后,commit時編輯器打開了但是bash中提示提交取消

$ git commit
Aborting commit due to empty commit message.

查找到StackOverflow上說法

When you set an editor in the configuration of Git, make sure to pass the parameter "-w" to force Git to wait your commit message that you would type on your custom editor.

相應的做法是設置編輯器的時候加上-w參數

For Visual studio Code

git config --global core.editor "code -w"

For atom

git config --global core.editor "atom -w"

For sublime

git config --global core.editor "subl -w"

但是有時候即我們加了-w參數也不成功或者說會報如下錯誤:

$ git config --global core.editor "Code -w"
 warning: core.editor has multiple values
 error: cannot overwrite multiple values with a single value
        Use a regexp, --add or --replace-all to change core.editor.

這個時候需要重置git的編輯器設置然后重新設置編輯器

git config --global --unset-all core.editor
git config  --unset-all core.editor
git config --global core.editor "code -w"

這樣操作之后終於把問題解決了,設置成功后提交時會提示如下:

$ git commit
hint: Waiting for your editor to close the file...

Ref:Aborting commit due to empty commit message


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM