0

I am a beginner with git, wanting to learn and better understand how it works!

I have setup a git repository on my VPS (running Ubuntu 18.04) that's separate from the working directory using git init --separate-git-dir . The git repository is at ~/project.git, while the working directory is at /var/www/project/. I also changed the branch master to main with git branch -m master main.

I did the entire setup on the VPS, and later cloned the repository to my local development machine. It worked, and I got the files from the working directory copied to the local machine.

When I add a hello.txt to my local working directory, and try to push it to the remote repository, I get the following error:

 ! [remote rejected] main -> main (branch is currently checked out)

I would like to know how I can continue developing this project, and "reversing" the local/remote repositories (for lack of better word), so that the one that is on my local machine can be pushed to the remote, without this problem...

I hope this makes sense – thank you for all advice and input!

DmnkVD
  • 75
  • 7
  • You can only push to a bare repository. – mkrieger1 Apr 20 '22 at 09:59
  • Alternatively, instead of pushing from A to B, fetch from A while in B. – mkrieger1 Apr 20 '22 at 10:01
  • Thank you. It doesn't completely answer it, as the top suggested answers seems to result in deleting all the contents of the working directory except `.git`, after initialising it as a bare directory. I would like to keep the files there, while also being able to work on some of them locally and push them to the server... Or perhaps I am not understanding something? I tried to fetch from my local machine while in the VPS folder, but it did not seem to work... – DmnkVD Apr 20 '22 at 10:09
  • Okay, I think this more specifically describes your situation: https://stackoverflow.com/questions/279169/deploy-a-project-using-git-push – mkrieger1 Apr 20 '22 at 10:18
  • A bare repository has no working tree *on purpose*. This keeps people from being in the middle of some work when a push arrives. The push would mess up their work, or be destroyed *by* their work (one or the other *must* happen in this case) so Git rejects the push on the non-bare repository that has that branch checked out. Since the bare repository has no place to do any work, there's no branch actually checked out, and therefore no push can mess up the in-progress work (or vice versa). – torek Apr 20 '22 at 10:36
  • There are some tricky methods for working around this, including the `updateInstead` mentioned in the duplicate, but they should be used **very carefully** at best. (In other words, don't do it!) – torek Apr 20 '22 at 10:37

0 Answers0