You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Git commit.template中出现^M特殊字符的去除方法咨询

Fixing ^M Characters in Your Git Commit Template

Hey, I’ve dealt with this annoying ^M issue plenty of times—those are just Windows-style carriage return (CR) characters clashing with Unix/macOS line endings (LF). Here’s how to get rid of them for good:

  • First, confirm the problem
    Run this command to see the raw line endings in your commit template (replace the path with your actual template file location):
    cat -A ~/.gitcommit.template
    You’ll see those ^M characters show up as \r at the end of lines, which confirms it’s a line ending mismatch.

  • Convert the template file to Unix-style line endings
    Pick one of these quick methods:

    1. Use dos2unix (simplest if available):
      dos2unix ~/.gitcommit.template
      This tool directly converts CRLF to LF in place.
    2. Use sed if you don’t have dos2unix:
      sed -i 's/\r$//' ~/.gitcommit.template
      This command strips out the trailing \r from every line.
    3. Use Vim:
      Open the file with vim ~/.gitcommit.template, then in command mode type :set fileformat=unix and save with :wq. This rewrites the file with Unix line endings.
  • Prevent this from happening again
    Set Git to handle line endings automatically so future edits don’t reintroduce the issue:
    git config --global core.autocrlf input
    This tells Git to convert any CRLF line endings to LF when you commit files, while leaving your working directory files as-is (ideal for Unix/macOS). If you’re on Windows, use git config --global core.autocrlf true instead.

The root cause here is that your commit template file was probably created or edited on a Windows system, which uses CRLF (\r\n) line endings, while your current system expects just LF (\n). Converting the file and setting up Git’s line ending handling will make those ^M characters disappear for good.

内容的提问来源于stack exchange,提问作者Marfaldo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 12:37:27