5

I am trying to train myself to write better commit messages, so I have created an entry in my ~/.gitconfig

[commit]
    template = ~/.gitmessage.txt

The conundrum is that I nearly always use:

git commit -m "Message here"

and sometimes:

git commit -am "Message here"

How can I prevent myself from using the -m flag, so that my template will be presented and I will be reminded to use it?

I don't need to absolutely enforce this in the project, but I would like to wean myself off of "-m".

Ubuntu/Bash is my environment.

Jason
  • 15,017
  • 23
  • 85
  • 116
Daniel Bower
  • 739
  • 1
  • 7
  • 27
  • 2
    Get an alias for `git commit` like `gcommit`? Honestly, I don't see how you cannot get yourself to type *less* initially ;) – pmr Mar 03 '14 at 21:08
  • What about something like http://stackoverflow.com/questions/9226528/how-can-i-avoid-an-accidental-dcommit-from-a-local-branch suggests to prevent an accidental dcommit? – Don Branson Mar 03 '14 at 21:48
  • @pmr - It's about overcoming "muscle memory". I thought of doing a short alias right away, as I'm doing something similar here: https://bowerstudios.com/node/1051. However with the "-m" case there is not quite the fun payoff of output to get me to use the commit alias, so instead of a carrot, I need to think of a stick to use :-) – Daniel Bower Mar 04 '14 at 01:46

3 Answers3

13

Put this in your .bashrc:

git() {
  for arg
  do
    if [[ $arg == -m* || $arg == -[^-]*m* ]]
    then
      annoy_me
      return 1
    fi
  done
  command git "$@"
}
annoy_me() { 
  echo "Stop using -m, $USER!" 
  echo "You are now in time out."
  settings=$(stty -g)        
  stty raw
  sleep 15
  stty "$settings"
}

annoy_me here waits 15 seconds and is not killable from that terminal.

You can replace it by whatever you consider suitably annoying, such as sl or mplayer -volume 100 Spice_Girls_Wannabe.mp3 < /dev/null &> /dev/null &

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • 3
    This is pretty cute, although looking for `-*m*` is going to be overbearing - I can't write `git merge --no-commit` anymore? – amalloy Mar 04 '14 at 01:18
  • For the curious I ended up deriving the following from @that-other-guy's answer: https://bowerstudios.com/node/1177 – Daniel Bower Mar 04 '14 at 02:25
  • 1
    I very much recommend this to disable `-a` switch. It's usually the most damaging thing I do to my repository, and prevents me from separating changes into proper commits. – Piotr Zierhoffer Oct 03 '14 at 09:25
1

You can write a shell script called git as well and put it into your PATH before the real git. Inside it, you just check the args for -m, if present scoff at yourself, if not call the real git binary.

See this question for an example how to forward the args.

Community
  • 1
  • 1
C. Ramseyer
  • 2,322
  • 2
  • 18
  • 22
0

I do not foresee an answer which could make you prevent you from your habit.

But you may try to use aliases. The only trick here would be getting rid of your old habit, replacing it with the alias which points to your template.

Tip: It takes an average of 30 days to form a habit

Igbanam
  • 5,904
  • 5
  • 44
  • 68