2017-06-05 51 views
1

我有時在git上使用這個命令。如何在git上添加這個冗長的命令作爲別名?

$ git filter-branch --commit-filter ' 
    if [ "$GIT_AUTHOR_EMAIL" = "[email protected]" ]; 
    then 
      GIT_AUTHOR_NAME="myname"; 
      GIT_AUTHOR_EMAIL="[email protected]"; 
      git commit-tree "[email protected]"; 
    else 
      git commit-tree "[email protected]"; 
    fi' HEAD 

我想添加,作爲別名。 然後我嘗試如下,它不工作...

git config --global alias.rewritelog '!f(){ \ 
    git filter-branch --commit-filter \' \ 
    if [ "$GIT_AUTHOR_EMAIL" = "[email protected]" ]; \ 
    then \ 
     GIT_AUTHOR_NAME="myname"; \ 
     GIT_AUTHOR_EMAIL="[email protected]"; \ 
     git commit-tree "[email protected]"; \ 
    else \ 
     git commit-tree "[email protected]"; \ 
    fi\' HEAD \ 
};f' 

此命令後,發生了錯誤。

zsh: parse error near `then' 
+0

爲什麼不使用shell別名? –

+0

這是解決方案之一。 但我總是直接使用別名的命令。 我在問如何通過git別名來管理它。 – Harry

回答

2

你不能在單引號內跳過單引號。 幸運的是,您可以在雙引號內跳過雙引號。 用雙引號代替單引號:

git config --global alias.rewritelog "!f(){ \ 
    git filter-branch --commit-filter ' \ 
    if [ \"$GIT_AUTHOR_EMAIL\" = [email protected] ]; \ 
    then \ 
     GIT_AUTHOR_NAME=myname; \ 
     [email protected]; \ 
    fi; \ 
    git commit-tree \"[email protected]\"' HEAD; };f" 

我還簡化了原密碼,並刪除了一些不必要的雙引號。

+0

謝謝! 但是,出現不同的錯誤。 '''的zsh:找不到事件:!F(){''' – Harry

+0

@Harry嘗試逃跑'',所以在一開始寫像'\ F(){' – janos

+0

它工作的感謝! 謝謝! – Harry