Git 不会在后续克隆上克隆所有分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5563349/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:17:26  来源:igfitidea点击:

Git doesn't clone all branches on subsequent clones?

gitbranchclone

提问by LeSpocky

I have some problems with Git using cloned repositories and branches and it's somehow not possible for me to find an answer to this. Let me describe: we have a bare master Git repository here we all pull from and push to, located on a local linux machine and reachable with ssh. I made a clone of this to my usb thumb drive like this:

我在使用克隆的存储库和分支的 Git 方面遇到了一些问题,我无法找到答案。让我来描述一下:我们在这里有一个裸的主 Git 存储库,我们都从中拉取和推送到,位于本地 linux 机器上,可通过 ssh 访问。我将它复制到我的 USB 拇指驱动器中,如下所示:

git clone ssh://adahl@gollum//net/repos/netcube/patches.git

This gives me of course a local clone with a working copy on my thumb drive. I cd to this and see some branches in this clone then:

这当然给了我一个本地克隆,在我的拇指驱动器上有一个工作副本。我 cd 到这个,然后在这个克隆中看到一些分支:

cd patches
git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/stable

So far so good, however if I clone the repository on my thumb drive another time to my notebook the stable branch is lost. See:

到目前为止一切顺利,但是如果我再次将拇指驱动器上的存储库克隆到我的笔记本上,稳定分支就会丢失。看:

cd ..
git clone patches patches2

cd patches2

git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

I tried several options when cloning or a git fetchafter cloning, nothing brings the stable branch to the patches2repository. I assume I have a lack of understandig git here and simply use it the wrong way. Could someone please point me to my error in usage and/or understanding?

我在克隆时或克隆git fetch后尝试了几个选项,但没有将稳定分支带到patch2存储库。我认为我对 git 缺乏了解,只是以错误的方式使用它。有人可以指出我在使用和/或理解方面的错误吗?

采纳答案by ThiefMaster

See How to clone all remote branches in Git?

请参阅如何在 Git 中克隆所有远程分支?

You need to create a local branch based on the remote branch if you actually want it to be included in a clone. However, since you don't work in remote branches anyway you'll create local branches as soon as you start working on a branch. And before that you don't really need it in your clone since you can simply fetch it from remote at any point.

如果您确实希望将其包含在克隆中,则需要基于远程分支创建本地分支。但是,由于无论如何您不在远程分支中工作,因此您将在开始在分支上工作时立即创建本地分支。在此之前,您的克隆中并不真正需要它,因为您可以随时从远程获取它。

However, if the notebook has no network connectivity, you'll have to create local branches for all remote branches you want so they are cloned when cloning your local repo.

但是,如果笔记本没有网络连接,则必须为所需的所有远程分支创建本地分支,以便在克隆本地存储库时克隆它们。

If you do have network connectivity however, use git remote add origin2 ssh://adahl@gollum//net/repos/netcube/patches.gitand then git fetch origin2- feel free to replace origin2with a more meaningful name.

但是,如果您确实有网络连接,请使用git remote add origin2 ssh://adahl@gollum//net/repos/netcube/patches.git然后git fetch origin2- 随意替换origin2为更有意义的名称。

回答by sehe

In addition to @ThiefMaster:

除了@ThiefMaster:

I like to

我喜欢

git clone --mirror

or

或者

git push --mirror 

to update all (local & remote) branch refs and tags

更新所有(本地和远程)分支引用和标签

Additional infoAs noted, --mirror will really replicate the repo as is, thus overwritany changes in the destination. Branches that do not exist in the source will get pruned unconditionally.

附加信息如前所述,--mirror 将真正按原样复制存储库,从而覆盖目标中的更改。源中不存在的分支将被无条件修剪。

Essentially, it is like working with a remote and doing 'git remote update --prune', the difference being that the branches affected can be local branches as well as 'remote' refs[1]

从本质上讲,这就像使用遥控器并执行“git remote update --prune”,不同之处在于受影响的分支可以是本地分支也可以是“远程”引用 [1]

@LeSpocky(and others?)

@LeSpocky(还有其他人?)

Now if changes disappear, they will never generate merge problems, so that's easy.

现在如果更改消失,它们将永远不会产生合并问题,所以这很容易。

--mirroris named after the real-life concept, so it was designed to pave over any differences in the target. If the target is non-bare, and you had local changes committed, you can always get them back via the reflog of the target's local branch (git log -g, git reflog).

--mirror以现实生活中的概念命名,因此它旨在消除目标的任何差异。如果目标是非裸的,并且您提交了本地更改,则始终可以通过目标本地分支 ( git log -g, git reflog)的引用日志取回它们。

As a general safety measure you could have a hook to 'git stash save' in the target.

作为一般安全措施,您可以在 target 中有一个钩子来“git stash save”

Keep in mind though, that --mirror was designed to, well, mirrorand this question was in fact on how to replicate all branches to a bare remote. :)

但请记住,--mirror 旨在很好地镜像,这个问题实际上是关于如何将所有分支复制到裸机的。:)

[1] (the refs are there, but the remote definitions don't get copied; if you want that, do a manual copy from .git/config to .git/config on the push destination)

[1](参考文件在那里,但远程定义不会被复制;如果你想这样做,请在推送目标上从 .git/config 手动复制到 .git/config)

回答by Amber

"origin" is the default name given to the place you cloned the repo from, which is automatically added as a remote (note: remote just means "a repo not the current one" - remotes can be on the same machine).

“origin”是您克隆存储库的位置的默认名称,它会自动添加为远程(注意:远程仅表示“不是当前的存储库”——远程可以在同一台机器上)。

In patches, "origin" refers to the original repo on gollum.

在 中patches,“origin”指的是 gollum 上的原始 repo。

In patches2, "origin" refers to patches.

在 中patches2,“起源”是指patches

Remote tracking refs (the ones that begin with remotes/) are not actually local branches - they're just pointers to where branches were last known to be on the remote. Thus, in patches, you have remote tracking refs for the original repo, but on patches2, you only have a remote tracking ref for the local masterbranch in patches, because that's where patches2's origin points to.

远程跟踪引用(以 开头的引用remotes/)实际上并不是本地分支——它们只是指向最后知道远程分支在哪里的指针。因此,在 中patches,您有原始存储库patches2的远程跟踪引用,但是在 中,您只有本地master分支的远程跟踪引用patches,因为那是patches2的原点指向的位置。

You can use git remote addto add the original repo as another remote in patches2after cloning it - or you could just clone again from the original repo instead of from patches.

您可以在克隆后将git remote add原始存储库添加为另一个远程存储库patches2- 或者您可以从原始存储库中再次克隆,而不是从patches.

回答by Edson Medina

$ git remote update
$ git pull --all