Last active
February 4, 2022 21:34
-
-
Save apolopena/8dcbde7dd0c0cec1cb1e11c15a5e19ff to your computer and use it in GitHub Desktop.
Create a new repository from the branch of an existing one. Wipe out git history.
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
# shellcheck shell=bash | |
# shellcheck disable=2001 | |
# | |
# If using this as zsh function snippet, remove the .sh file extension | |
# Creates a new repository from the branch of an existing repository | |
# All git history is removed | |
# $1: git url of the original repository to use to create a new repository from | |
# $2: branch name of the original repository to use | |
# $3: name of the new repository including the github user name. This repository must already exist in github. | |
# For example: apolopena/gitpod-laravel-starter | |
# $4: optional commit message to use. | |
main() { | |
local url branch new_repo loc msg user | |
url="$1" | |
branch="$2" | |
new_repo="$(echo "$3" | awk -F/ '{print $2}')" | |
loc="$(echo "$1" | sed 's/\.[^.]*$//')" | |
if [[ -z "$4" ]]; then | |
msg="Initial commit built from the $branch branch of $loc" | |
else | |
msg="$4" | |
fi | |
user="$(echo "$3" | awk -F/ '{print $1}')" | |
mkdir "$new_repo" && | |
cd "$new_repo" && | |
git clone "$url" -b "$branch" --single-branch . && | |
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