I am trying to make an alias for mv so it does its normal behaviour in normal folders and is replaced by git mv inside git repositories. I tried many ways. The if statement works, only the command git mv will not run correctly.

alias mv='"$(
if [ x`git rev-parse --show-toplevel 2> /dev/null` = x ]; 
    echo mv;  
    echo "git mv"; 
  • 7
    Use a function, not an alias. Mar 13, 2017 at 18:41
  • ... Or even a little custom script
    – roaima
    Mar 13, 2017 at 18:51
  • Regarding the contents of your alias, there's no reason to use git mv instead of mv because git will detect moves based on content. Rather move and commit, without changing the file, and AFTER commit modify the file. That way git will know that the file is moved. Oct 26, 2021 at 10:00
  • @Highmastdon true, but the alias has the advantage of making the task faster, easier and cleaner. faster: not having to look am I inside or outside the repo, use only 1 command in git. easier: only the simple mv command everywhere, always. cleaner: no unwanted commit messages floating around in the git history that have to get cleaned out before committing to the company git that has strict rules on commit messages and kind of work that is allowed to have a separate commit. your solution would add commands even if you dont clean up the commits afterwards.
    – switch87
    Oct 27, 2021 at 12:42

2 Answers 2


I would use a function for that, like so:

    # If in a git repo - call git mv. otherwise- call mv
    if [ x`git rev-parse --show-toplevel 2> /dev/null` = x ]; 
        mv "$@"  
        git mv "$@" 


alias mv=gitmv

  • "There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used." gnu.org/software/bash/manual/html_node/Aliases.html#Aliases
    – jhscheer
    Mar 13, 2017 at 18:56
  • indeed, like Sato Katsura also recomanded me I already did it that way. I will edit your answer with my solution (almost the same but with alias)
    – switch87
    Mar 13, 2017 at 19:02
  • 1
    @switch87: I'm glad you found a suitable solution for you. It seems your edit got rejected due to a conflict with another (random) edit. I'll add your alias.
    – jhscheer
    Mar 13, 2017 at 19:28

Although jhscheer proposed a much better solution (IMHO), it is still possible to achieve what you were trying to do with only an alias.

Your alias did not work because of the external double quotes, which make git mv a single token, and you have no git mv executable. If you remove the external double quotes, your alias should work fine.

By the way, the inner double quotes, in echo "git mv" are useless, but harmless.

I have no git repository immediately available to test, but I just tried with the following alias:

alias foo='$(if [ x"`pwd`" = x"$HOME" ] ; then echo echo; else echo echo foo; fi)'
  • Thank you for explaining why my first try did not work.
    – switch87
    Jan 1, 2019 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.