简体   繁体   中英

git remote rejected - local repositories

If I have directory1 which contains a selection of files and which is under git control, and I run ..

git clone -l $HOME/directory1 directory2

I now have a copy of directory1 in my cwd.

If I then edit a file in directory2, add it to staging and commit it, I cannot push it to directory1 as I get the error..

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

I don't understand this, as the remote branch (directory1) is up-to-date and I did not alter it between cloning it to directory2 and trying to push it back to directory1.

EDIT: if I run git status inside directory2, it says 'your branch is ahead of origin/master by 1 commit'.

I still don't get it.

The error you're seeing is because the repository in directory1 contains a working copy where master is the currently checked out branch.

At this point, you have two choices:

  1. Turn the repo in directory1 into a bare repository (ie a repository without a working copy)
  2. Tell Git to update the working copy in directory1 when you push from directory2 by setting the receive.denyCurrentBranch option to updateInstead :
     cd directory1 git config receive.denyCurrentBranch updateInstead

You can read more about this option in the 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 is the default value, hence the error when you push. Regarding the updateInstead value:

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

If your goal is to keep directory1 and directory2 in sync, I'd say this is the preferable alternative. However, keep in mind that:

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

Which means that if you have uncommitted changes in directory1 , a push from directory2 will fail since it would overwrite whatever is in the index and the working copy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM