Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

github - Conditionally delete a remote branch using `git push origin :branch-name` ... but only if branch exists?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could simply invoke the push-to-delete and continue anyway if it fails. This is crude—it would be better to continue if and only if it fails due to the branch not existing—but serviceable. To continue even if the command fails, use git push origin :gh-pages; git subtree push --prefix build origin gh-pages.

I would not recommend this method though (with or without something more clever to detect a "desirable failure"). There's another solution in that same comment thread you linked in your own comment that mentions this method:

git push origin `git subtree split --prefix build_folder master`:gh-pages --force

which is a bit shorter and simpler. Note that this uses build_folder rather than build; if we make it match, and use the + syntax to implement the forced push and the $(...) syntax that I find nicer than the backquote syntax, we get:

git push origin +$(git subtree split --prefix build master):gh-pages

If there is some chance that the git subtree split itself will fail, use:

hash=$(git subtree split --prefix build master) && git push origin +$hash:gh-pages

If you would like a local branch named gh-pages, use:

hash=$(git subtree split --prefix build master) && git branch -f gh_pages $hash && git push -f origin gh-pages

for instance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...