Welcome to the Comprehensive Guide to git-branch-is!
git-branch-is is an excellent npm package that checks the current git branch and whether it matches any specified condition. Designed for both newbies and experienced developers, this tool allows you to seamlessly manage your git branches through straightforward API calls.
API Overview and Examples
Here’s a quick glance at some of the most useful APIs provided by the git-branch-is package:
1. Checking Current Branch Name
const gitBranchIs = require('git-branch-is');
gitBranchIs('main').then(result => {
if (result) {
console.log('You are on the main branch!');
} else {
console.log('You are NOT on the main branch!');
}
});
2. Multiple Branch Check
gitBranchIs(['main', 'develop']).then(result => {
if (result) {
console.log('You are on either the main or develop branch!');
} else {
console.log('You are NOT on the main or develop branch!');
}
});
3. Custom Matcher Function
gitBranchIs(branchName => branchName.startsWith('release/')).then(result => {
if (result) {
console.log('You are on a release branch!');
} else {
console.log('You are NOT on a release branch!');
}
});
4. Synchronous API
const result = gitBranchIs.sync('main'); if (result) {
console.log('You are on the main branch!');
} else {
console.log('You are NOT on the main branch!');
}
App Example
Let’s create a small Node.js application to demonstrate using git-branch-is. This app will perform several branch checks before executing specific tasks:
const gitBranchIs = require('git-branch-is');
async function runApp() {
const onMainBranch = await gitBranchIs('main');
const onDevelopBranch = await gitBranchIs('develop');
const onFeatureBranch = await gitBranchIs(branchName => branchName.startsWith('feature/'));
if (onMainBranch) {
console.log('Running on main branch...');
// Main branch specific logic here
} else if (onDevelopBranch) {
console.log('Running on develop branch...');
// Develop branch specific logic here
} else if (onFeatureBranch) {
console.log('Running on feature branch...');
// Feature branch specific logic here
} else {
console.log('Branch not recognized. Please switch to main, develop, or a feature branch.');
}
}
runApp();
With these powerful APIs and flexible options, git-branch-is ensures that you have complete control over branch-specific behaviors in your projects.
Hash: 681c844d8fae5b39cce815772202a1230babc31c1e2a506085da325aef4d78cd