Skip to content

Instantly share code, notes, and snippets.

@apolopena
Last active February 4, 2022 21:26
Show Gist options
  • Save apolopena/2d7995e5e8bfcfa9287d74d16b14aafe to your computer and use it in GitHub Desktop.
Save apolopena/2d7995e5e8bfcfa9287d74d16b14aafe to your computer and use it in GitHub Desktop.
Create a new repository from an existing repository. Wipes out git history.
# shellcheck shell=bash
# shellcheck disable=2001
#
# If using this as zsh function snippet, remove the .sh file extension
#
# Creates a new repository from an existing repository
# All git history is removed
# $1: git url of the original repository to use to create a new repository from
# $2: name of the new repository including the github user name. This repository must already exist in github.
# For example: apolopena/gitpod-laravel-starter
# $3: optional commit message to use.
main() {
local url new_repo msg user
url="$1"
new_repo="$(echo "$2" | awk -F/ '{print $2}')"
if [[ -z "$3" ]]; then
msg="Initial commit built from $url"
else
msg="$3"
fi
user="$(echo "$2" | awk -F/ '{print $1}')"
mkdir "$new_repo" &&
cd "$new_repo" &&
git clone "$url" . &&
rm -rf .git &&
git init &&
git add -A &&
git commit -m "$msg" &&
git remote add origin "https://github.com/$user/$new_repo.git" &&
git branch -m main &&
git push -u origin main
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment