1

Is it possible to override git command or run another git commands before calling its alias. Like so: git checkout . -> git stash && git stash apply && git checkout .. After accidental checkout i think about it

IC_
  • 1,624
  • 1
  • 23
  • 57

2 Answers2

0

You cannot override a git command through a git alias.

You can do a bash script called git, which you can put in your $PATH first.
Or you can define a function called git in your .bashrc.

That script would look for checkout as a first argument and would apply your sequence of commands.

See an example here.

function git {
  if [[ "$1" == "checkout" && "$@" != *"--help"* ]]; then
    shift 1
    command git mycheckout "$@"
  else
    command git "$@"
  fi
}

In that case, you would implement your checkout in a script call git-mycheckout: Any script called git-xxx can be called by git as git xxx.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

I added a shortcut for checkout : alias.co = checkout, and am now so used to type it that I get surprised when I use a machine without the shortcut.

You could plug your command on such a shortcut.


A word on git stash : another way to create such a stash :

git stash store $(git stash create)

This has the advantage of not modifying the files on disk (and possibly triggering some actions watching for files modification ...)

(store and create are mentionned in git help stash)

LeGEC
  • 46,477
  • 5
  • 57
  • 104