Claudio Bredfeldt
Claudio Bredfeldt

Reputation: 1422

Git Alias: append string to commit message

To prevent our CI system from building on every commit, we have to append [skip ci] at the end of each commit (git commit -m "... [skip ci]").

Is it possible to make an Alias for that?

I have been playing with:

[alias]
  cms = !git commit -m "${1} \[skip ci\]" $1

Upvotes: 3

Views: 2332

Answers (2)

Erikw
Erikw

Reputation: 764

While not exactly what you asked for, I found a public gist that adds a git alias so that you can prepend the "[skip ci]" after creating a commit.

Hats off to the author of the gist; I did not come up with this solution, but I verified that it works!

First add the new alias:

$ git config --global alias.skip-ci '!git reset && COMMIT_MSG=`git show --format=format:%B -s` && git commit --allow-empty --amend -m "[skip ci] $COMMIT_MSG"'

Then it can be used liked

$ git commit -m "Updated README.md"
$ git skip-ci
$ git push

Upvotes: 0

Andrew C
Andrew C

Reputation: 14893

Commit Message Hooks

Instead of an alias I would consider prompting the user with y/n if they want to skip CI or not from a hook.

My bash mojo is not good enough to know if what you are trying to accomplish is possible via a Git Alias.

Really really rough sample code. Working, but doesn't cover all cases.

Put this in .git/hooks/prepare-commit-msg and make sure it is executable

#!/bin/sh
exec < /dev/tty
echo "Skip CI?"
select yn in "Yes" "No"
do
   case ${yn} in
      "Yes")
         echo "[skip ci]" >> $1
         break;;
      "No")
         break;;
   esac
done

This is what happens when you run commit. If you type "2" for no, the commit goes through normally, if you type "1" then it adds your skip ci message. A proper solution would need to be extended to handle things like Merge commits, commit templates, etc.

(master)$ git commit -m "also junk"
Skip CI?
1) Yes
2) No
#? 1
[master bfa5200] also junk [skip ci]]
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 bar
me@myvm:/scratch/commit_msg  (master)$ git log 
commit bfa5200096f7251e2dc8fc457af81c59519901f3
Author: Andrew C <[email protected]>

    also junk
    [skip ci]

Upvotes: 2

Related Questions