git commit -a - auto-indent?

这是 Git 提交信息编辑器(通常是 Vim)自动生成的注释格式,不是 git commit -a 本身的行为。

你看到的可能类似:

 

Implement Auto driving feature
 
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch feature_xxx
# Changes to be committed:
#       modified:   file1.c
#       modified:   file2.c
#

 

其中:

  • 第一行(commit title)是顶格
  • Git 自动生成的文件列表前面有缩进
  • 通常是 TAB 或多个空格
  • 不同 Git 版本、编辑器显示 TAB 宽度可能不同

如果你是说:

 

title
 
detail line 1
detail line 2

 

变成:

 

title
 
        detail line 1
        detail line 2

 

那通常是 Vim 开启了自动缩进。

可以在 Vim 中查看:

 

:set autoindent?
:set smartindent?
:set cindent?
`

 

如果返回:

 

autoindent

 

则新行会继承上一行缩进。

临时关闭:

 

:set noautoindent

 


如果你经常写 Git Commit Message,建议:

 

autocmd FileType gitcommit setlocal spell
autocmd FileType gitcommit setlocal textwidth=72
autocmd FileType gitcommit setlocal noautoindent

 

放到:

 

~/.vimrc

 

这样编辑 commit message 时不会自动缩进。


另一个可能是你在看:

 

git commit -v

 

生成的内容:

 

Implement feature
 
# ------------------------ >8 ------------------------
diff --git a/xxx b/xxx
...
 

 

这里下面的 diff 部分会保留原文件缩进,看起来可能像是:

 

        + new code

 

那是代码本身的缩进,不是 commit message 的缩进。

如果方便的话,把你实际看到的 commit 编辑界面贴出来(前十几行即可),我可以判断到底是:

  1. Git 自动生成的文件列表缩进
  2. Vim 自动缩进
  3. Commit template 导致
  4. git commit -v 的 diff 显示导致的缩进问题。

posted on 2026-09-27 09:14  ENGINEER-F  阅读(4)  评论(0)    收藏  举报