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



Is there a one-command way to get an up-to-date mirror of a remote repo?
That is

  • if local repo not there yet: clone
  • if it’s there: pull

I know I could script this around (e.g if [ -d repo ]; then (cd repo && git pull); else git clone $repourl;fi
) , but I need the simplest possible cross-platform way (actually used for Jenkins-CI, which I know does this by default, however I need 2 repos for which support is limited).

Git has similar shortcuts for other things (eg. checkout -b, and pull itself), so I’m wondering if I missed something. Thanks!


git pull knows from where to clone because the local repo has a remote registered in its local config. It operates from a working tree.

But git clone doesn’t, it must have an explicit remote url passed in parameter in order to clone. It operates outside the working tree.

The main reason for such a shortcut to not exist is that:

  • you git init rarely for a given repo: it is a one-time command in the life of the repo.
    Plus it might need additional command to be complete: if you havesubmodules, for instance, you would need to add a git submodule update --init.
  • you git pull often within a given repo. git pull is in itself a shortcut (for git fetch + git merge). Even git pull --rebase is another shortcut for git fetch + git rebase.

Considering the number of times you are to use git init, such a shortcut is not an high priority.

So a script remains the surest way to define what you need.

Answered By – VonC

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More