简体   繁体   English

使用 `git push origin:branch-name` 有条件地删除远程分支……但前提是分支存在?

[英]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?有没有办法使用git push origin:gh-pages有条件地删除远程分支,如果 gh-pages 分支不存在,该命令将被跳过或忽略?

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).我正在构建一个样板/入门模板,用于使用 NPM 作为构建基本网页的构建系统或工作流程(而不是使用诸如 Grunt 或 Gulp 之类的东西)。 The project uses a /build folder and I wanted to use Github Pages to serve the contents of /build .该项目使用/build文件夹,我想使用 Github Pages 来提供/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:从 /settings 似乎没有一个简单的方法来做到这一点(我只看到 /root,而不是 /build),所以我遇到了这个命令:

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.它创建了一个gh-pages分支,并将我的更改从我的 master 分支的/build文件夹推送到新创建的gh-pages分支。 Excellent!出色的!

Unfortunately, I can't simply update my gh-pages branch with changes (nor would I want to).不幸的是,我不能简单地用更改来更新我的gh-pages分支(我也不想这样做)。 I also can't make changes in master and then merge them into gh-pages .我也无法在master中进行更改,然后将它们合并到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.相反,我需要在master中进行更改,然后再次使用git subtree push --prefix build origin gh-pages命令来推送这些更改。 But I run into another error where it doesn't want to push new changes into an existing gh-pages branch.但是我遇到了另一个错误,它不想将新的更改推送到现有的gh-pages分支中。

Okay... So to fix this I added git push origin:gh-pages to my command.好的......所以为了解决这个问题,我将git push origin:gh-pages添加到我的命令中。 So now my "deploy" command in my package.json looks like this:所以现在我的 package.json 中的“部署”命令如下所示:

  "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.我只需在我的终端中输入npm run deploygit push origin:gh-pages删除现有的gh-pages分支和git subtree push --prefix build origin gh-pages更改创建一个新gh-pages分支。 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.如果是第一次运行 deploy 命令,则 gh-pages 分支将不存在,并且我的部署脚本将失败。

So is there a way to conditionally run the git push origin:gh-pages part?那么有没有办法有条件地运行git push origin:gh-pages部分? Something like if origin gh-pages exists... git push origin:gh-pages ?类似于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.并且git-push 手册页没有很好地解释它。 To understand how git push origin:gh-pages results in deleting the gh-pages branch, take a look at this link .要了解git push origin:gh-pages如何导致删除gh-pages分支,请查看此链接

In case it might be helpful, heres the repo .如果它可能有帮助,这里是repo The package.json file is where the deploy script is located. package.json 文件是部署脚本所在的位置。

You could simply invoke the push-to-delete and continue anyway if it fails.如果失败,您可以简单地调用 push-to-delete 并继续。 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即使命令失败也要继续,请使用git push origin:gh-pages; git subtree push --prefix build origin gh-pages git push origin:gh-pages; git subtree push --prefix build origin gh-pages . 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 ;请注意,这使用build_folder而不是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:如果git subtree split本身可能会失败,请使用:

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

If you would like a local branch named gh-pages , use:如果您想要一个名为gh-pages的本地分支,请使用:

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

for instance.例如。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Git结账差异git结账来源/ <branch-name> 和git结帐 <branch-name> ? - Git checkout difference git checkout origin/<branch-name> and git checkout <branch-name>? 我应该何时在git命令中使用“remote / branch-name”vs“remote branch-name”? - When should I use “remote/branch-name” vs “remote branch-name” in git commands? 仅当远程分支已经存在时才推送到 git 分支 - push to a git branch only if the remote branch already exists 为什么在git 1.5中git checkout“ origin / branch-name”导致“ no branch”? - Why does git checkout “origin/branch-name” lead to “no branch”, in git 1.5? 为什么“git push git push origin local-branch :development”会删除远程开发分支? - why "git push git push origin local-branch :development" will delete remote development branch? arc feature [branch-name]和git branch [branch-name]之间有什么区别? - What are the differences between arc feature [branch-name] and git branch [branch-name]? Git将原点-f remote_branch推送到隔离的分支 - Git push origin -f remote_branch to isolated branch 删除远程分支源 - delete remote branch origin `git pull` 不合并,但 `git pull origin<branch-name> `会,为什么?</branch-name> - `git pull` doesn't merge, but `git pull origin <branch-name>` does, why? 为什么我必须git pull origin <branch-name> 在git中? - Why do I have to git pull origin <branch-name> in git?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM