Search code examples
git-commitgit-configgit

How can I force "git commit -s" using "git commit" command?


I'm looking for a way to write the Signed-off-by: tag automatically when I commit.

I tried configuring it through the .git/config file (Reference). I put these lines of code:

[alias]
    commit = commit -s

This did not work. As commented below, you can not edit git's own alias (like commit).(Reference)

I also tried using the command (Reference): git config --global format.signoff true

Also had no effect. This explains why.

I'm looking for any solution that automatically places the tag and allows me to edit the commit message directly on git, without having to use a system alias.


Solution

  • Use the commits hooks to achieve this
    https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks#_committing_workflow_hooks

    prepare-commit-msg

    The prepare-commit-msg hook is run before the commit message editor is fired up but after the default message is created.

    It lets you edit the default message before the commit author sees it.

    This hook takes a few parameters: the path to the file that holds the commit message so far, the type of commit, and the commit SHA-1 if this is an amended commit.

    This hook generally isn’t useful for normal commits; rather, it’s good for commits where the default message is auto-generated, such as templated commit messages, merge commits, squashed commits, and amended commits.

    You may use it in conjunction with a commit template to programmatically insert information.