Created
April 1, 2012 19:34
-
-
Save nicferrier/2277987 to your computer and use it in GitHub Desktop.
Clone a git repo if it does not exist, or pull into it if it does exist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
REPOSRC=$1 | |
LOCALREPO=$2 | |
# We do it this way so that we can abstract if from just git later on | |
LOCALREPO_VC_DIR=$LOCALREPO/.git | |
if [ ! -d $LOCALREPO_VC_DIR ] | |
then | |
git clone $REPOSRC $LOCALREPO | |
else | |
cd $LOCALREPO | |
git pull $REPOSRC | |
fi | |
# End |
I made this script, a "little bit" more complex but which fulfilled my specific needs: https://gist.github.com/jotaelesalinas/cc88af3c9c4f8664216ea07bd08c250f
git clone "$REPOSRC" "$LOCALREPO" 2> /dev/null || (cd "$LOCALREPO" ; git pull)
Thank you. 👍
No need to cd
into the directory to pull from it. git -C "$LOCALREPO" pull
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
/dev/null would hide error messages in first clone, so the full
if
conditional is safer.Can always use
;
to compress shell code into a one-liner... 😉Could also do the pull unconditionally, mildly less efficient but condenses to:
The parens around
(cd ...; git pull ...)
are not needed when it's a standalone script like here but good in a larger context —cd
done in a sub-shell won't affect the rest of the script.P.S. A futher tweak: testing existance
-e $LOCALREPO_VC_DIR
rather than requiring it to be a directory-d
is slightly more generic for cases when the clone was created by other means — it's possible for.git
to be a text file with contentgitdir: ...
, as set up bygit submodule
,git worktree
and maybe others...