2017-06-05 55 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