git 远程拒绝 - 本地存储库

git remote rejected - local repositories

如果我有目录 1,其中包含选择的文件并且在 git 控制之下,并且我 运行 ..

git clone -l $HOME/directory1 directory2

我的 cwd 中现在有一个 directory1 的副本。

如果我随后编辑目录 2 中的文件,将其添加到暂存并提交,我无法将其推送到目录 1,因为出现错误..

remote: error: refusing to update checked out branch: refs/heads/master

我不明白这一点,因为远程分支 (directory1) 是最新的,我没有在将其克隆到 directory2 和尝试将其推回 directory1 之间进行更改。

编辑:如果我在目录 2 中 运行 git status,它会显示 'your branch is ahead of origin/master by 1 commit'。

还是没看懂

您看到的错误是因为 directory1 中的存储库包含一个 工作副本 ,其中 master 是当前签出的分支。

此时,您有两个选择:

  1. directory1 中的存储库转换为 bare repository(即没有工作副本的存储库)
  2. 通过将 receive.denyCurrentBranch 选项设置为 updateInstead,告诉 Git 在从 directory2 推送时更新 directory1 中的工作副本:
    cd directory1
    git config receive.denyCurrentBranch updateInstead
    

您可以在 documentation:

中阅读有关此选项的更多信息

If set to true or refuse, git-receive-pack will deny a ref update to the currently checked out branch of a non-bare repository. Such a push is potentially dangerous because it brings the HEAD out of sync with the index and working tree.

refuse 是默认值,因此在您推送时会出现错误。关于updateInstead值:

Another option is updateInstead which will update the working tree if pushing into the current branch.

如果您的目标是保持 directory1directory2 同步,我认为这是更好的选择。但是,请记住:

By default, updateInstead will refuse the push if the working tree or the index have any difference from the HEAD.

这意味着如果您在 directory1 中有未提交的更改,来自 directory2 的推送将失败,因为它会覆盖索引和工作副本中的任何内容。