Last active
July 31, 2018 15:32
-
-
Save thomasnorris/c4fe38da01c4fbf4255e476f56df4e79 to your computer and use it in GitHub Desktop.
Delete local branches that have been deleted from the remote via git prune
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
// --Uses execSync to call a remote prune origin, and then deletes any local branches that were pruned from the remote | |
(function() { | |
var exec = require('child_process').execSync; | |
var remoteBranchesBeforePrune = GetRemoteBranches(); | |
exec('git remote prune origin'); | |
var remoteBranchesAfterPrune = GetRemoteBranches(); | |
var prunedBranches = []; | |
remoteBranchesBeforePrune.forEach((branch) => { | |
if (remoteBranchesAfterPrune.indexOf(branch) === -1) | |
prunedBranches.push(branch); | |
}); | |
var localBranches = GetBranches(); | |
var toDelete = []; | |
localBranches.forEach((branch) => { | |
if (prunedBranches.indexOf(branch) !== -1) | |
toDelete.push(branch); | |
}); | |
if (toDelete.length !== 0) | |
toDelete.forEach((branch) => { | |
exec('git branch -D ' + branch); | |
console.log('Deleted local branch ' + branch); | |
}); | |
else | |
console.log('No local branches deleted.'); | |
function GetRemoteBranches() { | |
var allBranchesArr = GetBranches('-a'); | |
return allBranchesArr.filter((el) => { | |
return el.includes('remotes/origin/') | |
}).map((el) => { | |
return el.replace('remotes/origin/', ''); | |
}); | |
} | |
function GetBranches(param) { | |
if (!param) | |
param = ''; | |
return exec('git branch ' + param).toString().trim().replace(/\*/, '').split('\n').map((el) => { | |
return el.trim(); | |
}).filter((el) => { | |
return !el.includes('HEAD') | |
}); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment