20

After performing an interactive rebase in git I want to have a commit message that starts with the # (hash, or pound) character, but lines starting with # are treated as comments and ignored.

Is there any way to escape the # character to make my commit message actually start with a #?

More Details

I am performing an interactive rebase using:

git rebase -i HEAD~4

Then, in the editor I am doing whatever is needed, e.g.:

pick b010299 #91691 Add test for logging in with valid credentials
reword 5e9159d 91691 Implement log-in feature
pick 2735aa3 #91691 Re-factor logic
pick 14bd500 #91691 Tidy up 'using' declarations

# Rebase 60d6e3f..14bd500 onto 60d6e3f
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

Then git loads the commit message in my text editor for the commit I want to reword, but I want to save my commit message with a # at the beginning:

#91691 Implement log-in feature

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# rebase in progress; onto 60d6e3f
# You are currently editing a commit while rebasing branch 'master' on '60d6e3f'.
#
# Changes to be committed:
#   modified:   My.Website.LogInController.cs

But that means my commit message will be ignored. How do I go about making the commit message #91691 Implement log-in feature?

Owain Williams
  • 2,387
  • 17
  • 22
  • It isn't pretty, but it is quicker than the accepted answer: Add a space infront of the '#'. As only lines _Starting_ with the character are ignored. – JonoB Mar 18 '21 at 16:08
  • It's only quicker if it works ;-) The reason I wanted the hash at the beginning was because my issue tracking integration was looking for commits that started with a hash. I guess it's _possible_ it would have found them if there was a leading space, but the documentation indicated it needed to start with a hash. – Owain Williams Apr 26 '21 at 14:48

1 Answers1

20

If you save your commit message with the #, it results in your commit message being blank (you could also delete everything in the commit message). Git will show you what to do:

Aborting commit due to empty commit message.
Could not amend commit after successfully picking 5e9159d9ce3a5c3c87a4fb7932fda4e53c7891db... 91691 Implement log-in feature
This is most likely due to an empty commit message, or the pre-commit hook
failed. If the pre-commit hook failed, you may need to resolve the issue before
you are able to reword the commit.
You can amend the commit now, with

        git commit --amend

Once you are satisfied with your changes, run

        git rebase --continue

So, just amend the message:

git commit --amend -m "#91691 Implement log-in feature"

and continue the rebase:

git rebase --continue
Thomas Gassmann
  • 737
  • 1
  • 13
  • 27
Owain Williams
  • 2,387
  • 17
  • 22
  • 2
    What if the commit is very big and multi-line, how the hashmark can be added at the beginning of the line ? – Mohammed Mar 28 '17 at 07:00
  • 2
    Just put your line breaks in before closing the double quote marks. Git is happy with multi-line, lengthy commit messages. – Owain Williams Mar 30 '17 at 00:36
  • 2
    @Mohammed In the interactive rebase plan leave commits with `pick`, but add `b` lines after the commits where you want to add hashes to. Then `git commit --amend --cleanup=scissors` and `git rebase --continue`. – 0xF Aug 29 '22 at 09:34