From d8aa3f63fba3119cb9b1bde9d2a8019eed9d0bb5 Mon Sep 17 00:00:00 2001 From: Oyasumi Gitea Date: Thu, 1 Jan 2026 14:05:21 +0800 Subject: [PATCH] git: add clear git script --- package.json | 2 +- scripts/clear-git-history.js | 47 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 scripts/clear-git-history.js diff --git a/package.json b/package.json index c15cb8d..8cdb231 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "format": "npx prettier . --write", "test": "tsx --test", "profile": "0x -D prof ./quartz/bootstrap-cli.mjs build --concurrency=1", - "clear-git": "git checkout --orphan temp-branch && git add . && git commit -m \"init: init commit, build\" && git branch -m main old && git branch -m temp-branch main && git checkout main" + "clear-git": "node scripts/clear-git-history.js" }, "engines": { "npm": ">=10.9.2", diff --git a/scripts/clear-git-history.js b/scripts/clear-git-history.js new file mode 100644 index 0000000..f73384d --- /dev/null +++ b/scripts/clear-git-history.js @@ -0,0 +1,47 @@ +import { execSync } from "child_process" + +function run(cmd) { + try { + console.log(`> ${cmd}`) + execSync(cmd, { stdio: "inherit" }) + } catch (e) { + console.error(`Error running: ${cmd}`) + process.exit(1) + } +} + +function main() { + // Step 1: Create orphan branch + run("git checkout --orphan temp-branch") + + // Step 2: Add all files + run("git add .") + + // Step 3: Commit + run('git commit -m "init: init commit"') + + // Step 4: Rename current main to old + run("git branch -m main old") + + // Step 5: Rename temp-branch to main + run("git branch -m temp-branch main") + + // Step 6: Checkout main + run("git checkout main") + + // Step 7: Set upstream for main branch to origin/main + run("git branch --set-upstream-to=origin/main main") + + // Step 8: Set upstream for old branch to origin/old if it exists locally + try { + run("git show-ref --verify --quiet refs/heads/old") + run("git branch --set-upstream-to=origin/old old") + console.log("Upstream for 'old' branch set to origin/old.") + } catch (e) { + console.log("Local 'old' branch does not exist, skipping upstream setup.") + } + + console.log("Git history cleared and remote branches updated.") +} + +main()