对于脚本来说,是否有一种简单的方法来检测远程是否已经存在,并且只有当它不存在时才添加它?管道命令还是瓷旗?
当使用现有的遥控器运行git remote add foo https://example.net时,我得到:
fatal: remote foo already exists.理想情况下,会有一个标志,如--if-not-exists或--update-if-exists,但我在文档中找不到任何标记。
我正在考虑git remote的输出,但是有更好的方法吗?
发布于 2019-09-14 13:56:34
git config remote.foo.url >&- || git remote add foogit是为处理关闭的fd而编写的,因此您不需要>/dev/null,只需关闭它,当您请求一个不存在的配置时,配置设置返回代码。
发布于 2019-09-14 12:48:15
我正在考虑对git遥控器的输出表示祝贺,但是有更好的方法吗?
您可以使用命令退出代码。
如果遥控器不存在:
$ git remote add foo https://example.net
$ echo $?
0如果遥控器已经存在,则:
$ git remote add foo https://example.net
fatal: remote foo already exists.
$ echo $?
128编辑:
这很可疑,但我认为它能做你想做的事:
$ grep -q '\[remote "foo"\]' .git/config || git remote add foo https://example.net发布于 2019-09-14 13:01:49
使用git remote show <remote>
$ git remote show origin && echo ok || echo err
* remote origin
Fetch URL: …
Push URL: …
HEAD branch: master
Remote branch:
master tracked
Local branch configured for 'git pull':
master rebases onto remote master
Local ref configured for 'git push':
master pushes to master (up to date)
ok
$ git remote show xxx && echo ok || echo err
fatal: 'xxx' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
errhttps://stackoverflow.com/questions/57935486
复制相似问题