2

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?

Hilikus
  • 9,954
  • 14
  • 65
  • 118
succeed
  • 834
  • 1
  • 11
  • 28

2 Answers2

3

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

Hilikus
  • 9,954
  • 14
  • 65
  • 118
  • I have now cloned the prj(main) into a new empty prj2 directory, did some changes, commited and tried 'git push origin master' it gave the same error – succeed May 15 '15 at 14:38
  • sorry. i didn't notice your first repo was not bare. is that a requirement? you need to have two working copies? if so, what do you expect to happen to the working copy of prj1 when you push the same branch from prj2?? because that's the problem that you are getting. One solution is to make sure that both working copies are not in the same branch at the same time – Hilikus May 15 '15 at 14:49
  • Yeah it was unnecessary to have two working copies, so I did change the first prj repo to bare and then the push worked however the changes are evident when opening the pushed files in the repo they were pushed to? However it does show the commit in 'git log' – succeed May 15 '15 at 16:07
  • you mean opening the pushed file in the *bare* repo? AFAIK, this is not possible, but someone correct me if i'm wrong. You can however, run git commands in your bare repo so you can do for example `git log` and you will see the commits that were pushed from the non-bare repo – Hilikus May 15 '15 at 17:06
  • oh right, so if it is not possible then what is the purpose of pushing into another repo if that push file is not accessible or changed in the main project folder? – succeed May 15 '15 at 17:15
  • the point is to have a shared/central repo that "aggregates" the repos of others and works as a common point. For example, repos in shared server (think gitbhub) don't need a working copy, they are there just to publish a repo that others can clone into working repos – Hilikus May 15 '15 at 17:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77920/discussion-between-hilikus-and-user3464127). – Hilikus May 15 '15 at 17:18
0

If you intend to work in both repos then I found doing a git pull of prj2 from prj1 to be the cleanest solution, rather than trying to push prj2 into prj1. If prj1 was cloned from a third (possibly remote) repository then you can still push as usual to the third (remote) repo from prj1 if you use this set up.

If you don't intend to work in prj1 and it's just a central aggregation point then setting up a bare repository as others have indicated is probably best.

Shane
  • 1,207
  • 13
  • 16