设置Commit邮箱和用户名
git中每次commit时git都会自动在这次commit中添加提交人信息,用来mark这次commit是谁提交的,并记录该人的邮箱,否则你的同时看到commit历史记录他怎么知道这是谁提交的呢。
所以git要求必须要进行用户名和用户邮箱设置,否则不允许你提交。设置方式如下:
Global 设置
>> git config --global user.name
>> git config --global user.email
一旦这样设置后,在该计算机账户下所有仓库使用git commit的时候git都默认用这一组邮箱和用户名来mark你的commit信息
但是如果在同一个计算机账户下由于有多个人在工作或其他原因导致你对某个项目提交的时候需要用单独的邮箱和用户名提交怎么办呢?git中允许你正对单独的project进行设置,设置方法如下:
当前Project设置
>> git config user.name
>> git config user.email
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...