git 我如何拉到一个裸存储库?

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

how do I pull to a bare repository?

git

提问by chila

I have a "main" bare repository and a "personal" bare repository. I want to update changes form "main" to "personal", so I run:

我有一个“主”裸存储库和一个“个人”裸存储库。我想将“主要”更改为“个人”,所以我运行:

$ git pull
fatal: /home/gimenero/applib/git/libexec/git-core/git-pull cannot be used without a working tree.

How do I pull the changes pushed to "main"?

如何将更改推送到“主要”?

回答by Ben James

A git pulldoes a fetchfollowed by a merge, and you can't merge without a working tree. (There would be nowhere to resolve merge conflicts if they should arise.)

Agit pull执行 afetch后跟 a merge,如果没有工作树,您将无法合并。(如果出现合并冲突,将无处可解决。)

Instead, you could just fetch. Assuming your main repository is configured as a remote called origin on your personal repository:

相反,您可以只获取。假设您的主存储库配置为个人存储库上名为 origin 的远程:

$ git fetch origin master:master

Note that this will only be successful if the master branch of your personal repository is mirroring the master branch of the main repository. Otherwise, Git will reject the non-fast-forward fetch.

请注意,只有当您个人存储库的 master 分支镜像主存储库的 master 分支时,这才会成功。否则,Git 将拒绝非快进获取。

回答by antak

Update with:

更新:

$ git fetch origin +refs/heads/*:refs/heads/* --prune

What does this do?

这有什么作用?

First an aside: When we speak of a branch named "xyz", git actually addresses it as refs/heads/xyz. But you can type "xyz" for short because otherwise it would be insane. (Incidentally, tags are refs/tags/xyz.) Plain xyzis ambiguous as it could be a branch, a tag, or the first N letters of a commit hash. refs/heads/xyzon the other hand explicitly represents a branch.

首先,当我们说到一个名为“xyz”的分支时,git 实际上将它称为refs/heads/xyz. 但是你可以简单地输入“ xyz”,否则它会很疯狂。(顺便说一句,标签是refs/tags/xyz。)Plainxyz是不明确的,因为它可以是分支、标签或提交哈希的前 N ​​个字母。 refs/heads/xyz另一方面,明确表示一个分支。

So even though you can type git fetch origin foo:barto grab their foobranch as named barin your repository, you can more explicitly type git fetch origin refs/heads/foo:refs/heads/barto do the same thing. (Although if foowas actually a tag and not a branch, the latter will fail because their refs/heads/foodoesn't exist. Explicitness ftw.)

因此,即使您可以键入git fetch origin foo:bar以获取存储库中foo命名的分支bar,您也可以更明确地键入git fetch origin refs/heads/foo:refs/heads/bar以执行相同的操作。(虽然如果foo实际上是一个标签而不是一个分支,后者将失败,因为它们refs/heads/foo不存在。显式 ftw。)

git fetch origin refs/heads/*:refs/heads/*means all their branch are belong to us. The command is run as if the *part is substituted to their branch name for each of their branches. i.e. git fetch origin refs/heads/abc:refs/heads/abc refs/heads/def:refs/heads/def ...(assuming they have branches named abcand def).

git fetch origin refs/heads/*:refs/heads/*意味着他们所有的分支都属于我们。该命令的运行就像*部件被替换为其每个分支的分支名称一样。即git fetch origin refs/heads/abc:refs/heads/abc refs/heads/def:refs/heads/def ...(假设他们有名为abcand 的分支def)。

The --pruneoption means any branches we have in our repository that matches refs/heads/*but doesn't exist in their repository are deleted.

--prune选项意味着我们在我们的存储库中拥有匹配refs/heads/*但不存在于其存储库中的任何分支都将被删除

Finally, the +prefix is to allow non-fast-forward fetches. Without it, any update to branches that require force-updatesare rejected.

最后,+前缀是允许非快进提取。没有它,任何需要强制更新的分支的更新都会被拒绝。

Put together, the end result is that branches in your repository ends up looking exactly the same as theirs.

放在一起,最终结果是您存储库中的分支最终看起来与它们的完全相同。

Here's an example output:

这是一个示例输出:

 - [deleted]               (none)     -> bar
 * [new branch]            foo        -> foo
   4812558a5f..a6aeec6517  abc        -> abc
 + a1b2c3d4e5...1a2b3c4d5e def        -> def  (forced update)
  • The example tells us they have branches foo, abc, defwhile we have (had) one extra: bar
  • Notice the deletion of barby --pruneand force update of defallowed by the +prefix.
  • 这个例子告诉我们他们有分支foo, abcdef而我们有(有)一个额外的:bar
  • 请注意删除barby--prune并强制更新def允许的+前缀。

Here's what happens instead if +and --prunewere left off:

如果+--prune被忽略,则会发生以下情况:

 * [new branch]            foo        -> foo
   4812558a5f..a6aeec6517  abc        -> abc
 ! [rejected]              def        -> def  (non-fast-forward)

One last thing:

最后一件事:

Compare the command at the top with the following:

将顶部的命令与以下命令进行比较:

$ git fetch origin +refs/heads/*:refs/remotes/origin/* +refs/tags/*:refs/tags/* [--prune]

This is essentially what happens when we type git fetch origin [--prune]!

这基本上就是我们键入时发生的情况git fetch origin [--prune]