21
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

Organization

gitのaliasコマンドに引数を渡す方法

gitのaliasコマンドに引数を渡す

引数の渡し方をいろいろ調べたのでメモ。
関数を使うやり方の方が分かりやすくて良さそう。

[alias]
        command1 = "!sh -c 'echo $1 $2' -"
        command2 = "!f(){ echo $1 $2;};f"
$ git command2 hoge fuga
hoge fuga

sh -c を使う

command1 = "!sh -c 'echo $1 $2' -"

最後のハイフンは$0を消すためのダミー。
ハイフン無しで'echo $0 $1'とすると、引数ありの時は一つ目が\$0に。無しの時は\$0にshが入る。

関数を使う

command2 = "!f(){ echo $1 $2;};f"

こちらは見ての通り。{の後のスペースを詰めると動かないので注意。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
21
Help us understand the problem. What are the problem?