git-nuke-cli 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +70 -0
- package/package.json +28 -0
- package/src/cli.js +160 -0
- package/src/utils.js +71 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Larsen Cundric
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# git-nuke-cli
|
|
2
|
+
|
|
3
|
+
> The "I am done with this branch" nuclear option. Deletes local + remote + tracking refs in one command.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install -g git-nuke-cli
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
# Nuke a specific branch
|
|
15
|
+
git nuke feature-old
|
|
16
|
+
|
|
17
|
+
# Nuke without confirmation
|
|
18
|
+
git nuke -f stale-branch
|
|
19
|
+
|
|
20
|
+
# Find and nuke ALL merged branches
|
|
21
|
+
git nuke --merged
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Example output
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
git nuke feature-old ──────────────────────────────
|
|
28
|
+
|
|
29
|
+
Plan:
|
|
30
|
+
✗ Delete local branch feature-old
|
|
31
|
+
✗ Delete remote branch origin/feature-old
|
|
32
|
+
● Prune tracking refs
|
|
33
|
+
|
|
34
|
+
? Proceed? (y/N) y
|
|
35
|
+
|
|
36
|
+
✓ Deleted local branch feature-old
|
|
37
|
+
✓ Deleted remote branch origin/feature-old
|
|
38
|
+
✓ Pruned tracking refs
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Merged branches cleanup
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
git nuke --merged ─────────────────────────────────
|
|
45
|
+
|
|
46
|
+
Found 3 merged branches:
|
|
47
|
+
|
|
48
|
+
✗ feature/login
|
|
49
|
+
✗ fix/typo
|
|
50
|
+
✗ chore/deps
|
|
51
|
+
|
|
52
|
+
? Nuke all 3 branches? (y/N) y
|
|
53
|
+
|
|
54
|
+
✓ Deleted local branch feature/login
|
|
55
|
+
✓ Deleted local branch fix/typo
|
|
56
|
+
✓ Deleted local branch chore/deps
|
|
57
|
+
✓ Done. 3 branches nuked.
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Features
|
|
61
|
+
|
|
62
|
+
- **Shows the plan first**: See exactly what will be deleted before confirming
|
|
63
|
+
- **Deletes everywhere**: Local branch, remote branch, and prunes tracking refs
|
|
64
|
+
- **Merged cleanup**: `--merged` finds all branches already merged into the default branch
|
|
65
|
+
- **Safety first**: Refuses to nuke the current branch, asks for confirmation by default
|
|
66
|
+
- **Force mode**: `-f` skips confirmation for scripting
|
|
67
|
+
|
|
68
|
+
## License
|
|
69
|
+
|
|
70
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "git-nuke-cli",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "The 'I am done with this branch' nuclear option. Delete local + remote + tracking refs.",
|
|
6
|
+
"bin": {
|
|
7
|
+
"git-nuke": "./src/cli.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"src/",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"keywords": ["git", "branch", "delete", "cleanup", "cli"],
|
|
14
|
+
"author": "Larsen Cundric",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/larsencundric/git-nuke-cli.git"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/larsencundric/git-nuke-cli#readme",
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=18"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"chalk": "5.6.2",
|
|
26
|
+
"commander": "14.0.3"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { createInterface } from 'node:readline';
|
|
4
|
+
import chalk from 'chalk';
|
|
5
|
+
import { program } from 'commander';
|
|
6
|
+
import { git, gitLines, symbols, header, fatal, isInsideGitRepo, getCurrentBranch, getDefaultBranch } from './utils.js';
|
|
7
|
+
|
|
8
|
+
program
|
|
9
|
+
.name('git-nuke')
|
|
10
|
+
.description('Delete a branch everywhere — local, remote, and tracking refs')
|
|
11
|
+
.argument('[branch]', 'branch to nuke')
|
|
12
|
+
.option('-m, --merged', 'find and nuke all fully merged branches')
|
|
13
|
+
.option('-f, --force', 'skip confirmation prompt')
|
|
14
|
+
.version('1.0.0')
|
|
15
|
+
.addHelpText('after', `
|
|
16
|
+
Examples:
|
|
17
|
+
git nuke feature-old Delete feature-old locally and remotely
|
|
18
|
+
git nuke --merged Nuke all merged branches
|
|
19
|
+
git nuke -f stale-branch Nuke without asking`)
|
|
20
|
+
.action(run);
|
|
21
|
+
|
|
22
|
+
program.parse();
|
|
23
|
+
|
|
24
|
+
async function confirm(question) {
|
|
25
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
26
|
+
return new Promise((resolve) => {
|
|
27
|
+
rl.question(question, (answer) => {
|
|
28
|
+
rl.close();
|
|
29
|
+
resolve(answer.toLowerCase().startsWith('y'));
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function getBranchInfo(branch) {
|
|
35
|
+
const current = getCurrentBranch();
|
|
36
|
+
const isLocal = gitLines(['branch', '--list', branch]).length > 0;
|
|
37
|
+
let remotes = [];
|
|
38
|
+
try {
|
|
39
|
+
const remoteBranches = gitLines(['branch', '-r', '--list', `*/${branch}`]);
|
|
40
|
+
remotes = remoteBranches.map((r) => r.trim()).filter((r) => r.endsWith('/' + branch));
|
|
41
|
+
} catch {}
|
|
42
|
+
return { isLocal, remotes, isCurrent: branch === current };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function getMergedBranches() {
|
|
46
|
+
const defaultBranch = getDefaultBranch();
|
|
47
|
+
const current = getCurrentBranch();
|
|
48
|
+
const protected_ = new Set([defaultBranch, current, 'HEAD'].filter(Boolean));
|
|
49
|
+
const merged = gitLines(['branch', '--merged', defaultBranch])
|
|
50
|
+
.map((b) => b.trim().replace(/^\* /, ''))
|
|
51
|
+
.filter((b) => !protected_.has(b) && b.length > 0);
|
|
52
|
+
return merged;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function nukeBranch(branch, { skipConfirm = false, forceDelete = false } = {}) {
|
|
56
|
+
const info = getBranchInfo(branch);
|
|
57
|
+
if (info.isCurrent) {
|
|
58
|
+
fatal(`Cannot nuke the current branch (${branch}). Switch to another branch first.`);
|
|
59
|
+
}
|
|
60
|
+
if (!info.isLocal && info.remotes.length === 0) {
|
|
61
|
+
fatal(`Branch '${branch}' not found locally or on any remote`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
console.log(chalk.bold(' Plan:'));
|
|
65
|
+
if (info.isLocal) {
|
|
66
|
+
console.log(` ${chalk.red(symbols.cross)} Delete local branch ${chalk.white(branch)}`);
|
|
67
|
+
}
|
|
68
|
+
for (const remote of info.remotes) {
|
|
69
|
+
console.log(` ${chalk.red(symbols.cross)} Delete remote branch ${chalk.white(remote)}`);
|
|
70
|
+
}
|
|
71
|
+
console.log(` ${chalk.yellow(symbols.bullet)} Prune tracking refs`);
|
|
72
|
+
console.log();
|
|
73
|
+
|
|
74
|
+
if (!skipConfirm) {
|
|
75
|
+
const ok = await confirm(` ${chalk.yellow('?')} Proceed? (y/N) `);
|
|
76
|
+
if (!ok) {
|
|
77
|
+
console.log(chalk.dim(' Aborted.'));
|
|
78
|
+
console.log();
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (info.isLocal) {
|
|
84
|
+
try {
|
|
85
|
+
git(['branch', forceDelete ? '-D' : '-d', branch]);
|
|
86
|
+
console.log(` ${chalk.green(symbols.check)} Deleted local branch ${chalk.white(branch)}`);
|
|
87
|
+
} catch (e) {
|
|
88
|
+
console.log(` ${chalk.red(symbols.cross)} Failed to delete local branch: ${e.message}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
for (const remote of info.remotes) {
|
|
93
|
+
const [remoteName, ...branchParts] = remote.split('/');
|
|
94
|
+
const remoteBranch = branchParts.join('/');
|
|
95
|
+
try {
|
|
96
|
+
git(['push', remoteName, '--delete', remoteBranch]);
|
|
97
|
+
console.log(` ${chalk.green(symbols.check)} Deleted remote branch ${chalk.white(remote)}`);
|
|
98
|
+
} catch (e) {
|
|
99
|
+
console.log(` ${chalk.red(symbols.cross)} Failed to delete ${remote}: ${e.stderr || e.message}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const prunedRemotes = new Set();
|
|
104
|
+
for (const remote of info.remotes) {
|
|
105
|
+
const remoteName = remote.split('/')[0];
|
|
106
|
+
if (!prunedRemotes.has(remoteName)) {
|
|
107
|
+
git(['remote', 'prune', remoteName], { allowFail: true });
|
|
108
|
+
prunedRemotes.add(remoteName);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (prunedRemotes.size === 0) {
|
|
112
|
+
git(['remote', 'prune', 'origin'], { allowFail: true });
|
|
113
|
+
}
|
|
114
|
+
console.log(` ${chalk.green(symbols.check)} Pruned tracking refs`);
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async function run(branch, opts) {
|
|
119
|
+
if (!isInsideGitRepo()) fatal('Not inside a git repository');
|
|
120
|
+
|
|
121
|
+
if (opts.merged) {
|
|
122
|
+
header('git nuke --merged');
|
|
123
|
+
const merged = getMergedBranches();
|
|
124
|
+
if (merged.length === 0) {
|
|
125
|
+
console.log(chalk.dim(' No merged branches to clean up.'));
|
|
126
|
+
console.log();
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
console.log(chalk.bold(` Found ${merged.length} merged branch${merged.length === 1 ? '' : 'es'}:\n`));
|
|
131
|
+
for (const b of merged) {
|
|
132
|
+
console.log(` ${chalk.red(symbols.cross)} ${b}`);
|
|
133
|
+
}
|
|
134
|
+
console.log();
|
|
135
|
+
|
|
136
|
+
if (!opts.force) {
|
|
137
|
+
const ok = await confirm(` ${chalk.yellow('?')} Nuke all ${merged.length} branches? (y/N) `);
|
|
138
|
+
if (!ok) {
|
|
139
|
+
console.log(chalk.dim(' Aborted.'));
|
|
140
|
+
console.log();
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
console.log();
|
|
146
|
+
for (const b of merged) {
|
|
147
|
+
await nukeBranch(b, { skipConfirm: true });
|
|
148
|
+
}
|
|
149
|
+
console.log();
|
|
150
|
+
console.log(` ${chalk.green(symbols.check)} Done. ${merged.length} branch${merged.length === 1 ? '' : 'es'} nuked.`);
|
|
151
|
+
console.log();
|
|
152
|
+
} else {
|
|
153
|
+
if (!branch) {
|
|
154
|
+
fatal('Specify a branch name or use --merged');
|
|
155
|
+
}
|
|
156
|
+
header(`git nuke ${chalk.white(branch)}`);
|
|
157
|
+
await nukeBranch(branch, { skipConfirm: opts.force, forceDelete: opts.force });
|
|
158
|
+
console.log();
|
|
159
|
+
}
|
|
160
|
+
}
|
package/src/utils.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
|
|
4
|
+
export function git(args, opts = {}) {
|
|
5
|
+
try {
|
|
6
|
+
return execFileSync('git', args, {
|
|
7
|
+
encoding: 'utf-8',
|
|
8
|
+
maxBuffer: 50 * 1024 * 1024,
|
|
9
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
10
|
+
...opts,
|
|
11
|
+
}).trim();
|
|
12
|
+
} catch (e) {
|
|
13
|
+
if (opts.allowFail) return '';
|
|
14
|
+
throw e;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function gitLines(args, opts = {}) {
|
|
19
|
+
const out = git(args, opts);
|
|
20
|
+
return out ? out.split('\n') : [];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function getCurrentBranch() {
|
|
24
|
+
const branch = git(['rev-parse', '--abbrev-ref', 'HEAD']);
|
|
25
|
+
// In detached HEAD state, git returns the literal string "HEAD"
|
|
26
|
+
return branch === 'HEAD' ? null : branch;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function getDefaultBranch() {
|
|
30
|
+
try {
|
|
31
|
+
const ref = git(['symbolic-ref', 'refs/remotes/origin/HEAD']);
|
|
32
|
+
return ref.replace('refs/remotes/origin/', '');
|
|
33
|
+
} catch {
|
|
34
|
+
for (const name of ['main', 'master']) {
|
|
35
|
+
try {
|
|
36
|
+
git(['rev-parse', '--verify', name]);
|
|
37
|
+
return name;
|
|
38
|
+
} catch {}
|
|
39
|
+
}
|
|
40
|
+
return 'main';
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function isInsideGitRepo() {
|
|
45
|
+
try {
|
|
46
|
+
git(['rev-parse', '--is-inside-work-tree']);
|
|
47
|
+
return true;
|
|
48
|
+
} catch {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const symbols = {
|
|
54
|
+
bullet: '●',
|
|
55
|
+
check: '✓',
|
|
56
|
+
cross: '✗',
|
|
57
|
+
warning: '⚠',
|
|
58
|
+
line: '─',
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export function header(text) {
|
|
62
|
+
// Strip ANSI escape codes so chalk-colored text doesn't skew the padding
|
|
63
|
+
const plainLength = text.replace(/\u001b\[[0-9;]*[a-zA-Z]/g, '').length;
|
|
64
|
+
const line = symbols.line.repeat(Math.max(0, 50 - plainLength));
|
|
65
|
+
console.log(`\n${chalk.bold.cyan(text)} ${chalk.dim(line)}\n`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function fatal(msg) {
|
|
69
|
+
console.error(`${chalk.red(symbols.cross)} ${msg}`);
|
|
70
|
+
process.exit(1);
|
|
71
|
+
}
|