1

I'm learning and adjusting my workflow integrating Git. My wish is to be able to push my development branch to my test server from my local terminal. My current setup is giving me an error once I branch of. I have the feeling it's not an error but Git doing its work, in combination with me not understanding it completely. Are you able to explain why this is happening and how to avoid it?

The Git "error"

! [remote rejected] feature -> feature (branch is currently checked out)
error: failed to push some refs to (I hide the ssh server address)

How I came to this situation:

  • Create repo on github
  • Include deployment keys generated via cpanel from my server
  • Git clone on local machine
  • Git clone on server via SSH
  • Add server as remote to the repo as test

Flow

  • Adjusting master locally, pushing it to github and server all works fine. But my understanding is that the power of Git is create a branch for a 'feature' > test it > merge it to the master.

  • I make a branch called feature on my local machine

git checkout -b feature

  • Make some changes to the repo and push it to origin and test

git add . git commit -m 'added index.html' git push origin feature (for backup/sync purposes) git push test feature (to be able so see my code working on the test server

  • I have to 'checkout' the new branch in order to show the files on the server, so I SSH to my server and checkout the new branch:

    git checkout feature

My thinking is, that from this point on, I can work on feature branch locally > commit adjustments and with a simple push command git push test feature I can test the code on my test server.

Broke the flow

But now my flow is broken. After I check out the feature branch on my server, I'm not able to push my adjusted branch to my remote test. Git returns the message shown above.

Ole
  • 59
  • 7

1 Answers1

1

Git clone on server via SSH

You would need, still on that server through your SSH session, to add:

cd /path/to/cloned/repo
git config --local receive.denyCurrentBranch updateInstead

Meaning, using Git 2.4 or more, using a push-to-deploy, in order to allow a git push to directly update a checked out working tree.
Usually, you would rather push to a bare repository: see "What is a bare repository and why would I need one?", and use a post-receive hook with a checkout.
That is: an executable (chmod 775) file named post-receive, in a "myrepo.git/hooks" folder, as detailed in the discussion.
myrepo.git is a git clone --bare of your repo on the server: you can push to it.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you for your reply @VonC, you're pointing me in an interesting direction. I will read up on push-to-deploy and bare repositories. – Ole Jun 28 '20 at 08:32
  • I've read your information, and I think from the information a bare repo is what will fulfill my needs. I'm able to clone a bare version of my repo to my server 'git clone --bare [url repo github]' A folder is being created 'name repo.git' with none of the project files inside. When I push to the repo from my local machine: 'git push [name remote] [name branch]' = everything up-to-date but no files have been pushed (what I understand, because I think with bare repo I'm disconnecting). I think I have created a bare, but no idea how to push/update for deploy purposes. Do you know? – Ole Jun 28 '20 at 12:08
  • @Ole Just to be clear, a bare repo still has files: *internal* files in `refs/`, `objects/`, .... You need to add a `post-receive` hook in the hooks folder in order to checkout those files once pushed. – VonC Jun 28 '20 at 12:23
  • @Ole Pushing to a bare means simply using the `repo.git` folder path in your push URL. The `post-receive` hook, through at least a simple echo, can confirm when you have pushed *new* commit successfully. – VonC Jun 28 '20 at 13:51
  • The push part I understand. With the --bare command, the repo.git is being created. So I thought pushing to that folder path would make sense instead of the repo folder. (that does not excist btw after) Having some trouble understanding and creating the post receive hook. Not sure what goes where... – Ole Jun 28 '20 at 14:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216824/discussion-between-vonc-and-ole). – VonC Jun 28 '20 at 14:21