Add flag to force branch creation
See original GitHub issueThe use case I’m trying to address is to either create a branch and switch to it or just switch to it if it already exists. It’s the functional equivalent of:
await git.branch({ ...repo, ref: 'branchname', checkout: true })
.catch((e) => {
if (e.code === git.E.RefExistsError && e.data.noun === 'branch') {
return git.checkout({ ...repo, ref: 'branchname' })
}
throw e
})
However, that’s not idiomatic. It also requires some work to avoid calling checkout with side effects (the creation of a remote tracking branch).
What I’d like to be able to do is force create the branch:
await git.branch({ ...repo, ref: 'branchname', force: true })
which is the same as git branch -f branchname
.
which I can then combine with the checkout option to checkout the branch at the same time:
await git.branch({ ...repo, ref: 'branchname', checkout: true, force: true })
which is the same as git checkout -B branchname
.
The force
flag would thus allow isomorphic-git to cover both use cases.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:10 (10 by maintainers)
Top Results From Across the Web
github - Git branch editing flag
Is there an oportunity to put some sort of flag like "USERNAME is editing this branch" to git's branch? Such feature can help...
Read more >Git - git-branch Documentation
Shortcut for --delete --force . --create-reflog. Create the branch's reflog. This activates recording of all changes made to the branch ref, enabling use...
Read more >Force Push in Git - Everything You Need to Know
You can use the --force flag (or -f for short). This can look like an easy workaround when the git push command does...
Read more >Git Branch | Atlassian Git Tutorial
The git branch command lets you create, list, rename, and delete branches. It doesn't let you switch between branches or put a forked...
Read more >Introduction to Git rebase and force-push - GitLab Docs
If you added changes to my-feature-branch after creating the backup branch, ... To force an update, pass the flag --force or -f to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hmm. I don’t like that solution, because I’ve had bad experiences creating optional arguments the default to
true
(like thisreset
argument). Edit: although that’s easy to fix, by naming the argumentnoReset
*shrug*It also seems complicated. Every time you add a boolean option, you double the complexity of a function. If you think about it, a single function with 3 boolean options is equivalent to 8 functions. (Or one switch statement with 2^3 cases.) Out of all 8 combinations of
checkout
,force
, andreset
- how many behaviors do we actually care about?Side note: I made this explicit one time in calculateBasicAuthUsernamePasswordPair and somebody tweeted it as exceptionally clever. Edit: to this day, I’m not sure whether that function is code golf or extremely readable.
Let’s sit on this for a bit and see if we can come up with a simpler solution.
Oops, you’re absolutely right!