如果还没有本地,则可以使用克隆进行拉取的 Git 快捷方式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15602059/
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
Git shortcut to pull with clone if no local there yet?
提问by inger
Is there a one-command way to get an up-to-date mirror of a remote repo? That is
有没有一种单一命令的方式来获取远程仓库的最新镜像?那是
- if local repo not there yet: clone
- if it's there: pull
- 如果本地仓库还没有:克隆
- 如果它在那里:拉
I know I could script this around (e.g if [ -d repo ]; then (cd repo && git pull); else git clone $repourl;fi
) , but I need the simplest possible cross-platform way (actually used for Jenkins-CI, which I know does this by default, however I need 2 repos for which support is limited).
我知道我可以围绕(例如if [ -d repo ]; then (cd repo && git pull); else git clone $repourl;fi
)编写脚本,但我需要最简单的跨平台方式(实际上用于 Jenkins-CI,我知道默认情况下会这样做,但是我需要 2 个支持有限的存储库)。
Git has similar shortcuts for other things (eg. checkout -b, and pull itself), so I'm wondering if I missed something. Thanks!
Git 对其他事情也有类似的快捷方式(例如 checkout -b 和 pull 自身),所以我想知道我是否遗漏了什么。谢谢!
采纳答案by VonC
git pull
knows from where to clone because the local repo has a remote registered in its local config. It operates from a working tree.
git pull
知道从哪里克隆,因为本地存储库在其本地配置中注册了一个远程。它从一个工作树运行。
But git clone
doesn't, it must have an explicit remote url passed in parameter in order to clone. It operates outside the working tree.
但git clone
不是,它必须有一个显式的远程 url 传入参数才能克隆。它在工作树之外运行。
The main reason for such a shortcut to notexist is that:
这种快捷方式不存在的主要原因是:
- you
git init
rarely for a given repo: it is a one-time command in the life of the repo.
Plus it might need additionalcommand to be complete: if you havesubmodules, for instance, you would need to add agit submodule update --init
. - you
git pull
often within a given repo. git pull is in itself a shortcut (forgit fetch
+git merge
). Evengit pull --rebase
is another shortcut forgit fetch
+git rebase
.
- 你
git init
很少使用给定的 repo:它是 repo 生命周期中的一次性命令。
此外,它可能需要额外的命令才能完成:例如,如果您有子模块,则需要添加一个git submodule update --init
. - 你
git pull
经常在给定的回购中。git pull 本身就是一个快捷方式(对于git fetch
+git merge
)。Evengit pull --rebase
是git fetch
+ 的另一个快捷方式git rebase
。
Considering the number of times you are to use git init
, such a shortcut is not an high priority.
考虑到您要使用的次数git init
,这种快捷方式的优先级并不高。
So a script remains the surest way to define what you need.
因此,脚本仍然是定义所需内容的最可靠方式。
回答by Amber
There is not, given that the commands which operate on existing repos all assume that they're being run inside a given repo.
没有,因为在现有存储库上运行的命令都假设它们是在给定存储库中运行的。
That said, if you're running in a shell, you could simply make use of the shell built-ins. For instance, here's bash:
也就是说,如果您在 shell 中运行,则可以简单地使用 shell 内置程序。例如,这里是 bash:
if cd repo; then git pull; else git clone https://server/repo repo; fi
This checks to see if repo
is a valid directory, and if so, does a pull
within it; otherwise it does a clone
to create the directory.
这会检查是否repo
是有效目录,如果是,则pull
在其中执行 a ;否则它会clone
创建目录。
回答by jhrmnn
The cleanest one-liner might be
最干净的单线可能是
git -C repo pull || git clone https://server/repo repo
回答by Noam Manos
回答by andylukem
If upgrading git is not an option, and you don't want to pass an argument for a repo, a scripting approach could be:
如果升级 git 不是一种选择,并且您不想为 repo 传递参数,则脚本方法可能是:
#!/bin/bash
function clone_pull {
DIRECTORY=$(basename "" .git)
if [ -d "$DIRECTORY" ]; then
cd "$DIRECTORY"
git pull
cd ../
else
git clone ""
fi
}
clone_pull https://github.com/<namespace>/<repo>
# or
clone_pull [email protected]:<namespace>/<repo>.git
回答by yoyo
Since you mentioned Jenkins, it is possible to clone or pull using the Jenkins SCM API. The checkout
method does exactly what you want.
由于您提到了 Jenkins,因此可以使用Jenkins SCM API进行克隆或拉取。该checkout
方法完全符合您的要求。
You can run it from a groovy script like so:
你可以像这样从一个 groovy 脚本运行它:
dir (targetFolder) {
checkout(scm: [$class: 'GitSCM', branches: [[name: 'master']], ...])
}
Note that it does not check out the specified branch in the git sense, but rather leaves the workspace on a detached head at the corresponding commit.
请注意,它不会检出 git 意义上的指定分支,而是将工作区留在相应提交时的分离头上。