tags:

views:

204

answers:

2

I want to restart my virtual server after switching branches, and tried adding some aliases in my .gitconfig but did not get it to work.

co = checkout | !rap

only runs rap, the restart script.

co = checkout && !rap

and some other things I tried (like checkout $1) gives my "git usage: bla bla".

+3  A: 

I think, what you really want is post-checkout hook (look into your .git/hooks/ directory and githooks manpage).

Michael Krelin - hacker
Thanks but.. I tried #!/bin/sh echo "foo"in .git/hooks/post-checkout and `chmod +x post-checkout`, but git-checkout doesn't echo anything!
Jacob R
I have just checked and it does echo. What happens if you run `./.git/hooks/post-checkout` directly?
Michael Krelin - hacker
It echoes fine when running it directly. I even tried `git-init` but it didn't matter.
Jacob R
The only thing I can think of is that you've misnamed the file...
Michael Krelin - hacker
Or.. what is your `git` version?
Michael Krelin - hacker
I have version 1.5.3.7
Jacob R
I've checked the git repository, looks like the first release, that incorporates `post-checkout` hook is `1.5.4-rc0`. Can you upgrade your git or is it not an option?
Michael Krelin - hacker
Ok, thanks for the help. I checked the docs on our machine and they included post-checkout so I thought it was there. I will see if I can get the admins to upgrade.
Jacob R
This is weird. The post-checkout docs have been included in the same commit as functionality. Maybe you have docs from different version...
Michael Krelin - hacker
+1  A: 

Note that the correct solution to your problem could be simply using post-checkout hook, as written in hacker's answer.

Below there is posible solution for your question.


You either use single git command in alias, like e.g.

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

or you use any command with '!' prefix, perhaps using "sh -c" trick; then you have to spell "git command", e.g.

[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 that alias.sed is the best solution).


If you want for "git br <somebranch>" to do "git checkout <somebranch>", then "rap", try

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

Here && means: do next command if previous one is succesfull. You can use ; instead to run command regardless of status of earlier command

BTW don't you switch branches using "git checkout <branch>"? The "git branch <branchname>" creates branch, without checking it out.

Jakub Narębski
It wasn't really a solution to my question, but very generic. I tried `co = "!sh -c 'git checkout \"$1\"' | rap -"` and `co = !git checkout $1 | rap -` and it still only runs rap.
Jacob R
Errr... "`|`" means to pipe output of command (connest standard output of earlier with standard input of later command). You need to use "``". See also updated answer.
Jakub Narębski