Skip to content

Instantly share code, notes, and snippets.

@putnamhill
Last active February 26, 2023 22:45
Show Gist options
  • Save putnamhill/981f6d3cc8c760c5519ae21cef58eaaa to your computer and use it in GitHub Desktop.
Save putnamhill/981f6d3cc8c760c5519ae21cef58eaaa to your computer and use it in GitHub Desktop.
#!/bin/sh
# create remote origin and push
# use 'trunk' unless init.defaultBranch is set
default_branch=$(git config init.defaultBranch || echo 'trunk')
remote_host='your-remote-host'
# assuming the remote host has a directory in $HOME named git
toplevel=$(git rev-parse --show-toplevel)
remote_repo_path="git/${toplevel##*/}.git"
# if the remote repo exists, don't try to create it
ssh "$remote_host" "test -d $remote_repo_path || exit 1" || \
ssh "$remote_host" "git init --bare $remote_repo_path"
# use newline as a word boundry character in the wildcard match
b=$'\n'
[[ ${b}$(git remote)${b} == *${b}origin${b}* ]] || \
git remote add origin "$remote_host:$remote_repo_path"
test -z "$(git branch)" || \
git push --set-upstream origin $default_branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment