Search code examples
gitversion-controlgit-push

git push error between two local respositories


I have a (main)folder 'prj' with directories and have git init there creating a repo and added and committed all files with git add *

I have created another directory 'prj2' where I have git init and then did a git pull which copied everything over to 'prj2'

now when I make changes to files in the prj2 repo and commit, then do a git push to prj(main) it gives the error:

remote: error: refusing to update checked out branch: refs/heads/master[K remote: error: By default, updating the current branch in a non-bare repository[K remote: error: is denied, because it will make the index and work tree inconsistent[K remote: error: with what you pushed, and will require 'git r.............

is this the correct way to set up a main project(prj) and a second(prj2) from which I can push to the main?

and if pushing is not possible because the main project repo is a non bare git init..etc, then how should one push to the main repo?

or is a different set up better?


Solution

  • The easier way is to clone the main project and configure it as a remote. The fact that it's local is not an issue

    In other words, instead of initing two repos, init --bare one and then clone it.

    Or if they both need to have a working copy (i.e. not bare) then you can make it work only if both repos are not in the same branch at the same time. If they were, that means that the working copy of prj1 would become invalid when prj2 pushed its changes (that's basically why you get that error).

    Finally, if you need to be in the same branch, git is the wrong tool to use since you are talking more of a mirror. In that case something like rsync will work much better