Last active
May 17, 2024 17:10
-
-
Save lmmx/19cfcbcc117dc116afb7a7b7a171249e to your computer and use it in GitHub Desktop.
Eject a git repo: make a PR to GitHub, squash merge the PR (once checks pass), and delete both branch and local directory
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
function ejectit () { | |
# e: exit immediately on error; u: treat unset variables as an error | |
set -eu | |
# Create a new PR with auto-filled content (title from branch name, commits as body) | |
gh pr create --fill | |
# Merge the new PR (squashing commits) automatically [after checks pass], then delete the branch | |
gh pr merge --squash --auto --delete-branch | |
# Get an absolute path to the repo (top level directory) | |
local REPO=$(git rev-parse --show-toplevel) | |
# Double check that the .git/ directory is under it (be careful not to get the wrong target) | |
if [[ -d $REPO/.git ]]; then | |
# cd into the repo's parent directory and then delete it (as if 'ejecting' the repo) | |
cd $REPO/.. && rm -rf $REPO; | |
else | |
echo "Not deleting the local repo directory: $REPO/.git was not found" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment