Search code examples
gitrebase

Squash commits in interactive rebase with number sign (hash sign)


we use TFS with Git implementation as our source control. If commit messages include Backlog Item IDs starting with the number sign (like #1234) TFS is linking this commit directly with this item.

For the commit message writing, there is no problem, the commit message appears correctly in the log. Since I wrote a git-hook, the item number is set in every commit.

enter image description here

The problem appears when I use git's interactive rebase. I want to squash some commits, but in the editor, the number sign is interpreted as a Comment sign.

This is how it looks like:

# This is a combination of 4 commits.                                                                                                                            # This is the 1st commit message: 

#1234 Commit Message 1

# This is the commit message #2: 

#1234 some coding here

# This is the commit message #3: 

#1234 just a fix

# Please enter the commit message for your changes. Lines starting                                                                                               # with '#' will be ignored, and an empty message aborts the commit. 
.
.
.

I tried with a backslash so far, but it doesn't work. The Backslash appeared then in the message itself (\#1234 Commit Message 1)

Any suggestions, how I could squash my commits while keeping the number sign? Same problem in changing commit message text too.


Solution

  • You'll need to set core.commentChar to some character other than # so that you can use # as commit text, instead of as a comment.

    You can set this just for the duration of the one rebase:

    git -c core.commentchar=: rebase -i ...
    

    for instance. But if you are going to do this often, you might want to use git config to set it in your per-repository or global configuration.