Skip to content

Instantly share code, notes, and snippets.

@eastenluis
Last active August 17, 2016 20:37
Show Gist options
  • Save eastenluis/077020291c441e56bf0df8f44b7054e6 to your computer and use it in GitHub Desktop.
Save eastenluis/077020291c441e56bf0df8f44b7054e6 to your computer and use it in GitHub Desktop.
Remove local branches that do not have remote tracking branches.
var exec = require('child_process').exec;
exec('git branch -l', function (error, stdout, stderr) { // retrieve local branches
var branchesWithoutTrack = [];
var localBranches = stdout.split('\n')
.map(function (branchName) {
return branchName.trim();
})
.filter(function (branchName) {
return branchName.length > 0;
});
exec('git branch -r', function (error, stdout, stderr) { // retrieve remote branches
var remoteBranches = stdout.split('\n')
.map(function (branchName) {
return branchName.trim().substring(7);
})
.filter(function (branchName) {
return branchName.length > 0;
});
localBranches.forEach(function (localBranch) {
if (remoteBranches.indexOf(localBranch) < 0) {
branchesWithoutTrack.push(localBranch);
}
});
if (branchesWithoutTrack.length > 0) {
exec('git branch -d ' + branchesWithoutTrack.join(' '), function (err, stdout, stderr) {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment