The short version of my question:
Is there a way to conditionally delete a remote branch using git push origin :gh-pages
where the command is skipped or ignored if the gh-pages branch doesn't exist?
The long version of my question:
I'm building a boilerplate / starter template for using NPM as a Build System or Workflow for building basic webpages (instead of using something like Grunt or Gulp). The project uses a /build
folder and I wanted to use Github Pages to serve the contents of /build
. There doesn't seem to be an easy way to do that from /settings (I only see /root, not /build), so I ran across this command:
git subtree push --prefix build origin gh-pages
Which creates a gh-pages
branch and pushes my changes from the /build
folder of my master branch to the newly created gh-pages
branch. Excellent!
Unfortunately, I can't simply update my gh-pages
branch with changes (nor would I want to). I also can't make changes in master
and then merge them into gh-pages
. I get lots of error messages with branches being unrelated, or out of sync, or whatever.
Instead I need to make the changes in master
and then use the git subtree push --prefix build origin gh-pages
command again to push those changes. But I run into another error where it doesn't want to push new changes into an existing gh-pages
branch.
Okay ... So to fix this I added git push origin :gh-pages
to my command. So now my "deploy" command in my package.json looks like this:
"scripts": {
...
"deploy": "git push origin :gh-pages && git subtree push --prefix build origin gh-pages",
...
},
This works great. I simply type npm run deploy
in my terminal and git push origin :gh-pages
deletes the existing gh-pages
branch and git subtree push --prefix build origin gh-pages
creates a new gh-pages
branch with my changes. Excellent!
But ... the final problem I can't seem to figure out is this. If it's the first time running the deploy command, the gh-pages branch won't exist and my deploy script fails.
So is there a way to conditionally run the git push origin :gh-pages
part? Something like if origin gh-pages exists ... git push origin :gh-pages
?
By the way, deleting a branch this way (using a leading colon) was totally new to me. And the git-push man page didn't explain it well. To understand how git push origin :gh-pages
results in deleting the gh-pages
branch, take a look at this link.
In case it might be helpful, heres the repo. The package.json file is where the deploy script is located.
question from:
https://stackoverflow.com/questions/65947717/conditionally-delete-a-remote-branch-using-git-push-origin-branch-name-bu