0

There are two remote repositories as follows.

  • original.git: The original bare repository.
  • mirror.git: A mirror repository cloned with git clone --mirror original.git.

Pushing refs from the mirror to the original remote using git push --mirror works as expected. However, when refspec (e.g. branch name) is combined with git-push, Git tries to delete all the other branches except for the specified one, from both original and mirror repositories.

  1. Why does Git try to delete other branches?
  2. How can I prevent from deleting remote branches in cases where receive.denyDeletes is not set in remote repositories? (I just deleted remote branches by mistake.)

Note: I'm using git v2.18.0 now, and as far as I know, git push --mirror <repo> <refsepc> is not allowed in older versions such as git v1.7.1.

bash-4.1$ cd mirror.git/
bash-4.1$ git branch
* master
  new_branch

bash-4.1$ git config --list | grep remote
remote.origin.url=/user/han/git/original.git/
remote.origin.fetch=+refs/*:refs/*
remote.origin.mirror=true

bash-4.1$ git push --mirror
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 16 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 280 bytes | 280.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /user/han/git/original.git/
 * [new branch]      new_branch -> new_branch

bash-4.1$ git push origin master
To /user/han/git/original.git/
 - [deleted]         new_branch
4

1 回答 1

0

选项规范--mirrorgit push是:

不是命名要推送的每个 ref,而是指定 refs/(包括但不限于refs/heads/refs/remotes/refs/tags/)下的所有 ref 都镜像到远程存储库。新创建的本地 refs 会被推送到远端,本地更新的 refs 会在远端强制更新,删除的 refs 会从远端移除。如果设置了配置选项,这是默认remote.<remote>.mirror设置。

(我的粗体字)。与命令行 refspec结合--mirror使用会使您的 Git 相信命令行中提及的所有引用都已删除,这会导致您的 Git 将“删除此引用”请求1发送到另一个 Git。

有人可能会争辩说这是一个错误——你自己的 Git 应该简单地拒绝与命令行参数 refspec 结合的尝试--mirror——这肯定有点不友好。另请参阅--prune,它具有类似的行为,旨在与命令行 refspecs 结合使用。


1像往常一样,这些采用礼貌的形式“请,如果您愿意,请执行 <thing>”,除非您将--force标志添加到命令行,或者在生成请求的 refspec 中添加加号。不幸的是,默认情况下会遵守删除分支的礼貌请求,这与以非快进方式移动分支的礼貌请求不同。

于 2019-07-17T16:00:25.677 回答