web-dev-qa-db-ja.com

git aliasShellコマンドのエラー

Git 1.7.1を使用してcygwinでbashバージョン4.1.2(1)-リリース(x86_64-redhat-linux-gnu)を使用しています。 input引数を2回使用する必要があるコマンドのエイリアスを作成したかったのです。次の これらの指示 、私は書いた

[alias]
branch-excise = !sh -c 'git branch -D $1; git Push Origin --delete $1' --

そして私はこのエラーを受け取ります:

$> git branch-excise my-branch
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file

私は両方を試しました---最後に、しかし私は同じエラーを受け取ります。どうすればこれを修正できますか?

8
user394

man git-configのコメント:

構文はかなり柔軟で寛容です。空白はほとんど無視されます。 #と;文字は行末までコメントを開始し、空白行は無視されます。

そう:

branch-excise = !bash -c 'git branch -D $1; git Push Origin --delete $1'

と同等です:

#!/usr/bin/env bash

bash -c 'git branch -D $1

上記のスクリプトを実行すると、次のように出力されます。

/tmp/quote.sh: line 3: unexpected EOF while looking for matching `''
/tmp/quote.sh: line 4: syntax error: unexpected end of file

1つの解決策は、コマンド全体を"に配置することです。

branch-excise = !"bash -c 'git branch -D $1; git Push Origin --delete $1'"

ただし、$1が空であるため、それでも機能しません。

$ git branch-excise master
fatal: branch name required
fatal: --delete doesn't make sense without any refs

これを機能させるには、.gitconfigにダミー関数を作成し、次のように呼び出す必要があります。

branch-excise = ! "ddd () { git branch -D $1; git Push Origin --delete $1; }; ddd"

使用法:

$ git branch-excise  master
error: Cannot delete the branch 'master' which you are currently on.
(...)
11