Calling external commands when switching branches

I want to restart my virtual server after switching branches and tried to add some aliases to mine .gitconfig

, but didn't get it to work.

co = checkout | !rap

      

only rap works, restart script.

co = checkout && !rap

      

and some other things I've tried (for example checkout $1

) my "git usage: bla bla" gives.

+2


source to share


2 answers


I think you really need a post-checkout

hook (look in your directory .git/hooks/

and githooks manpage ).



+3


source


Note that the correct solution to your problem might simply be using a post-checkout

hook , as written in the hacker's answer .

Below is a possible solution to your question .


You either use a single git command in an alias like eg.

[alias]
    alias = config --get-regexp ^alias\\.

      

or you use any command with '!' prefix, possibly using the " sh -c

" trick ; then you need to write "git command" for example.

[alias]
    sed = !git ls-files --stage | grep ^100 | awk '{print $4}' | xargs sed
    who = "!sh -c 'git log -1 --pretty=\"format:%an <%ae>\" --author=\"$1\"' -"

      



(not what alias.sed

is the best solution).


If you want to " git br <somebranch>

" execute git checkout <somebranch>

, then " rap

" try

[alias]
    br = !sh -c 'git checkout "$0" && rap'

      

Here &&

means: run the next command if the previous one is successful. Instead, you can use ;

to run the command regardless of the status of the earlier command

By the way, don't you switch branches with " git checkout <branch>

"? " git branch <branchname>

" creates a branch without checking it out.

+6


source







All Articles