Search code examples
git

Specifying a default target directory for cloning git directories


I have a folder (~/repos) in my home directory in which I store cloned repositories from github. Is there a way to set an option so that git clone <repository> [directory] will clone into ~/repos if [directory] is empty, and otherwise will clone it into [directory]?


Solution

  • You could get most of the way there by creating an alias like this:

    git config --global alias.pclone '!git -C $HOME/repos clone'
    

    Now if you run:

    git pclone https://git.savannah.gnu.org/git/hello.git
    

    You will find that repository available locally in ~/repos/hello.

    (And of course you can call your alias anything you want; I just picked pclone for project-clone.)


    If you need more logic than you can conveniently fit into a git alias, you can instead create a shell script named git-pclone with content like:

    #!/bin/sh
    
    exec git -C $HOME/repos clone "$@"
    

    Make it executable and place it somewhere in your $PATH. This by itself is identical in behavior to the alias, but you can add whatever logic you feel is appropriate.