Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

jenkins

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!

like image 975
inger Avatar asked Mar 24 '13 18:03

inger


People also ask

What is git fetch upstream?

In review, git fetch is a primary command used to download contents from a remote repository. git fetch is used in conjunction with git remote , git branch , git checkout , and git reset to update a local repository to the state of a remote.

How do I fetch and pull in git?

Git Fetch is the command that tells the local repository that there are changes available in the remote repository without bringing the changes into the local repository. Git Pull on the other hand brings the copy of the remote directory changes into the local repository.


2 Answers

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.

like image 115
Amber Avatar answered Oct 15 '22 12:10

Amber


The cleanest one-liner might be

git -C repo pull || git clone https://server/repo repo 
like image 33
jhrmnn Avatar answered Oct 15 '22 12:10

jhrmnn