bash 在第一次推送之前检查 git remote 是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12170459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if git remote exists before first push
提问by apc
I'm writing a bash script and I need a test to see whether a given remote exists.
我正在编写一个 bash 脚本,我需要一个测试来查看给定的遥控器是否存在。
Suppose, for concreteness, that I want to test whether the remote farawayexists. If I've pushed something to faraway, I can do if [ -d .git/refs/remotes/faraway ]; then .... But as far as I can see, the alias farawaycan still be defined even if .git/refs/remotes/farawaydoes not exist.
假设,为了具体起见,我想测试遥控器是否faraway存在。如果我已经推送了一些东西faraway,我可以做if [ -d .git/refs/remotes/faraway ]; then ...。但据我所知,即使别名不存在,faraway仍然可以定义.git/refs/remotes/faraway。
One other option is to parse through the output of git remoteand see if farawayappears there. But I'm wondering whether there is an easier way of checking whether farawayis defined, regardless of whether .git/refs/remotes/faraway/exists.
另一种选择是解析输出git remote并查看是否faraway出现在那里。但我想知道是否有更简单的方法来检查是否faraway已定义,无论是否.git/refs/remotes/faraway/存在。
回答by Christopher
One thought: You could test exit status on git ls-remote faraway. This will actually force communication with the remote, instead of just looking for its presence or absence locally.
一个想法:您可以在 上测试退出状态git ls-remote faraway。这实际上将强制与远程通信,而不仅仅是在本地寻找它的存在或不存在。
git ls-remote --exit-code faraway
if test $? = 0; then
....
fi
回答by tig
Another way to check if farawayis defined in .git/config:
另一种检查是否faraway在 中定义的方法.git/config:
if git config remote.faraway.url > /dev/null; then
…
fi
回答by reubano
If the remote is defined in .git/config, you can avoid pinging the remote server with git remote.
如果在 中定义了远程.git/config,则可以避免使用 ping 远程服务器git remote。
if git remote | grep faraway > /dev/null; then
...
fi

