Git shortcut to pull with clone if no local there yet?

There is not, given that the commands which operate on existing repos all assume that they're being run inside a given repo.

That said, if you're running in a shell, you could simply make use of the shell built-ins. For instance, here's bash:

if cd repo; then git pull; else git clone https://server/repo repo; fi

This checks to see if repo is a valid directory, and if so, does a pull within it; otherwise it does a clone to create the directory.


If upgrading git is not an option, and you don't want to pass an argument for a repo, a scripting approach could be:

#!/bin/bash

function clone_pull {
  DIRECTORY=$(basename "$1" .git)
  if [ -d "$DIRECTORY" ]; then
    cd "$DIRECTORY"
    git pull
    cd ../
  else
    git clone "$1"
  fi
}

clone_pull https://github.com/<namespace>/<repo>
# or
clone_pull [email protected]:<namespace>/<repo>.git

git -C repo pull will only work on git 1.8.5 or above. To achieve this on earlier versions, try:

remote_repo=https://server/repo
local_repo=repo

if [ -d $local_repo/.git ]; then pushd $local_repo; git pull; popd; else git clone $remote_repo; fi

The cleanest one-liner might be

git -C repo pull || git clone https://server/repo repo

Tags:

Git

Jenkins