1

I want to contribute to open source projects. I am working and living at 2 different locations. I have a VPN setup with a local central repository (in my home network) reachable from both locations. I have 2 developer desktop machines.

The difference to this similar question is that I want to use a server to which I clone the source code first and where I will create the branch and from where I merge the commits to the Gitlab remote repository.

Let us take the example of an open source project on Gitlab. I do a git clone https://gitlab.example.com/opensource.git on my central server and then create a branch there git checkout -b new_feature. Afterwards I clone it to both desktop machines via git clone central-server:/srv/data/Git/opensource on both desktop machines. I developed and committed the changes locally on one of my developer machines. When I tried to push the changes I get an error message, see below. I tried the bare repository as well but got error messages as well. When I implemented the change and did the test, I want to push the changes from my central server to the Gitlab repository.

Question: what is the best workflow and the git commands here?

[user@laptop src]$ git branch
* new_feature
  master
[user@laptop src]$ git status
On branch new_feature
Your branch is ahead of 'origin/new_feature' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
[user@laptop src]$ git push
Enumerating objects: 22, done.
Counting objects: 100% (22/22), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (13/13), done.
Writing objects: 100% (13/13), 1.90 KiB | 1.90 MiB/s, done.
Total 13 (delta 11), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/new_feature
remote: error: By default, updating the current branch in a non-bare repository
remote: is denied, because it will make the index and work tree inconsistent
remote: with what you pushed, and will require 'git reset --hard' to match
remote: the work tree to HEAD.
remote: 
remote: You can set the 'receive.denyCurrentBranch' configuration variable
remote: to 'ignore' or 'warn' in the remote repository to allow pushing into
remote: its current branch; however, this is not recommended unless you
remote: arranged to update its work tree to match what you pushed in some
remote: other way.
remote: 
remote: To squelch this message and still keep the default behaviour, set
remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To central-server:/srv/data/Git/opensource
 ! [remote rejected] new_feature -> new_feature (branch is currently checked out)
error: failed to push some refs to 'central-server:/srv/data/Git/opensource'
[user@laptop src]$ 
Martin Sand
  • 482
  • 1
  • 8
  • 14
  • Possible duplicate of [Git push error '\[remote rejected\] master -> master (branch is currently checked out)'](https://stackoverflow.com/questions/2816369/git-push-error-remote-rejected-master-master-branch-is-currently-checked) – phd Sep 10 '18 at 08:02
  • https://stackoverflow.com/search?q=%5Bgit%5D+remote%3A+error%3A+refusing+to+update+checked+out+branch – phd Sep 10 '18 at 08:02
  • It is not about the error message, it is about having a local server to which I push my commits first before pushing the changes finally to the Gitlab server. – Martin Sand Sep 10 '18 at 09:37

1 Answers1

1

You've initialized your remote repository as a standard one (with workspace) and you're trying to push to it.

In other words central-server:/srv/data/Git/opensource has .git folder inside. You need to create bare respository:

central-server:/srv/data/Git$ git clone --bare ./opensource opensource_bare
laptop:~$ git clone central-server:/srv/data/Git/opensource_bare <somewhere>
laptop:~$ <work on your new clone here>
laptop:~$ git push 

As comments are not a good place to give answers here is what I'd do. Let's assume that you have 4 machines:

  • laptop1
  • laptop2
  • central-server
  • gitlab-server

laptop1 and laptop2 would have clones of the repository with 2 remote repositories configured (git remote add ...):

  • central-server
  • gitlab-server

Whenever you'd like to push something that is "finished" you'll push to gitlab-server. Whenever you'd like to push work in progress you could use central-server. The latter could be used to synchronize your work between laptop1 and laptop2.

Merges are always done on clones, so only on laptop1 and laptop2. central-server would be used only as a additional remote repository accessible from both laptop1 and laptop2.

Marcin Pietraszek
  • 3,134
  • 1
  • 19
  • 31
  • Thanks a lot. I will give it a try when I am at home. Where do I branch? I assume on the central server? – Martin Sand Sep 10 '18 at 07:53
  • It seems like I cannot create a branch on my central server. And in addition it has all branches of the open source Gitlab repository. – Martin Sand Sep 10 '18 at 09:35
  • Not a single command above created a branch. First like created a bare version of the reposiory to which you could push. Branches could be created on `laptop` machine and pushed to origin (`central-server:/srv/data/Git/opensource_bare`). – Marcin Pietraszek Sep 10 '18 at 10:36
  • So download to bare, clone to the 2 developer machines and create a branch there, commit and push branch to local server. Practically, where would you do the merge before committing to the Gitlab repo on the internet, developer machine or local server? – Martin Sand Sep 10 '18 at 11:13